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

How to follow a function's value in ODE45 Matlab?

  1. Jun 18, 2007 #1
    Hi everyone,
    It is extremely helpful for me if you could answer the following question. I hope it is a relatively simple problem:

    I just need a plot or an output data file of the values of the function 'm=2/y(1)' appearing in the function 'myfun' for each time-step. This is extremely easy in other programming languanges but in Matlab when I plot(t,m) it only returns the value at the end of the integration.

    function manoura;
    tspan = [0 0.8];
    y0 = [3; 7/3];

    [T, Y] = ode45(@myfun, tspan, y0);
    subplot(211); plot(T,Y(:,1));
    subplot(212); plot(T,Y(:,2));

    function F1 = myfun(t,y);
    F1 = [y(2); m*y(2) - 4*y(1) + (t+2)*exp(-2*t)];

    Thank you in advance!
    I have tried everywhere and I cannot find a solution.
    Thank you in advance!!!

    (I have already posted it as "How to follow a function's value in ODE45 Matlab?")
  2. jcsd
  3. Jun 18, 2007 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Use an array of points for tspan rather than just the start and end points. For example, tspan = 0:0.05:0.8;
  4. Jun 19, 2007 #3
    I have tried an array but it does not still show the values for (m) at each time step. Since my actual code is more complicated and it runs for about more than 1000secs I guess there must be also another solution to that.

    Perhaps it needs "odeset" or "handle" functions, I do not really know!

    Thanx anyway!

    My code now using the array for tspan has became:
    function manoura;
    tspan = 0:0.2:0.3:0.4:0.6:0.8:0.9;
    y0 = [3; 7/3];

    [T, Y] = ode45(@myfun, tspan, y0);

    function F1 = myfun(t,y);
    F1 = [y(2); m*y(2) - 4*y(1) + (t+2)*exp(-2*t)];
  5. Jun 19, 2007 #4
    Why dont you create a data file?

    Google for fopen, fclose, and fprint commands. With those you can create a data archive containig all numerical calculations.

    If you write
    Code (Text):
    Matlab will create an archive and asign it the value x. The 'w' stands for write.

    Then with fprint, Matlab will save the data in the archive (browse the help for specific commands)
    Code (Text):
    fprint(x,'%f  %f \n', var1,var2)
    where var1, var2 are the variables you want to save. The %f %f \n tells matlab the way you want the archive to be formatted (again, check the help).

    Finally when you are done, type
    Code (Text):
    to close the archive.

    If you latter on whant to call the data archive, simply use the code
    Code (Text):
    [v1,v2]=textread('nameofarchive.dat','%f %f',m)
    and then you can use v1 and v2 for further calculations. Here, m is the number of columns of your archive (you can modify it to read it partially I believe).

    It's a great idea to create data archives instead of carrying variables in order to free up memory for Matlab, as it turns out to be quite stupid in allocating memory blocks by itself. This will improve considerably the calculation time of your code. Also remeber to clear as many variables as you can when no longer need them, for example, after you finish writting your archive, clear var1, var2 with the line
    Code (Text):
    Anyway, you can find all sorts of neat tricks in the web, just look for them.
    Last edited: Jun 19, 2007
  6. Jun 19, 2007 #5
    Thanx, I have included "fprinf" commands and etc... but still no luck!

    I can write an output file for all except the one I want, "m" :(
    I guess it calculates "m" time series internally whithin the ode45 and keeps no records for output.
    When I include the "fprinf" and so in function "manoura", it does not recognize "m".
    On the other hand when I include the "fprinf" and so in function "myfun", it calculates again only the last value of t.

    My file is shown below:
    function manoura
    tspan = [0 0.8];
    y0 = [3; 7/3];

    [T, Y] = ode45(@myfun,tspan, y0);

    fid=fopen('mvalues.txt', 'w+');
    for c = 1:size(T)
    fprintf(fid, '%5f %5f %5f\n', [T(c); Y(c,2); Y(c,1)]);

    function F1 = myfun(t,y)
    F1 = [y(2); m*y(2) - 4*y(1) + (t+2)*exp(-2*t)];
  7. Jun 20, 2007 #6
    I believe is in the way you are asking the program to write the file. When Matlab performs ode45, it generates an array of points, and I think what you are doing is asking it to create the file by steps. I am not sure about this, better go to a Matlab forum and ask there.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook