MATLAB: 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.

Best 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.