Index exceeds the number of array elements (1).
Show older comments
I am unable to spot why I am getting an error. Please help to resolve the error.
x1_rec(1)=x1(1);
x2_rec(1)=x2(1);
y1_rec(1)=y1(1);
y2_rec(1)=y2(1);
%value of constants
%a1=x1
a1=ksaix1(1);
a2=ksaix1(2);
a3=ksaix1(3);
a4=ksaix1(4);
a5=ksaix1(5);
a6=ksaix1(6);
a7=ksaix1(7);
a8=ksaix1(8);
a9=ksaix1(9);
a10=ksaix1(10);
a11=ksaix1(11);
a12=ksaix1(12);
a13=ksaix1(13);
a14=ksaix1(14);
a15=ksaix1(15);
a16=ksaix1(16);
a17=ksaix1(17);
a18=ksaix1(18);
a19=ksaix1(19);
a20=ksaix1(20);
a21=ksaix1(21);
a22=ksaix1(22);
a23=ksaix1(23);
a24=ksaix1(24);
a25=ksaix1(25);
a26=ksaix1(26);
a27=ksaix1(27);
a28=ksaix1(28);
a29=ksaix1(29);
a30=ksaix1(30);
a31=ksaix1(31);
a32=ksaix1(32);
%c=x2
c1=ksaix2(1);
c2=ksaix2(2);
c3=ksaix2(3);
c4=ksaix2(4);
c5=ksaix2(5);
c6=ksaix2(6);
c7=ksaix2(7);
c8=ksaix2(8);
c9=ksaix2(9);
c10=ksaix2(10);
c11=ksaix2(11);
c12=ksaix2(12);
c13=ksaix2(13);
c14=ksaix2(14);
c15=ksaix2(15);
c16=ksaix2(16);
c17=ksaix2(17);
c18=ksaix2(18);
c19=ksaix2(19);
c20=ksaix2(20);
c21=ksaix2(21);
c22=ksaix2(22);
c23=ksaix2(23);
c24=ksaix2(24);
c25=ksaix2(25);
c26=ksaix2(26);
c27=ksaix2(27);
c28=ksaix2(28);
c29=ksaix2(29);
c30=ksaix2(30);
c31=ksaix2(31);
c32=ksaix2(32);
h=0.1; %step size
t=0:h:50;
%ode
p=@(t,x1_rec,y1_rec,x2_rec,y2_rec) a1+a2*x1_rec+a3*x2_rec+a4*y1_rec+a5*y2_rec+a6*x1_rec*x2_rec+a7*y1_rec*y2_rec+a8*x1_rec*y1_rec+a9*x1_rec*y2_rec+a10*x2_rec*y1_rec+a11*x2_rec*y2_rec+a12*x1_rec*x2_rec*y1_rec+a13*x1_rec*x2_rec*y2_rec+a14*x1_rec*y1_rec*y2_rec+a15*x2_rec*y1_rec*y2_rec+a16*x1_rec*x2_rec*y1_rec*y2_rec+a17*x1_rec^2+a18*x2_rec^2+a19*y1_rec^2+a20*y2_rec^2+a21*x1_rec^2*x2_rec+a22*x1_rec^2*y1_rec+a23*x1_rec^2*y2_rec+a24*x2_rec^2*x1_rec+a25*x2_rec^2*y1_rec+a26*x2_rec^2*y2_rec+a27*y1_rec^2*x1_rec+a28*y1_rec^2*x2_rec+a29*y1_rec^2*y2_rec+a30*y2_rec^2*x1_rec+a31*y2_rec^2*x2_rec+a32*y2_rec^2*y1_rec;
q=@(t,x1_rec,y1_rec,x2_rec,y2_rec) c1+c2*x1_rec+c3*x2_rec+c4*y1_rec+c5*y2_rec+c6*x1_rec*x2_rec+c7*y1_rec*y2_rec+c8*x1_rec*y1_rec+c9*x1_rec*y2_rec+c10*x2_rec*y1_rec+c11*x2_rec*y2_rec+c12*x1_rec*x2_rec*y1_rec+c13*x1_rec*x2_rec*y2_rec+c14*x1_rec*y1_rec*y2_rec+c15*x2_rec*y1_rec*y2_rec+c16*x1_rec*x2_rec*y1_rec*y2_rec+c17*x1_rec^2+c18*x2_rec^2+c19*y1_rec^2+c20*y2_rec^2+c21*x1_rec^2*x2_rec+c22*x1_rec^2*y1_rec+c23*x1_rec^2*y2_rec+c24*x2_rec^2*x1_rec+c25*x2_rec^2*y1_rec+c26*x2_rec^2*y2_rec+c27*y1_rec^2*x1_rec+c28*y1_rec^2*x2_rec+c29*y1_rec^2*y2_rec+c30*y2_rec^2*x1_rec+c31*y2_rec^2*x2_rec+c32*y2_rec^2*y1_rec;
%loop
for i=1:(length(t)-1)
k1=p(t(i),x1_rec(i),y1_rec(i),x2_rec(i),y2_rec(i));
l1=q(t(i),x1_rec(i),y1_rec(i),x2_rec(i),y2_rec(i));
k2=p(t(i)+h/2,(x1_rec(i)+(h/2)*k1),(y1_rec(i)+(h/2)*l1),(x2_rec(i)+(h/2)*k1),(y2_rec(i)+(h/2)*l1));
l2=q(t(i)+h/2,(x1_rec(i)+(h/2)*k1),(y1_rec(i)+(h/2)*l1),(x2_rec(i)+(h/2)*k1),(y2_rec(i)+(h/2)*l1));
k3=p(t(i)+h/2,(x1_rec(i)+(h/2)*k2),(y1_rec(i)+(h/2)*l2),(x2_rec(i)+(h/2)*k2),(y2_rec(i)+(h/2)*l2));
l3=q(t(i)+h/2,(x1_rec(i)+(h/2)*k2),(y1_rec(i)+(h/2)*l2),(x2_rec(i)+(h/2)*k2),(y2_rec(i)+(h/2)*l2));
k4=p(t(i)+h,(x1_rec(i)+k3*h),(y1_rec(i)+l3*h),(x2_rec(i)+k3*h),(y2_rec(i)+l3*h));
l4=q(t(i)+h,(x1_rec(i)+k3*h),(y1_rec(i)+l3*h),(x2_rec(i)+k3*h),(y2_rec(i)+l3*h));
x1_rec(i+1) = x1_rec(i) + h*(k1+2*k2+2*k3+k4)/6;
%y1(i+1) = y1(i) + h*(l1+2*l2+2*l3+l4)/6;
x2_rec(i+1) = x2_rec(i) + h*(k1+2*k2+2*k3+k4)/6;
%y2(i+1) = y2(i) + h*(l1+2*l2+2*l3+l4)/6;
end
plot(x1_rec(1:length(x1)),'r','LineWidth',2)
plot(x2_rec(1:length(x2)),'r','LineWidth',2)
%%%%%Error%%%%
Index exceeds the number of array elements (1).
Error in coup (line 333)
k1=p(t(i),x1_rec(i),y1_rec(i),x2_rec(i),y2_rec(i));
Answers (2)
KSSV
on 25 Nov 2020
The error is clear...you are trying to extract more number of elements than present int he array.
A = rand(1,10) ;
A(1) % no error
A(5) % no error
A(end) % no error
A(11) % error, as there is no 11th element
Your code is very long and also you are accessing some local data in between which we don't have access to. So we cannot help you exact on the point. But the error occurs due to the reason mentioned above.
6 Comments
Heya :)
on 25 Nov 2020
Matlab tells you:
Error in coup (line 333)
k1=p(t(i),x1_rec(i),y1_rec(i),x2_rec(i),y2_rec(i));
If you use the option i mentioned you can check whcih value has i in the moment the error appears and what are the lengths of t, x1_rec, y1_rec, x2_rec, y2_rec.
This should be enough information to understand whats going wrong.
Heya :)
on 25 Nov 2020
KSSV
on 25 Nov 2020
You have messed up with your code at lot of places.
Heya :)
on 25 Nov 2020
Nora Khaled
on 25 Nov 2020
0 votes
can't run your code because it need to open file.
But check the sizes x1_rec, y1_rec, x2_rec, and y2_rec.
it seems that you the for loop exceed the number of elements.
8 Comments
Heya :)
on 25 Nov 2020
Nora Khaled
on 25 Nov 2020
are they equal to the size of t ?
cause matlab says its in line 333
Error in coup (line 333)
k1=p(t(i),x1_rec(i),y1_rec(i),x2_rec(i),y2_rec(i));
so you should check here..
for i=1:(length(t)-1)
k1=p(t(i),x1_rec(i),y1_rec(i),x2_rec(i),y2_rec(i));
l1=q(t(i),x1_rec(i),y1_rec(i),x2_rec(i),y2_rec(i));
Heya :)
on 25 Nov 2020
Heya :)
on 25 Nov 2020
Nora Khaled
on 25 Nov 2020
This does not seem right...
you have in line 326 and 327
h=0.1; %step size
t=0:h:50;
so, t should be
size(t)
ans =
1 501
meaning that the loop will be form 1 to 500.
Heya :)
on 25 Nov 2020
KSSV
on 25 Nov 2020
so use linspace:
t = linspace(0,50,50) ;
Or run the loop index upto length of t.
Heya :)
on 25 Nov 2020
Categories
Find more on Matrix Indexing 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!