Index exceeds the number of array elements (11)
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
KSSV
am 12 Okt. 2020
Bearbeitet: KSSV
am 14 Okt. 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!