Store value of variable computed in ODE Solver at the specified time steps.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Teo Protoulis
el 19 de Nov. de 2020
Comentada: Teo Protoulis
el 20 de Nov. de 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 comentarios
Respuesta aceptada
Star Strider
el 19 de Nov. de 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 comentarios
Star Strider
el 20 de Nov. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Ordinary Differential Equations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!