Store value of variable computed in ODE Solver at the specified time steps.
5 次查看(过去 30 天)
显示 更早的评论
I am simulating a system using the ode15s(..) solver. Code is like this:
tspan = 0:0.01:10;
options = odeset('RelTol',1e-7,'AbsTol',1e-10,'Refine',4);
[t,theta] = ode15s(@ode_simulation, tspan, theta_init, options);
function d_theta = ode_simulation(t,theta)
u = k*(p-d)/e;
d_theta = [theta(2) ; -m*c*[theta(2) theta(4)]'-m*g+m*u ; theta(4) ; -m*c*[theta(2) theta(4)]'-m*g+m*u];
end
The solver returns the results in t, theta variable at the time steps specified by tspan. I want to find a way to obtain the values of variable u that is being calculated inside the ode_simulation(..) function. I don't want to define a global array and keep inserting values cause it is computationally-wise and memory-wise terrible. So, after the simulation is exefuted I want to have the t,theta variables and in addition a vector u contaning the computed u values for the same time steps. How could I do that ?
0 个评论
采纳的回答
Star Strider
2020-11-19
There are not enough variables provided to run your code.
This is how I would do it:
tspan = 0:0.01:10;
options = odeset('RelTol',1e-7,'AbsTol',1e-10,'Refine',4);
[t,theta] = ode15s(@ode_simulation, tspan, theta_init,options);
function [d_theta,u] = ode_simulation(t,theta)
u = k*(p-d)/e;
d_theta = [theta(2) ; -m*c*[theta(2) theta(4)]'-m*g+m*u ; theta(4) ; -m*c*[theta(2) theta(4)]'-m*g+m*u];
end
for k = 1:numel(t)
[~,u(k)] = ode_simulation(t(k),theta(k,:));
end
figure
plot(t, theta, t, u)
grid
legend('\theta(t)', 'u(t)')
.
12 个评论
Star Strider
2020-11-20
Stephen Cobledick — Thank you!
Teo Protoulis — I have no idea what the problelm is or the reason you are not getting the desired result. I would check the magnitudes of the imaginary components. If they are vanishingly small (on the order of 1E-10 or less), they could simply be the result of floating-point calculation approximation errors, and not significant. In that event, you can safely ignore them, using the real() values of the vectors.
Also, note that one complex value calculated for a vector results in the entire vector being complex, even if the imaginary values for the other elements are 0.
更多回答(0 个)
另请参阅
类别
在 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!