matlab订阅的分配维度不匹配。

时间:2017-05-27 10:02:48

标签: matlab mismatch

我已经构建了这个脚本,但我无法在其中找到问题矩阵。 这是我的回复呼叫标准选项的脚本:

N=50;
T=90/252;
dt=T/N;
K=102;
S0=100;
B0=1;
r=0.02;

sigma=0.25;



for i=1:N
    ttomat(i)=(N-i+1)*dt;   %+1 serve per aggiustare il tempo
    d1(i)=(log(S(i,:)./K)+(r+0.5*sigma^2)*ttomat(i))./(sigma*sqrt(ttomat(i)));
    d2(i)=d1(i)-sigma*sqrt(ttomat(i));
    Call(i,:)=S(i,:).*normcdf(d1(i))-K*exp(-r*ttomat(i)*normcdf(d2(i)));
    alpha(i,:)=normcdf(d1(i));    %delta della Call 
    beta(i,:)=(Call(i,:)-alpha(i,:).*S(i,:))./(B0*exp(r*(i-1)*dt));

end

1 个答案:

答案 0 :(得分:0)

您必须初始化/预分配结果,该结果以循环计算。在您的代码中,您尚未预先分配结果。预分配可帮助您快速实现结果。预分配所需变量始终是最佳实践。检查下面的代码,它适合你吗?

N=50;
T=90/252;
dt=T/N;
K=102;
S0=100;
B0=1;
r=0.02;

sigma=0.25;

S = rand(N,1) ;

ttomat = zeros(1,N) ;
d1 = zeros(1,N) ;
d2 = zeros(1,N) ;
Call = zeros(1,N) ;
alpha = zeros(1,N) ;
beta = zeros(1,N) ;
for i=1:N
    ttomat(i)=(N-i+1)*dt;   %+1 serve per aggiustare il tempo
    d1(i)=(log(S(i,:)./K)+(r+0.5*sigma^2)*ttomat(i))./(sigma*sqrt(ttomat(i)));
    d2(i)=d1(i)-sigma*sqrt(ttomat(i));
    Call(i,:)=S(i,:).*normcdf(d1(i))-K*exp(-r*ttomat(i)*normcdf(d2(i)));
    alpha(i,:)=normcdf(d1(i));    %delta della Call 
    beta(i,:)=(Call(i,:)-alpha(i,:).*S(i,:))./(B0*exp(r*(i-1)*dt));

end