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 Kommentare

Stephan
Stephan am 6 Mai 2022
Please provide the code you have so far.
Juan Lara Alcaraz
Juan Lara Alcaraz am 6 Mai 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

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Sam Chak
Sam Chak am 7 Mai 2022

1 Stimme

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 Kommentare

Juan Lara Alcaraz
Juan Lara Alcaraz am 7 Mai 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 am 7 Mai 2022
@Sam Chak Thank you so much!!!! IT WORKS!!
Sam Chak
Sam Chak am 11 Mai 2022
I hope that information in this link could help you. Always find a way...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Torsten
Torsten am 6 Mai 2022

0 Stimmen

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 Kommentare

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by