各位好,下面是我写的Code》
function [price]=EuropeanUpAndOutCall(S,X,B,r,d,deltaT,sigma)
%S is the current price of the underlying asset,
%X is the exercise price of the option,
%B is the barrier of the option,
%r is the risk-free rate,
%d is the dividend,
%deltaT is the time to maturity,
%sigma is the volatility of the asset price,
S = 1389.46;
NUM = xlsread('EuropeanUpAndOutCall.xls');
B = NUM(:,1);
X = NUM(:,2);
%问题的关键部分在下面
for i = 1:5
deltaT = NUM(i,3);
sigma = NUM(i,4);
r = NUM(i,7);
d = NUM(i,8);
lambda = (r-d+sigma^2/2)/(sigma^2);
y = (log(B.^2./(S*X)))/(sigma*sqrt(deltaT)+eps)+lambda*sigma*sqrt(deltaT);
x1 = (log(S./B))/(sigma*sqrt(deltaT)+eps)+lambda*sigma*sqrt(deltaT);
y1 = (log(B/S))/(sigma*sqrt(deltaT)+eps)+lambda*sigma*sqrt(deltaT);
d1 = (log(S./X)+(r-d+sigma^2/2)*deltaT)/(sigma*sqrt(deltaT)+eps);
d2 = d1-sigma*sqrt(deltaT);
f1 = S*exp(-d*deltaT)*normcdf(d1)-X*exp(-r*deltaT).*normcdf(d2);
f2 = -S*normcdf(x1)*exp(-d*deltaT)+X*exp(-r*deltaT).*normcdf(x1-sigma*sqrt(deltaT));
f3 = S*exp(-d*deltaT)*((B/S).^(2*lambda)).*(normcdf(-y)-normcdf(-y1));
f4 = X*exp(-r*deltaT).*((B/S).^(2*lambda-2)).*(normcdf(-y+sigma*sqrt(deltaT))-normcdf(-y1+sigma*sqrt(deltaT)));
price = f1 + f2 + f3 - f4;
end
end
最终的price是输出量,在for循环中,对于每一个i值得出的price是一个26行的向量,但是最后在命令窗口只出现最后循环的值,中间的price值被覆盖了,请问怎样能把所有的结果,即一个26行5列的矩阵输出到excel的表格里呢?
我在网上有搜到说先定义一个空矩阵a,最后在得出price值之后,再定义a =[a;price],但还是不行,请各位指教,实在不知道该怎么解决。。。多谢多谢