Store value of variable computed in ODE Solver at the specified time steps.

7 ビュー (過去 30 日間)
Teo Protoulis
Teo Protoulis 2020 年 11 月 19 日
コメント済み: Teo Protoulis 2020 年 11 月 20 日
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 ?

採用された回答

Star Strider
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
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.
Teo Protoulis
Teo Protoulis 2020 年 11 月 20 日
I have checked these conditions. There are more than one complex values and their magnitude can't be ignored. Anyway, I will keep checking. Thank you very much for your time and the useful information !

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by