Index exceeds the number of array elements (11)

I need help please. I am trying write a program with the following steps:
(i) to obtain x(i) and y(i), i=1,2,...,m
(ii) to create a data matrix with the following elements:
row 1: [x(i) y(i) x(i)^2 x(i)*y(i) x(i)^2*y(i) ...]
row 2: [x(i+1) y(i+1) x(i+1)^2 x(i+1)*y(i+1) x(i+1)^2*y(i+1)...]
row 3: [x(i+2) y(i+2) x(i+2)^2 x(i+2)*y(i+2) x(i+2)^2*y(i+2)...]
row 4: [x(i+3) y(i+3) x(i+3)^2 x(i+3)*y(i+3) x(i+3)^2*y(i+3)...]
row 5: ....
and so on.
Row 1 and row 2 has no problem. However, when I enter row 3, I get the following error: "Index exceeds the number of array elements (11)". Can someone help me how to fix this error? Appreciation in anticipation.
Here is what I have done:
clear
h = 0.01; % time step
x(1)=1;
y(1)=1;
m=10; h = 0.01;
dxdt = @(x,y) y;
dydt = @(x,y) x-x^3;
for i=1:1:m
x(i+1) = x(i) + h*dxdt(x(i), y(i));
y(i+1) = y(i) + h*dydt(x(i), y(i));
end
% Data Matrices X
X = [x(i) y(i) x(i)^2 x(i)*y(i) x(i)^2*y(i) x(i)*y(i)^2 y(i)^2 y(i)^3 y(i)*x(i)^3 x(i)*y(i)^3 y(i)^3 x(i)^4 y(i)*x(i)^4 y(i)^4;
x(i+1) y(i+1) x(i+1)^2 x(i+1)*y(i+1) y(i+1)*x(i+1)^2 x(i+1)*y(i+1)^2 y(i+1)^3 y(i+1)*x(i+1)^3 x(i+1)*y(i+1)^3 y(i+1)^3 x(i+1)^4 y(i+1)*x(i+1)^4 y(i+1)*x(i+1)^4 y(i+1)^4;
x(i+2) y(i+2) x(i+2)^2 x(i+2)*y(i+2) y(i+2)*x(i+2)^2 x(i+2)*y(i+2)^2 y(i+2)^3 y(i+2)*x(i+2)^3 x(i+2)*y(i+2)^3 y(i+2)^3 x(i+2)^4 y(i+2)*x(i+2)^4 y(i+2)*x(i+2)^4 y(i+2)^4];

5 commentaires

KSSV
KSSV le 12 Oct 2020
As you are using i+1,i+2, i+3....when you reach i = length(x)..the array will try to access i+1 and you will get this error.
Editor
Editor le 12 Oct 2020
@KSSV Thank you for your response. However, upon using 'm=length(x)', I get a zero matrix. I do not know what else I can do.
KSSV
KSSV le 12 Oct 2020
Share your complete code.
Editor
Editor le 12 Oct 2020
Modifié(e) : Editor le 12 Oct 2020
@KSSV, Here is my code after making the change you suggested:
clear
h = 0.01; % time step
x(1)=1;
y(1)=1;
h = 0.01;
dxdt = @(x,y) y;
dydt = @(x,y) x-x^3;
m = length(x)-3;
for i=1:1:m
x(i+1) = x(i) + h*dxdt(x(i), y(i));
y(i+1) = y(i) + h*dydt(x(i), y(i));
end
% Data Matrices X
X = [x(i) y(i) x(i)^2 x(i)*y(i) x(i)^2*y(i) x(i)*y(i)^2 y(i)^2 y(i)^3 y(i)*x(i)^3 x(i)*y(i)^3 y(i)^3 x(i)^4 y(i)*x(i)^4 y(i)^4;
x(i+1) y(i+1) x(i+1)^2 x(i+1)*y(i+1) y(i+1)*x(i+1)^2 x(i+1)*y(i+1)^2 y(i+1)^3 y(i+1)*x(i+1)^3 x(i+1)*y(i+1)^3 y(i+1)^3 x(i+1)^4 y(i+1)*x(i+1)^4 y(i+1)*x(i+1)^4 y(i+1)^4;
x(i+2) y(i+2) x(i+2)^2 x(i+2)*y(i+2) y(i+2)*x(i+2)^2 x(i+2)*y(i+2)^2 y(i+2)^3 y(i+2)*x(i+2)^3 x(i+2)*y(i+2)^3 y(i+2)^3 x(i+2)^4 y(i+2)*x(i+2)^4 y(i+2)*x(i+2)^4 y(i+2)^4];
Editor
Editor le 13 Oct 2020
I still encounter an error. I need your help please.

Connectez-vous pour commenter.

 Réponse acceptée

