Output of ode45 for System of Differential Equations

  • Topic: MATLAB 
  • Thread starter Thread starter marellasunny
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
1 reply · 3K views
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: