Help solving differential equations

Hello,
I need helping a differential equation using ode45().
I have the following statements and need to find tmax:
A(t) = dA/dt = -k0*A
A(0) = A0
B(t) = dB/dt = k0*A - k1*B
B(0) = 0
E(t) = dE/dt = k1*B
E(0) = 0
k0 = 0.01
k1 = 0.035

2 comentarios

Stephan
Stephan el 6 de Mayo de 2022
Please provide the code you have so far.
Juan Lara Alcaraz
Juan Lara Alcaraz el 6 de Mayo de 2022
I was thinking of using a fuction like this function
dAdt = odefun(A,B,k0,k1)
dAdt = zeros(2,1);
dAdt = -k0*A;
dBdt = k0*A-k1*B;
dEdt=k1*B
end
It's all the code I have

Iniciar sesión para comentar.

 Respuesta aceptada

Sam Chak
Sam Chak el 7 de Mayo de 2022
Since you have written the code for the differential equations,
function dydt = odefcn(t, y)
dydt = zeros(3,1);
k0 = 0.01;
k1 = 0.035;
A = y(1);
B = y(2);
E = y(3);
dydt(1) = -k0*A;
dydt(2) = k0*A - k1*B;
dydt(3) = k1*B;
end
then you can use ode45 to obtain the numerical solution:
tspan = 0:0.1:1e3; % simulation time
init = [1 0 0]; % assume initial values, A(0) = 1, B(0) = 1, E(0) = 0
[t, y] = ode45(@odefcn, tspan, init);
% For plotting purposes
% ---------------------
plot(t, y, 'linewidth', 1.5)
grid on
xlabel('Time, t [sec]')
title('Time responses of the System')
legend({'$A(t)$', '$B(t)$', '$E(t)$'}, 'Interpreter', 'latex', 'location', 'best')
Result:

4 comentarios

Juan Lara Alcaraz
Juan Lara Alcaraz el 7 de Mayo de 2022
Thank you @Sam Chak. I don't understand why it isn't working, here I'll leave the screenshot of the error its giving me. Thank you again.
In one of your comments above, you have used odefcn() to define the system of differential equations.
But in your folder, you used myode. So, I guess this would solve the issue.
[t, y] = ode45(@myode, tspan, init);
Hope it helps.
Juan Lara Alcaraz
Juan Lara Alcaraz el 7 de Mayo de 2022
@Sam Chak Thank you so much!!!! IT WORKS!!
Sam Chak
Sam Chak el 11 de Mayo de 2022
I hope that information in this link could help you. Always find a way...

Iniciar sesión para comentar.

Más respuestas (1)

Torsten
Torsten el 6 de Mayo de 2022
syms k0 k1 A0 A(t) B(t) E(t)
eqns = [diff(A,t)==-k0*A,diff(B,t)==k0*A-k1*B,diff(E,t)==k1*B];
conds = [A(0)==A0,B(0)==0,E(0)==0]
[Asol(t),Bsol(t),Esol(t)]=dsolve(eqns,conds)

2 comentarios

Juan Lara Alcaraz
Juan Lara Alcaraz el 7 de Mayo de 2022
Is it possible to do it without extra toolboxes?
Torsten
Torsten el 7 de Mayo de 2022
Yes. The equations are that easy that they can be solved using pencil and paper.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 6 de Mayo de 2022

Comentada:

el 11 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by