Output additional vectors from ode45
64 次查看(过去 30 天)
显示 更早的评论
I am calculating concentrations and radii at all the timesteps within the ode45 loop. What is the best way to output all that information?
采纳的回答
Star Strider
2021-11-28
Adding additional outputs is permitted, providing that they are not used in the ode45 call. Normally, this is not a problem, because unless more outputs are requested in the calling script or function, only the first output is used.
The only way to recover them is to use the returned independent variable (usually time) vector and the solved dependent variables in a for loop and save the other desired output at each step of the loop.
tspan = [0 10];
ic = [0.1; -0.1];
[t,y] = ode45(@odefcn, tspan, ic);
for k = 1:numel(t)
[~,radii(k,:)] = odefcn(t(k),y(k,:));
end
figure
yyaxis left
plot(t,y)
ylabel('y(t)')
yyaxis right
plot(t, radii)
ylabel('Radius')
grid
function [dy,radius] = odefcn(t,y)
dy = zeros(2,1);
radius = hypot(y(1),y(2));
dy(1) = y(1);
dy(2) = radius * cos(y(1)) * sin(y(2));
end
.
更多回答(1 个)
另请参阅
类别
在 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!