Stoping a specific matlab function

In summary: I then use a for loop to plot the data chunk by chunk.In summary, this solution allows me to stop ODE45 if it takes too long to find a solution, and keeps the data collected so far accurate.
  • #1
nand_1
16
0
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:
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:
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.
 
Physics news on Phys.org
  • #2
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
 
  • #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:
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 occurred 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
   --
   --
}
 

Related to Stoping a specific matlab function

1. How do I stop a specific function in Matlab?

To stop a specific function in Matlab, you can use the "return" command within the function. This will immediately exit the function and return control to the calling function.

2. Can I use a keyboard shortcut to stop a function in Matlab?

Yes, you can use the "Ctrl+C" keyboard shortcut to stop a function in Matlab. This will interrupt the execution of the function and bring you back to the command prompt.

3. Is there a way to pause a function in Matlab and then resume it?

Yes, you can use the "keyboard" command to pause a function in Matlab and enter debug mode. From there, you can use the "dbcont" command to resume the function execution.

4. Can I stop a function from running in a specific iteration in a loop?

Yes, you can use the "break" statement within the loop to stop the function from running in a specific iteration. This will exit the loop and continue with the rest of the code.

5. What if my function is stuck in an infinite loop, how can I stop it?

If your function is stuck in an infinite loop, you can use the "Ctrl+C" keyboard shortcut to interrupt the execution of the function and return to the command prompt. From there, you can debug and fix the issue causing the infinite loop.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
897
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Advanced Physics Homework Help
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
3K
Back
Top