Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
saving internal values from ode45 solver

Subject: saving internal values from ode45 solver

From: Luke

Date: 17 Apr, 2009 19:07:01

Message: 1 of 8

Okay, I have a problem I can't figure out. I'm solving a set of ODE's with ODE 45 for a system of springs/masses. The force on each mass varies at each time step based on the position of the masses in a non-trivial way. The function called by ode45 first calculates the forces at each time step, then solves for positions. The solution is fine, but how can I store/access the force values for each time step? I've tried using a global variable, but the resultant vector is too long (683 force values, only 430 positions in the output). I'm guessing this has to do with the ode solver refining the time steps and throwing away solutions when it does this, whereas the global force variable saves all the solutions. But how can I get just the forces at each of the time steps output at the end of the ode45 routine? I don't think it's as simple as adding the force to the dy output vector, since then
Matlab will try to integrate the force, right?

Subject: saving internal values from ode45 solver

From: KurtCav

Date: 29 Apr, 2009 08:01:09

Message: 2 of 8

I've been trying to solve this same problem. I have a Runge-Kutta
integrator I wrote for a previous assignment. I will just used that,
declare my own step size, and save my variable of interest to a global
variable.

Subject: saving internal values from ode45 solver

From: Sean Torrez

Date: 10 Jun, 2009 15:06:02

Message: 3 of 8

I have also been working on this problem. Has anyone found a good way to do it? Seems like they ought to just add this functionality to the ODE solvers, since it's pretty common to want to get non-derivative information (or the values of the derivatives!) from the internals of the ODE routines. I think there may be a way to do it with the 'OutputFcn' option, but in my case, that will me recomputing several values from scratch which are already available to me from the original derivative function.

So, I think there ought to be a good way to do this, but if there is I haven't been able to find it. I know of several not so great ways to do it. They work, but they're slow and cumbersome. One is to write to a file, another is to use globals, and the third is to use the OutputFcn, which is relatively fast, but as I said, it's redundant because I already know the values I need.

KurtCav <Kurt.Cavalieri@gmail.com> wrote in message <88e04beb-58bb-46ec-b22f-5582deeafe44@b7g2000pre.googlegroups.com>...
> I've been trying to solve this same problem. I have a Runge-Kutta
> integrator I wrote for a previous assignment. I will just used that,
> declare my own step size, and save my variable of interest to a global
> variable.

Subject: saving internal values from ode45 solver

From: Sean Torrez

Date: 11 Jun, 2009 13:28:03

Message: 4 of 8

Figured it out. Treat the problem as a differential-algebraic equation. There is some stuff on this in the help (not enough, as usual), enough to figure it out.

"Sean Torrez" <eltorrisimo@gmail.com> wrote in message <h0oi4q$2cv$1@fred.mathworks.com>...
> I have also been working on this problem. Has anyone found a good way to do it? Seems like they ought to just add this functionality to the ODE solvers, since it's pretty common to want to get non-derivative information (or the values of the derivatives!) from the internals of the ODE routines. I think there may be a way to do it with the 'OutputFcn' option, but in my case, that will me recomputing several values from scratch which are already available to me from the original derivative function.
>
> So, I think there ought to be a good way to do this, but if there is I haven't been able to find it. I know of several not so great ways to do it. They work, but they're slow and cumbersome. One is to write to a file, another is to use globals, and the third is to use the OutputFcn, which is relatively fast, but as I said, it's redundant because I already know the values I need.
>
> KurtCav <Kurt.Cavalieri@gmail.com> wrote in message <88e04beb-58bb-46ec-b22f-5582deeafe44@b7g2000pre.googlegroups.com>...
> > I've been trying to solve this same problem. I have a Runge-Kutta
> > integrator I wrote for a previous assignment. I will just used that,
> > declare my own step size, and save my variable of interest to a global
> > variable.

Subject: saving internal values from ode45 solver

From: Richard Crozier

Date: 11 Jan, 2010 11:19:03

Message: 5 of 8

"Sean Torrez" <eltorrisimo@gmail.com> wrote in message <h0r0p3$qc0$1@fred.mathworks.com>...
> Figured it out. Treat the problem as a differential-algebraic equation. There is some stuff on this in the help (not enough, as usual), enough to figure it out.
>


