Index exceeds the number of array elements (11)
    3 views (last 30 days)
  
       Show older comments
    
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 Comments
Accepted Answer
  KSSV
      
      
 on 12 Oct 2020
        
      Edited: KSSV
      
      
 on 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 Comments
More Answers (0)
See Also
Categories
				Find more on Numerical Integration and Differential Equations 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!
