MATLAB Solving Matlab Plot Problem: Fix Error in Exsimpode21

  • Thread starter Thread starter bdave
  • Start date Start date
  • Tags Tags
    Matlab Plot
AI Thread Summary
The discussion revolves around a MATLAB program designed to solve a system of ordinary differential equations (ODEs) using the `ode45` function. The user encounters an error when attempting to access the second column of the output matrix `z`, which only has one column, resulting in an "index out of bounds" error. This issue arises because the user is trying to extract values from a matrix that does not contain the expected number of columns. The solution involves ensuring that the program correctly handles the output of `ode45`, which returns a matrix with one column if only one variable is being solved. To fix the error, the user should check the dimensions of the output matrices and adjust their code accordingly to avoid accessing non-existent columns.
bdave
Messages
1
Reaction score
0
Hello everyone, I'm having a problem.

This is my program:

Script simpode2120.m:
function zdot=simpode2120(t,z)
zdot=[z(2);(20/100)*(50-10*z(2)-30*(z(1)-z(3)));z(4);(1/25)*(-245.2*z(4)-30*(z(1)-z(3))-50*z(3))];
end

Script simpode2140.m:
function wdot=simpode2140(t,w)
wdot=[w(2);(40/100)*(50-10*w(2)-30*(w(1)-w(3)));w(4);(1/25)*(-245.2*w(4)-30*(w(1)-w(3))-50*w(3))];
end

Script simpode2160.m:
function xdot=simpode2160(t,x)
xdot=[x(2);(60/100)*(50-10*x(2)-30*(x(1)-x(3)));x(4);(1/25)*(-245.2*x(4)-30*(x(1)-x(3))-50*x(3))];
end


Script simpode2180.m:
function sdot=simpode2180(t,s)
sdot=[s(2);(80/100)*(50-10*s(2)-30*(s(1)-s(3)));s(4);(1/25)*(-245.2*s(4)-30*(s(1)-s(3))-50*s(3))];
end


Script simpode21100.m:
function bdot=simpode2180(t,b)
bdot=[b(2);(100/100)*(50-10*b(2)-30*(b(1)-b(3)));b(4);(1/25)*(-245.2*b(4)-30*(b(1)-b(3))-50*b(3))];
end














Script Exode21.m:

clear
clc
close all



% Solving ODE:
[t,z]=ode45('simpode2120',[0 100],[0 0 0 0]);
[t,w]=ode45('simpode2140',[0 100],[0 0 0 0]);
[t,x]=ode45('simpode2160',[0 100],[0 0 0 0]);
[t,s]=ode45('simpode2180',[0 100],[0 0 0 0]);
[t,b]=ode45('simpode21100',[0 100],[0 0 0 0]);

% Obtaining x1:
z=z(:,1);
w=w(:,1);
x=x(:,1);
s=s(:,1);
b=b(:,1);

% Obtaining x'1:
z1=z(:,2);
w1=w(:,2);
x1=x(:,2);
s1=s(:,2);
b1=b(:,2);

% Obtaining x2:
z2=z(:,3);
w2=w(:,3);
x2=x(:,3);
s2=s(:,3);
b2=b(:,3);

% Obtaining x'2:
z3=z(:,4);
w3=w(:,4);
x3=x(:,4);
s3=s(:,4);
b3=b(:,4);



% Plot Results:
subplot(2,1,1),plot(t,z, t,w, t,x, t,s, t,b);
grid on
xlabel('Time (s)')
ylabel('x')
title('Values for x1')
legend('x20 ','x40','x60','x80','x100');

subplot(2,1,2),plot(t,z1, t,w1, t,x1, t,s1, t,b1);
grid on
xlabel('Time (s)')
ylabel('x')
title('Values for x1dot')
legend('x20 ','x40','x60','x80','x100');

subplot(2,2,1),plot(t,z2, t,w2, t,x2, t,s2, t,b2);
grid on
xlabel('Time (s)')
ylabel('x')
title('Values for x2')
legend('x20 ','x40','x60','x80','x100');

subplot(2,2,1),plot(t,z3, t,w3, t,x3, t,s, t,b);
grid on
xlabel('Time (s)')
ylabel('x')
title('Values for x2dot')
legend('x20 ','x40','x60','x80','x100');



I keep getting an error of:
? Attempted to access z(:,2); index out of bounds because size(z)=[1217,1].

Error in ==> Exsimpode21 at 23
z1=z(:,2);

>>

If I just plot that one part, it does it fine. Some of the other variables also bring about the same problem.
How do I fix this?
Thanks
 
Physics news on Phys.org
bdave said:
keep getting an error of:
? Attempted to access z(:,2); index out of bounds because size(z)=[1217,1].

Error in ==> Exsimpode21 at 23
z1=z(:,2);
The statement z(:,n) returns the nth column of the matrix. Your array has only one column, how can you attempt to access the second column?
 

Similar threads

Back
Top