Solving Matlab Plot Problem: Fix Error in Exsimpode21

  • Context: MATLAB 
  • Thread starter Thread starter bdave
  • Start date Start date
  • Tags Tags
    Matlab Plot
Click For Summary
SUMMARY

The forum discussion addresses a MATLAB error encountered while attempting to access the second column of a variable 'z' that only contains one column. The user is executing the script 'Exode21.m', which utilizes the 'ode45' function to solve ordinary differential equations defined in multiple scripts (simpode2120.m, simpode2140.m, etc.). The error arises specifically at the line where the user tries to assign 'z1' from 'z(:,2)', leading to an "index out of bounds" error. The solution is to ensure that the variable being accessed has the required dimensions before attempting to index into it.

PREREQUISITES
  • Understanding of MATLAB syntax and functions, particularly 'ode45'
  • Familiarity with ordinary differential equations (ODEs)
  • Knowledge of matrix indexing in MATLAB
  • Experience with MATLAB plotting functions
NEXT STEPS
  • Review MATLAB documentation on the 'ode45' function for solving ODEs
  • Learn about MATLAB matrix dimensions and indexing to avoid similar errors
  • Explore MATLAB plotting techniques to visualize ODE solutions effectively
  • Investigate debugging techniques in MATLAB to handle runtime errors
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly those working with numerical methods for solving ordinary differential equations, as well as developers looking to enhance their debugging skills in MATLAB.

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

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K