How to output matlab ode dependent variable at same time step as T and Y?

5 views (last 30 days)
I am using ode15s to solve a coupled ode functions, like
[T Y]=ode15s(@ydot, [1:100],y0,options)
function ydot=odefunction(t,y)
dept1=f(y(1),y(2));
ydot(1)=dept1*y(1)*y(2)
ydot(2)=a*y(1)+c*y(2)
end
Now I have output file with T and Y from t=1 to t=100 with interval of 1. Right now I am using global variable and output "dept1" at every time step odesolver called, like t=[...7.7241 7.8241 8.6241 9.6854 10.524...]. Question is how could I output dependent variable like "dept1" from t=1 to t=100 with interval of 1?

Answers (2)

Richard Brown
Richard Brown on 2 Jul 2012
Edited: Richard Brown on 2 Jul 2012
By far the best way is to compute your dependent variable afterwards with the y matrix that was computed by the ODE solver. No messy code (or globals / persistents) required, and it probably won't add a huge amount (proportionally) to your computational cost.

Walter Roberson
Walter Roberson on 2 Jul 2012
Edited: Walter Roberson on 2 Jul 2012
You might still need to use a global variable, but you can get it to output only at the locations you specify in tspan.
Use odeset() to construct an options structure that includes an OutputFcn property. See http://www.mathworks.com/help/techdoc/ref/odeset.html#f92-1016858 for the parameters of the function that will be called.
I'm thinking: use the init flag to initialize a persistent variable to []; use the calls with no flags to append to the persistent; use the done flag to transfer data out of the persistent (possibly by setting a global.)
  2 Comments
Di
Di on 2 Jul 2012
Edited: Walter Roberson on 2 Jul 2012
Thanks for your quick answer.
However, I tried outputFcn and write a myoutput, if I put the commends for output global dependent variable in case of '[]', it output nothing, else if I put the commends under case 'done', it will output that variable at every integration fine time step. well, no expected output at tspn is shown.
Any more suggestions? Thanks
my self-defined output function is as below:
function status = myoutput (t,y,flag)
switch (flag)
case 'init'
statement;
case '[]'
statement for output;
case 'done'
statement for output;
end

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!