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.
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 Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

2022년 5월 6일

댓글:

2022년 5월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by