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

Stoping a specific matlab function

  1. Sep 21, 2010 #1
    Hi all,

    I'm having some issues with Matlab.

    I'm running ode45 inside a function which is called repeatedly in a finite loop. As it is being called, Matlab is plotting and saving the data externally.

    Here's a quick pseudo-code
    Code (Text):

    func1 ( )
      loop {
       data = func2 (variables)
       save data
       plot data
      }
    end func1

    func2 ( input vars)
      --
      --
      options = odeset('events',@missile_event, 'RelTol',1e-14);
      [t, x, te, xe, ie] = ode45 ( @de, [t0 tf], x0, options );
      --
      --
      plot x vs t etc..
     --
     --
    return data;
    end
     
    This is all well and good, but as soon as ode45 is unable to solve a DE (were the solution does not converge), Matlab gets stuck in an (almost?) infinite loop repeatedly calling ode45 until a solution is found. I can obviously stop this using "Ctrl-C" but this will screw up the data I'm trying to gather and plot.

    Is there a way to get ode45 to return some value if it takes more than x seconds to find a solution? Or perhaps get it to stop executing func2 and resume func1 (see above code).

    I am already using the events function to stop ode45 as soon as it has converged close to the solution.

    i.e,
    Code (Text):

    function [lookfor stop direction ] = missile_event(t, x)
    r = sqrt(x(1)^2 + x(2)^2) ;
    lookfor = r - 0.1;
    stop = 1;
    direction = 0;
     
    Thanks in advance.
     
  2. jcsd
  3. Sep 21, 2010 #2

    Pythagorean

    User Avatar
    Gold Member

    Well... what you can do is plot chunks of time series at at a time (i.e. a smaller [to tf], then stop, check the result, put the result back in as the initial condition for a short chunk of time.

    Then you can somehow analyze the trajectory of your results to see if its diverging.

    Unfortunately (and I've tested this with ode113) an ODE solved by doing the whole [t0 tf] comes out different than if you to partials of [t0 tf], using the result from each 'chunk' as the IC for the next 'chunk'. So that means some kind of window analysis is going on (or something).

    I'd like to hear a better solution, personally
     
  4. Sep 23, 2010 #3
    Hi Pythagorean,

    I found a simple solution that works for me.

    Inside func2 and just before line "[t, x, te, xe, ie] = ode45 ( @de, [t0 tf], x0, options );" i record the internal CPU time.

    Inside function 'de', i check the new CPU time against what was recorded initially, and if 'x' seconds have passed, the solution is not going to converge or will take too long. If that occurs, I change the parameters of de such that it calls the event function and terminates ODE45 (in other words, ODE "thinks" it found a solution and stops).

    Here's an example code:

    Code (Text):

    func2 ( input vars)
      --
      --
      options = odeset('events',@missile_event, 'RelTol',1e-14);
      initial_time = cputime;
      [t, x, te, xe, ie] = ode45 ( @de, [t0 tf], x0, options );
     
      if (status == -1)
         problem occured etc..
         return error value;
      end
      otherwise
      --
      plot x vs t etc..
      --
      return data;
    end

    function d_x = de(t, x) {        //this is the function ode45 calls
       --
       --
       if (cputime - initial_time > 15 )  then //i.e 15 seconds have passed
            d_x(1) = 0
            d_x(2) = 0
            etc....
            status = -1;
       end
       --
       --
    }
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Stoping a specific matlab function
  1. MATLAB function (Replies: 2)

  2. Matlab function (Replies: 1)

  3. Matlab functions (Replies: 0)

  4. Matlab functions (Replies: 0)

  5. MATLAB function (Replies: 1)

Loading...