Solving Matlab Plot Problem: Fix Error in Exsimpode21

  • MATLAB
  • Thread starter bdave
  • Start date
  • Tags
    Matlab Plot
In summary: If you need only the first column, then replace the statement with z1=z without the need of the second column.In summary, the conversation is about a problem with accessing a specific column in a matrix and how to fix it. The code being discussed is trying to plot values for different variables over time using the ODE45 function. The issue is that the matrix being used only has one column, so trying to access a second column results in an error. The solution is to remove the statement trying to access the second column and use the first column instead.
  • #1
bdave
1
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
  • #2
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?
 

1. What is the most common error encountered when plotting in Matlab?

The most common error encountered when plotting in Matlab is the "Exsimpode21" error. This error occurs when the "exsimpode21" function is not found in the Matlab path or is not a valid function.

2. How can I fix the "Exsimpode21" error?

To fix the "Exsimpode21" error, you will need to add the "exsimpode21" function to your Matlab path. This can be done by using the "addpath" command or by adding the folder containing the function to your Matlab path.

3. What does the "Exsimpode21" function do?

The "Exsimpode21" function is a built-in Matlab function that solves a system of ordinary differential equations using the explicit Runge-Kutta method. It is commonly used for solving mathematical models in science and engineering.

4. Can I use a different function instead of "Exsimpode21" for solving Matlab plots?

Yes, there are several other built-in Matlab functions that can be used for solving plots, such as "ode23" and "ode45". You can also create your own custom function for solving plots.

5. What are some troubleshooting steps if I continue to encounter errors when using "Exsimpode21" in Matlab?

If you continue to encounter errors when using "Exsimpode21" in Matlab, you can try updating your Matlab software to the latest version, checking for any updates or patches for the function itself, or seeking help from the Matlab community or technical support team.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
999
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
571
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
939
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top