How to store intermediate values of solution in ode45
Show older comments
I have the following ode dm/dt model. I want to extract intermediate values of t, m (the solution),u at specfic intermediate points that satisfies the condition rho_air == rho_1 and rho_air == rho_2 (It is the commented block in the following code).
My question is that how to extract these values V_star1 and V_star2 and store them so that I have access to them when I call the function in ode45? Especially how to store the solution to the ode m at that point of time? I tried adding them as an additional output to the function of myode but it did not work
Thanks
clc,clear,close all
global rho_fuel Ve Mcv Cd rho_air_0 A_r g a_max rho_1 rho_2
rho_1 = 1.0098;
rho_2 = 4.7317e-04;
a_max = 32.361945; % m/s^2
g = 9.8; % m/s^2
Cd = 0.47;
r = 3.7; % m
A_r = pi * r^2;
rho_fuel = 1000; % Kg/m^3
Ve = 3000; % m/s
rho_air_0 = 1.2754; % Kg/m^3
TurnPoint = 100000; % m
Mbody = 60684; % Kg
Mfuel = 488370; % Kg
Mpayload = 31184; % Kg
Mcv = Mbody + Mfuel + Mpayload;
tspan = [0 60];
m0 = Mfuel;
[t,m] = ode45(@(t,m) myode(t), tspan, m0);
plot(t,m,'LineWidth',2)
legend('M_e(t)','a(t)')
xlabel('time')
function [dmdt] = myode(t,m)
global rho_fuel Ve Mcv Cd rho_air_0 A_r g a_max rho_1 rho_2
a = dudt_const(t,a_max);
a_fun = @(tt) dudt_const(tt,a_max);
u = integral( a_fun , 0 , t);
h = u * t;
H=[-1;0;1;2;3;4;5;6;7;8;9;10;11;13;15;17;20;25;30;32;35;40;45;47;50;51;60;70;71;80;84.9;89.7;100.4;105;110;120];
H=H*1000;
rho=[1.347;1.225;1.1116;1.0065;0.9091;0.8191;0.7361;0.6597;0.5895;0.5252;0.4664;0.4127;0.3639;0.2655;0.1937;0.1423;0.0880;0.0395;0.0180;0.0132;0.0082;0.0039;0.0019;0.0014;0.0010;0.00086;0.000288;0.000074;0.000064;0.000015;0.000007;0.000003;0.0000005;0.0000002;0.0000001;0.0000001];
f_rho=fit(H, rho, 'linearinterp');
rho_air=f_rho(h);
% %%%%%%%%%%%%%%%%%
% if rho_air == rho_1
% t_star1 = t;
% m_star1 = m; % the solution of the ode at t = t_star1
% u_star1 = u;
% V_star1 = [t_star1,m_star1,u_star1];
% V_star1
% end
%
% if rho_air == rho_2
% t_star2 = t;
% m_star2 = m; % the solution of the ode at t = t_star2
% u_star2 = u;
% V_star2 = [t_star2,m_star2,u_star2];
% V_star2
% end
%
% %%%%%%%%%%%%%%%%%
Fd = 0.5 * rho_air * u^2 * A_r * Cd;
dmdt = (1/Ve) * (-Mcv * g - Fd - Mcv * a);
end
function a = dudt_const(t,a_max)
if t == 0
a = a_max;
end
if t > 0
a = a_max .* t./t;
end
end
1 Comment
Jan
on 7 Dec 2022
Note: myode need 2 input arguments. Then:
[t,m] = ode45(@(t,m) myode(t), tspan, m0);
% ^ 1 argument only
Better:
[t,m] = ode45(@(t,m) myode(t, m), tspan, m0);
% Or simply:
[t,m] = ode45(@myode, tspan, m0);
Accepted Answer
More Answers (0)
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!