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

3 次查看(过去 30 天)
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?

回答(2 个)

Richard Brown
Richard Brown 2012-7-2
编辑:Richard Brown 2012-7-2
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 2012-7-2
编辑:Walter Roberson 2012-7-2
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 个评论
Di
Di 2012-7-2
编辑:Walter Roberson 2012-7-2
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by