Azzera filtri
Azzera filtri

Several vectors as inputs to function

2 visualizzazioni (ultimi 30 giorni)
Simon
Simon il 17 Apr 2014
Commentato: lvn il 17 Apr 2014
Hi! I am trying to calculate values of Black-Scholes options using a function that requires inputs:
Values=BSCall(S,K,T,v,r)
Where v and r are both 1x11 arrays of risk-free rates and volatilities that correspont to each other. This means that I want in total 11 option values to be calculated. How can I do this without using loops?
Regards
Simon

Risposte (1)

lvn
lvn il 17 Apr 2014
It all depends whether your function BSCall supports vector input. If it does, then just
Values=BSCall(S,K,T,v,r)
with v and r vectors should work. On the other if BSCall expects scalar v and r, then your only option will be to use a for loop (or of course to change BSCall to support vector input).
  2 Commenti
Simon
Simon il 17 Apr 2014
Thanks!
I gueess I'll try to make it support vector input then. I am new to Matlab, so there are lots of things that are confusing to me. It seems to be the case that the code can handle one vector, such as a vector with different stock prices, but as soon as I use several vectors as input something goes wrong. Here is my code:
function [Call]=BScall(S0,T,K,sigma,r)
d1=(log(S0./K)+(r+0.5.*sigma.^2).*T)/(sigma.*sqrt(T));
d2=d1-sigma.*sqrt(T);
if T>0
Call=S0.*N(d1)-K.*exp(-r*T).*N(d2);
else
Call=max(S0-K,0);
end
end
The code is obviously short and easy, but I cannot seem to make it work with vectors. I'd be grateful for any help! Regards Simon
lvn
lvn il 17 Apr 2014
What is the error message? In any case, I think there should be a dot before the second / in your formula.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by