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

Matlab Plot

  1. Jul 13, 2011 #1
    Hey i need help in ODE45...i'm new to PF...pls help me

    i have the following code

    >>for i=1:5
    >>tspan=[0 30]
    >>[t,h]=ode45['a ODEfunction',tspan,h0]

    in this loop how can i save 5 values of [t,h],
    as t, h produces each loop run like follows

    also i need to plot all of them n i could view the 5 graphs them..pls help..
  2. jcsd
  3. Jul 13, 2011 #2


    User Avatar
    Gold Member

    You can add a line in the loop, after [t,h] = ode...

    that goes:

    ts(i,:) = t;
    hs(i,:) = h;

    but you might want to preallocate those vectors before the loop:

    ts = zeros(5,n);
    hs = zeros(5,n);

    where n is the number of points in each set.

    The way you're plotting now, you wipe out your last plot every time. You can either add a "hold on" command that keeps the old data up when it plots the new data, or you can plot the whole set at once (hs).

    also, ts, might won't be different every time as is, so you would have plot(t,hs) at the end OUTSIDE the for loop and it will plot all five cases at once, each a different color
  4. Jul 14, 2011 #3
    Thanks for the replies..

    @ Pythagoran..I tried the same way...But another problem arises

    As in each go through the loop the length(t) & length(h) varies in the for loop.
    Hence the command t(i,:) produces one result but then shows error as :

    'Subscripted assignment dimension mismatch'...What to do..pls help me...?
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - Matlab Plot Date
Matlab Plotting Coordinate Transformations in Matlab Feb 12, 2018
Matlab Plotting animations (time-evolution) Jan 17, 2018
Does anyone had this plotting problem with MATLAB? Dec 23, 2017
Matlab Matlab's plot command Nov 30, 2017
Matlab Matlab -- how to make a smooth contour plot? Jun 28, 2017