MATLAB Output of ode45 for System of Differential Equations

  • Thread starter Thread starter marellasunny
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The output of the MATLAB function ode45 for a system of differential equations is structured as a time vector "t" and a matrix "x", where "x" has dimensions m by n, with "m" representing the number of time sample points and "n" the number of state variables. When plotting the results, the command "plot(t,x(:,i))" is used to visualize the i-th state variable against time. The confusion arises from the interpretation of "x" as a column vector, which can lead to errors if one attempts to access elements using "x(1)" instead of the correct syntax "x(:,1)". This behavior is consistent across both MATLAB and GNU Octave.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Familiarity with the ode45 function for solving differential equations
  • Knowledge of matrix dimensions and indexing in MATLAB
  • Basic concepts of state variables in dynamic systems
NEXT STEPS
  • Explore the MATLAB documentation for the ode45 function
  • Learn about matrix indexing in MATLAB, specifically for multi-dimensional arrays
  • Investigate plotting techniques in MATLAB for visualizing differential equation solutions
  • Study the differences between MATLAB and GNU Octave in handling matrix operations
USEFUL FOR

Students, engineers, and researchers working with MATLAB for solving and visualizing systems of differential equations, particularly those new to the ode45 function and matrix indexing.

marellasunny
Messages
245
Reaction score
3
My book explains the command x(:,1) as "x,all rows,first column".
Q.How does the output of ode45 for a system of differential equations look like?
But,when I solve a system of 2 first order differential equations(w.r.t time),I take x as a "column vector".
[x1
x2]
So,if I wanted to plot x(1) which is the first row of the column vector,I should write plot(t,x(1)) right? MATLAB gives an error.

MATLAB however accepts this ''plot(t,x(:,1))'',which literally means plotting all rows,1st column x versus t.

I don't get it.x has only 1 column and 2 rows.Where is the question of 1st and 2nd column in x?
 
Last edited:
Physics news on Phys.org
marellasunny said:
My book explains the command x(:,1) as "x,all rows,first column".
Q.How does the output of ode45 for a system of differential equations look like?
But,when I solve a system of 2 first order differential equations(w.r.t time),I take x as a "column vector".
[x1
x2]
So,if I wanted to plot x(1) which is the first row of the column vector,I should write plot(t,x(1)) right? MATLAB gives an error.

MATLAB however accepts this ''plot(t,x(:,1))'',which literally means plotting all rows,1st column x versus t.

I don't get it.x has only 1 column and 2 rows.Where is the question of 1st and 2nd column in x?

If the function "f" that you're integrating has "n" state variables, then [t,x]=ode45(@f,[ti,tf],x0) should return a time vector "t" length "m", and an m by n matrix "x". Here "m" is the number of time sample points used, and the i-th column of "x" is a vector length "m" containing the values of the i-th state variable at each time point.

So yes, "plot(t,x(:,i))" should plot the results for the i-th state variable versus time.

BTW. That's how it works in gnu-Octave anyway. I think MATLAB is the same.
 
Last edited:

Similar threads

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