X(:,1) command in matlab

  • #1
255
3

Main Question or Discussion Point

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 dont get it.x has only 1 column and 2 rows.Where is the question of 1st and 2nd column in x?
 
Last edited:

Answers and Replies

  • #2
uart
Science Advisor
2,776
9
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 dont 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:

Related Threads on X(:,1) command in matlab

Replies
4
Views
265
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
4
Views
24K
Replies
4
Views
13K
  • Last Post
Replies
1
Views
586
  • Last Post
Replies
11
Views
1K
Replies
1
Views
497
Replies
1
Views
16K
Replies
1
Views
611
Replies
4
Views
789
Top