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?
0 个评论
回答(2 个)
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.
0 个评论
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!