Vectorization of a function
이전 댓글 표시
I have dotted all the variable that are defined as a vector, but matlab gives me an error.
Do you see where is my problem?
function [C] = BlackScholesCall(S,K,t,r,sigma)
% Calculates the price of a call option
% INPUT S 1x1 ... Current stock price (underlying)
% K 1x1 ... Strike price
% t 1x1 ... Time to maturity
% r 1x1 ... Risk-free interest rate
% sigma 1x1 ... standard deviation (volatility of the underlying)
% OUTPUT C 1x1 ... The price of a call option
% USAGE BlackScholesCall(S,K,t,r,sigma)
d1=(log(S/K.))+(r+(1/2)*sigma^2)*t)/(sigma*sqrt(t));
d2=d1-sigma*sqrt(t.);
C=(S*normcdf(d1))-(K.*(exp(-r*.t))*normcdf(d2));
end
Code to call the function:
S = 22
K = 20:25
t = 0.1:0.1:0.6
r = 0.02
sigma = 0.25
C = BlackScholesCall(S, K, t, r, sigma)
Thank you very much!
댓글 수: 2
Geoff Hayes
2019년 9월 30일
편집: Geoff Hayes
2019년 9월 30일
Francesco - please copy and paste the full error message to this question. In the function header, you have
t 1x1 ... Time to maturity
which implies that t is a scalar...but in your input to this function, you define t as an array
t = 0.1:0.1:0.6
Which should it be - a scalar or an array? Perhaps this is the problem...you are passing in an array but the code is expecting a scalar? Are you the author of BlackScholesCall?
Francesco Rossi
2019년 9월 30일
채택된 답변
추가 답변 (1개)
meghannmarie
2019년 9월 30일
1 개 추천
I think you have some of your dot operators wrong:
d1 = (log(S./K) +(r+(1/2)*sigma^2)*t)/(sigma*sqrt(t));
d2 = d1-sigma*sqrt(t);
C = (S*normcdf(d1)) - (K.*(exp(-1.*t)).*normcdf(d2));
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!