Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

X(:,1) command in matlab

  1. Nov 6, 2012 #1
    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: Nov 7, 2012
  2. jcsd
  3. Nov 7, 2012 #2

    uart

    User Avatar
    Science Advisor

    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: Nov 7, 2012
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: X(:,1) command in matlab
Loading...