help, im getting the following error: error using zeros size input must be scalar

1 view (last 30 days)
% data
S=70; n=10; K=100; r=0.05; delta=0.10; sigma=0.2; dt=1; d=4; b=50;
% allocate storage
syms j x
for j=1:1:d
w(j)=j;
end
for i=1:1:b
j=1:1:d;
v(i,j)=i+j;
end
% initialize parameters
v(1,1) = S;
w(1) = 1;
v=zeros(j+1,n);
for j = 2:1:d
v(i,j) = S(i,j)*exp(((r-delta-(sigma)^2)/2)*dt+sigma*(sqrt(dt*randn)));
w(j) = 1;
end
% process tree
j = d;
while (j > 0)
switch j
case 1
v=zeros(j+1,n);
for j=1:n
v(w(j),j) = max(max((S-K),0),exp(-r*max(S-K,0)));
v(w(j)+1,j) = S(w(j),j)*exp(((r-delta-(sigma)^2)/2)*dt+sigma*(sqrt(dt*randn)));
w(j) = w(j)+1;
end
case 2
v(w(j),j) = max(max((S-K),0),exp(-r*max(S-K,0)));
w(j)=0;
j=j-1;
case 3
v=zeros(j+1,n);
v(w(j),j)=max(max((S-K),0),(symsum((1/b)*exp(-r*max((S-K),0)),j,1,b)));
if (j>1)
for j=1:n
v(w(j)+1,j)=S(w(j),j)*exp(((r-delta-(sigma)^2)/2)*dt+sigma*(sqrt(dt*randn)));
w(j)=w(j)+1;
for i=j+1:1:d
v(i,j)=S(w(j),j)*exp(((r-delta-(sigma)^2)/2)*dt+sigma*(sqrt(dt*randn)));
w(i)=1;
end
j=d;
else
j=0;
end
end
case 3
v(w(j),j)=max(max((S-K),0),(symsum((1/b)*exp(-r*max((S-K),0)),j,1,b)));
w(j)=0;
j=j-1;
end
end

Answers (1)

Walter Roberson
Walter Roberson on 25 Feb 2020
for i=1:1:b
j=1:1:d;
That code sets j to be a vector. Later you try to use j+1 as the first parameter to zeros(). You can only pass a vector to zeros if the vector is the only numeric input.
It is unlikely that you want to use a vector for j at that point.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!