Getting data from inside ode15s solver

I am using ode15s to solve a differential equation in a combustion particle system. The problem that I have is that the ode_func that I am putting in ode15s has some data inside of it that I want to get outsie of the function. This data is not the output of the ode_func, so it's not saved as output. My question is how is the best way to get it out? Should I use a global variable inside the ode_func that updates it's value while it solves, then I can access it outside of the func? Or is it possible to add another output to the ode_func? I'm not sure how the ode15s would handle multiple outputs from the ode_func.

 Accepted Answer

Do not use global variables!
Write one version of your ODE function that ode45 (or whatever solver you are using) then solves.
Then in a for loop, use those independent and dependent variable arrays with a second version of your ODE function that uses the solved values for the independent and dependent varialbles and instead returns the varialbles you are interested in in each iteration of the loop.
That has worked for me in the past, and it is likely more efficient than any other option.

4 Comments

I understand that general practice is to avoid global variables, but is there any intrinsic reason to avoid the global varables? For example, if I use a global variable, will the value that the ode solver saves in the final timestep evaluation be incorrect? I know that the ode solver needs to evaluate the numerical jacobian for the first segment of iterations, but after that, is everything the actual numerical solution?
As a side note, I hate global variables, however, the bozos that built up this code that has thousands of lines in it used global variables willy-nilly, so I'm not going to go back and change all these so I figure that using just 1 more global variable isn't too bad from where I'm already at.
Considering that you have little control over the code, your options are limited. I still believe that using the integrated variables and independent variable to output the values you want at each time step using a slightly modified version of the ODE function to do that is the optimal approach.
I actually have no idea what results you want. The global variable approach will likely give you the other variables only at the end of the integration.
There was a time about 25 years ago when the only way to pass extra parameters to an ODE function was to use global variables. That has since been supplanted by the approach in Passing Extra Parameters. However for what you want to do, creating a copy of the ODE function that will output the variables you want at each time step (if that is what you want) is still likely the best option. If you only need the results at the end of the integration, the global variables approach will work.
Ok. Last question: what was the point of the doing your suggestion in a for-loop? Are you saying that I should iterate over the timesteps that the ode solver output, then use the output variable as an input to my altered odefunc to calculate the things I want?
The for loop is essentially the only way to get the step-by-step data for the other variables, because of the way ODE functions are written. (They are intended to be evaluated step-wise.) At each iteration of the for loop, calculate the desired variables with the specific values of the integrated independent and dependent variables and return the other variables as an output of the slightly revised ODE function.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!