Index exceeds the number of array elements (11)
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
KSSV
2020-10-12
编辑:KSSV
2020-10-14
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!