KSSV
KSSV le 12 Oct 2020
Modifié(e) : KSSV le 14 Oct 2020
h = 0.01; % time step
dxdt = @(x,y) y;
dydt = @(x,y) x-x^3;
m = 100 ;
x = zeros(m,1) ;
y = zeros(m,1) ;
x(1)=1;
y(1)=1;
for i=1:1:m-1
x(i+1) = x(i) + h*dxdt(x(i), y(i));
y(i+1) = y(i) + h*dydt(x(i), y(i));
end
% Data Matrices X
X = [x y x.^2 x.*y x.^2.*y x.*y.^2 y.^2 y.^3 y.*x.^3 x.*y.^3 y.^3 x.^4 y.*x.^4 y.^4];

8 commentaires

Editor
Editor le 13 Oct 2020
Thank you very much @KSSV . The code resolves the error. However, from row 3 downwards, I get rows of zeros. What could be the issue? Is there a way in which I can do to resolve this?
KSSV
KSSV le 13 Oct 2020
I am not getting any zeros. Can you be more clear?
Editor
Editor le 13 Oct 2020
I mean when I increase the number of rows as follows:
h = 0.01; % time step
dxdt = @(x,y) y;
dydt = @(x,y) x-x.^3;
m = 100 ;
x = zeros(1,m);
y = zeros(1,m);
x(1)=1;
y(1)=1;
for i=1:m-5
x(i+1) = x(i) + h*dxdt(x(i), y(i));
y(i+1) = y(i) + h*dydt(x(i), y(i));
end
X = [x(i) y(i) x(i)^2 x(i)*y(i) x(i)^2*y(i) x(i)*y(i)^2 y(i)^2 y(i)^3 y(i)*x(i)^3 x(i)*y(i)^3 y(i)^3 x(i)^4 y(i)*x(i)^4 y(i)^4;
x(i+1) y(i+1) x(i+1)^2 x(i+1)*y(i+1) y(i+1)*x(i+1)^2 x(i+1)*y(i+1)^2 y(i+1)^3 y(i+1)*x(i+1)^3 x(i+1)*y(i+1)^3 y(i+1)^3 x(i+1)^4 y(i+1)*x(i+1)^4 y(i+1)*x(i+1)^4 y(i+1)^4;
x(i+2) y(i+2) x(i+2)^2 x(i+2)*y(i+2) y(i+2)*x(i+2)^2 x(i+2)*y(i+2)^2 y(i+2)^3 y(i+2)*x(i+2)^3 x(i+2)*y(i+2)^3 y(i+2)^3 x(i+2)^4 y(i+2)*x(i+2)^4 y(i+2)*x(i+2)^4 y(i+2)^4;
x(i+3) y(i+3) x(i+3)^2 x(i+3)*y(i+3) y(i+3)*x(i+3)^2 x(i+3)*y(i+3)^2 y(i+3)^3 y(i+3)*x(i+3)^3 x(i+3)*y(i+3)^3 y(i+3)^3 x(i+3)^4 y(i+3)*x(i+3)^4 y(i+3)*x(i+3)^4 y(i+3)^4;
x(i+4) y(i+4) x(i+4)^2 x(i+4)*y(i+4) y(i+4)*x(i+4)^2 x(i+4)*y(i+4)^2 y(i+4)^3 y(i+4)*x(i+4)^3 x(i+4)*y(i+4)^3 y(i+4)^3 x(i+4)^4 y(i+4)*x(i+4)^4 y(i+4)*x(i+4)^4 y(i+4)^4;
x(i+5) y(i+5) x(i+5)^2 x(i+5)*y(i+5) y(i+5)*x(i+5)^2 x(i+5)*y(i+5)^2 y(i+5)^3 y(i+5)*x(i+5)^3 x(i+5)*y(i+5)^3 y(i+5)^3 x(i+5)^4 y(i+5)*x(i+5)^4 y(i+5)*x(i+5)^4 y(i+5)^4]
I get the followwing:
X =
Columns 1 through 12
1.5475 -0.2579 2.3949 -0.3991 -0.6176 0.1029 0.0665 -0.0171 -0.9557 -0.0265 -0.0171 5.7355
1.5450 -0.2795 2.3869 -0.4318 -0.6671 0.1207 -0.0218 -1.0306 -0.0337 -0.0218 5.6974 -1.5922
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
Columns 13 through 14
-1.4790 0.0044
-1.5922 0.0061
0 0
0 0
0 0
0 0
KSSV
KSSV le 13 Oct 2020
For those last values, you have not fixed the value of i, so it will take i = m-5..what exactly you want to do with the last lines?
Editor
Editor le 13 Oct 2020
I want to form an mxn matrix (which is X in this case) with n-rows using [x(i) y(i) x(i)^2 x(i)*y(i) x(i)^2*y(i) ...] as my data. Rows should start as follows:
row 1: x(i)
row 2: x(i+1)
row 3: x(i+2)
.....
row n: x(i+n)
I need i to start, say, at index 1 or 3
Editor
Editor le 13 Oct 2020
I know your schedule is tight but I will appreciate if you can assist me fixing this problem above. Thank you in anticipation
KSSV
KSSV le 14 Oct 2020
Hey..I have edited the answer.....please check..
Editor
Editor le 14 Oct 2020
@KSSV it works perfectly! Thank you so much for your time. You've been so helpful and patient

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by