Index exceeds matrix dimensions.
Show older comments
I plan to use matlab doing the simulation, but it appears "Index exceeds matrix dimensions", could you find out what's wrong with my code? Thank you. The code is as following:
price= LTFGROUP1Predicted;
p0(1)=price(1);
p1(1) = price(2);
n1(1) = 0.25;
n2(1) = 0.25;
n3(1) = 0.25;
n4(1) = 0.25;
nr = 0;
t=1;
u1(1) = 0;
u2(1)= 0;
u3(1)= 0;
u4(1)= 0;
beta = 0.4;
eta = 0.7;
delta=0.9;
div_type=1;
L =0;
w = 0.85;
g_weak=0.3;
g_strong=0.7;
a=0.5;
b1 = 1.5;
b2 = -1;
v =0.25;
av_p(1) =p0(1);
pe1(1) = p0(1) ;
pe2(1) = p0(1) ;
pe3(1) = p0(1) ;
pe4(1) = p0(1);
pe1old(1) = pe1(1) ;
pe2old(1) = pe2(1) ;
pe3old(1)= pe3(1) ;
pe4old(1) = pe4(1);
marketprice(1)= LTFGROUP1Price(1);
for t=2:50
% plag(t) = p0(t-1);
% p0(t) = p1(t-1);
plag(t) = p0(t-1);
p0(t)=marketprice(t-1);
pe1old(t) = pe1(t-1) ;
pe2old(t) = pe2(t-1) ;
pe3old(t)= pe3(t-1) ;
pe4old(t) = pe4(t-1);
pe1(t) = w*p0(t) + (1-w)*pe1(t-1);
pe2(t) = p0(t) -0.3*(p0(t)-plag(t));
pe3(t) = p0(t) +0.7*(p0(t)-plag(t)) ;
pe4(t) = a*av_p(t-1) + b1*p0(t) + b2*plag(t);
meanprediction(t)=n1(t-1) * pe1(t-1)+n2(t-1) * pe2(t-1)+n3(t-1) * pe3(t-1)+ n4(t-1) * pe4(t-1);
marketprice(t)=price(t);
if (t<21)
%p1(t) = 20*(114.8-meanprediction(t))/21+0.3*noise(1,t);
p1(t) = 20*(2.8+meanprediction(t))/21+0.3*noise(1,t);
end
if (t>20 && t<44)
p1(t) = 20*(2.05+meanprediction(t))/21+0.3*noise(1,t);
end
if (t>43)
p1(t) = 20*(3.1+meanprediction(t))/21+0.3*noise(1,t);
end
%Average Past Price is Updated
if (L == 0)
av_p(t) = (av_p(t-1)*(t-1) + p1(t)) / t ;
end
if (L == 1)
av_p = v*pnew + (1-v)*av_p ;
end
if (L == 2)
av_p = (pnew + p0) / 2;
end
if (L == 3)
av_p(t) = (p1(t) + p0(t) + plag(t))/3;
end
if (L == 4)
av_p = pf ;
end
err1(t) = marketprice(t) - pe1old(t);
err2(t) = marketprice(t) - pe2old(t);
err3(t) = marketprice(t) - pe3old(t);
err4(t) = marketprice(t) - pe4old(t);
%cst=1000;
cst=0;
u1(t) = eta*u1(t-1) - err1(t)*err1(t)+cst;
u2(t) = eta*u2(t-1) - err2(t)*err2(t)+cst;
u3(t) = eta*u3(t-1) - err3(t)*err3(t)+cst;
u4(t) = eta*u4(t-1) - err4(t)*err4(t)+cst;
s(t) = exp(beta*u1(t))+exp(beta*u2(t))+exp(beta*u3(t))+exp(beta*u4(t));
n1 (t)= (1-delta)*exp(beta*u1(t)) / s(t) + delta*n1(t-1);
n2 (t)= (1-delta)*exp(beta*u2(t)) / s(t) + delta*n2(t-1);
n3 (t) =(1-delta)*exp(beta*u3(t)) / s(t) + delta*n3(t-1);
n4 (t) =(1-delta)*exp(beta*u4(t)) / s(t) + delta*n4(t-1);
end
x=1:50;
plot(x,marketprice(x),'s-b',x,p1(x),'o-r');
*Index exceeds matrix dimensions.*
Answers (1)
Are Mjaavatten
on 5 Mar 2017
0 votes
It appears that one of your arrays has too few elements. On the editor toolstrip, select
Breakpoints -> Stop on errors
and inspect the size of the array when the the error occurs.
Categories
Find more on Language Fundamentals 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!