Can someone from TMW give an example of doing this, just to get those internally computed values please? It would be very helpful for me and probably others it seems.

Subject: saving internal values from ode45 solver

From: Richard Crozier

Date: 11 Jan, 2010 13:07:04

Message: 6 of 8

"Richard Crozier" <r.crozier@ed.ac.uk> wrote in message <hif1f7$ns2$1@fred.mathworks.com>...
> "Sean Torrez" <eltorrisimo@gmail.com> wrote in message <h0r0p3$qc0$1@fred.mathworks.com>...
> > Figured it out. Treat the problem as a differential-algebraic equation. There is some stuff on this in the help (not enough, as usual), enough to figure it out.
> >
>
>
> Can someone from TMW give an example of doing this, just to get those internally computed values please? It would be very helpful for me and probably others it seems.

Never mind, there's an example here:

http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/246590

Subject: saving internal values from ode45 solver

From: Andres Mauricio Gonzalez

Date: 14 Mar, 2012 16:22:12

Message: 7 of 8

This might be inelegant, but works:

First add some lines to your ode function so you retrieve and save a file with one variable:
-------------------
function ds = EXAMPLEODE(t,s)
ds1=First Differential Equation
ds2=Second Differential Equation
insidevariables=[t ds1 ds2]
load('example.mat')
insidevariables=[insidevariables; insidevariables];
save('example', 'insidevariables')
ds=[ds1;ds2];
------------------


And then, to call it you would do something like this:
insidevariables=[]; %empty array
save('example.mat', 'insidevariables'); %saves array to file
[t,s] = ode45(@EXAMPLEODE,[0 10],[0;0],[],); %call the ode solver
load('example.mat'); %loads the variable inside the file
insidevariables=sortrows(insidevariables); %sorts the array in ascending order according %to time, because comes unsorted from the solver



Andres
thehappyengineer.blogspot.com
 

Subject: saving internal values from ode45 solver

From: Richard Crozier

Date: 15 Mar, 2012 09:03:24

Message: 8 of 8

"Andres Mauricio Gonzalez" <biomedicoandres@gmail.com> wrote in message <jjqgjk$mf$1@newscl01ah.mathworks.com>...
> This might be inelegant, but works:
>
> First add some lines to your ode function so you retrieve and save a file with one variable:
> -------------------
> function ds = EXAMPLEODE(t,s)
> ds1=First Differential Equation
> ds2=Second Differential Equation
> insidevariables=[t ds1 ds2]
> load('example.mat')
> insidevariables=[insidevariables; insidevariables];
> save('example', 'insidevariables')
> ds=[ds1;ds2];
> ------------------
>
>
> And then, to call it you would do something like this:
> insidevariables=[]; %empty array
> save('example.mat', 'insidevariables'); %saves array to file
> [t,s] = ode45(@EXAMPLEODE,[0 10],[0;0],[],); %call the ode solver
> load('example.mat'); %loads the variable inside the file
> insidevariables=sortrows(insidevariables); %sorts the array in ascending order according %to time, because comes unsorted from the solver
>
>
>
> Andres
> thehappyengineer.blogspot.com
>

Hi Adres, I appreciate the suggestion, but if only it were this easy!

First of all, your method assumed the ode solution function is called only one at each time step you see in the final output. This is not the case, the solver may try several calls to achieve the desired accuracy in the result, in your solution all of these test calls would also be saved the file, which are not in fact part of the final solution.

There is also a more general problem with your solution, the point of getting the variables directly is to save the time of recomputing the internally calculated variables. I haven't tried your solution, but I'm guessing that doing file IO on every call to the solution function during the ODE progress will often be much slower than simply calling it again with the final set of time steps (this is also related to my point above, the ode solution function is not only called at the time steps you see in the output). In addition, this will also require reallocating memory, in what could be a very large array as the solution progresses, also possibly very time consuming.

On another thread, a possible solution has been proposed using the mass matrix that can be specified to extract the results, but I don't have the link to hand.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us