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 件のコメント

Stephan
Stephan 2022 年 5 月 6 日
Please provide the code you have so far.
Juan Lara Alcaraz
Juan Lara Alcaraz 2022 年 5 月 6 日
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

サインインしてコメントする。

 採用された回答

Sam Chak
Sam Chak 2022 年 5 月 7 日

1 投票

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 件のコメント

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

サインインしてコメントする。

その他の回答 (1 件)

Torsten
Torsten 2022 年 5 月 6 日

0 投票

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 件のコメント

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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by