Effacer les filtres
Effacer les filtres

Index exceeds the number of array elements (11)

2 vues (au cours des 30 derniers jours)
Editor
Editor le 12 Oct 2020
Commenté : Editor le 14 Oct 2020
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
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
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)

Community Treasure Hunt

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

Start Hunting!

Translated by