How to use one ode45 to solve another?

I have the following for my first ODE:
t0=0; tf=7;
y0=[0 0 0.332]';
[t,y]=ode45('ydot',[t0 tf],y0);
'ydot' is shown below:
function xdot = ydot(t,y)
xdot = zeros(3,1);
xdot(1) = y(2);
xdot(2) = y(3);
xdot(3) = -0.5*y(1)*y(3);
end
I now have a separate set of equations, where I need y from the initial solution.
function zdot = gdot(t,g)
zdot = zeros(2,1);
zdot(1) = g(2);
zdot(2) = -0.5*Pr*y(1)*g(2);
end
The second ode45 I need to solve is below.
t0=0; tf=7; Pr=0.6;
%y0=[0 0 0.332]';
%[t,y]=ode45('ydot',[t0 tf],y0);
g0=[0 0.332*Pr^(1/3)]';
[t,g]=ode45('gdot',[t0 tf],g0);
Any help is greatly appreciated.

 Risposta accettata

David Goodmanson
David Goodmanson il 29 Apr 2020
Modificato: David Goodmanson il 29 Apr 2020
Hi Carlos,
you can toss them all in together, with g(1) = y(4) and g(2) = y(5). That way y(1) is available in context.
t0=0; tf=7;
Pr = 0.6;
y0=[0 0 0.332 0 0.332*Pr^(1/3)]';
[t,y]=ode45(@ydotcalc,[t0 tf],y0);
fig(1)
plot(t,y);grid on
function ydot = ydotcalc(t,y)
Pr = 0.6;
ydot = zeros(5,1);
ydot(1) = y(2);
ydot(2) = y(3);
ydot(3) = -0.5*y(1)*y(3);
ydot(4) = y(5);
ydot(5) = -0.5*Pr*y(1)*y(5);
end

2 Commenti

This worked perfect. Thanks so much, David!
Maybe you could help with the rest of the solution. I now wish to plot T(y) with the following info:
U_inf = 1; %Free stream velocity
T_s = 300; %Surface temperature Celsius
T_inf = 27; %Ambient temperature
v = 30E-6; %m^2/s
x = 0.5; %distance (m)
Re_x = U_inf*x/v; %Reynolds
T(y)=(((U_inf*v*x)^(1/2)*y(4))-T_s)/(T_inf-T_s);
figure(2)
plot(T,y);
But I'm getting an error. Can you help? Thanks!
Hi Carlos,
it looks like you might be intending
T = (((U_inf*v*x)^(1/2)*y(:,4))-T_s)/(T_inf-T_s);
figure(2)
plot(T,y(:,4));
but it's not so clear. What is the underlying equation here?

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by