Store value of variable computed in ODE Solver at the specified time steps.
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Teo Protoulis
am 19 Nov. 2020
Kommentiert: Teo Protoulis
am 20 Nov. 2020
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 Kommentare
Akzeptierte Antwort
Star Strider
am 19 Nov. 2020
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 Kommentare
Star Strider
am 20 Nov. 2020
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!