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 Comments

Please provide the code you have so far.
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

Sign in to comment.

 Accepted Answer

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 Comments

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

Sign in to comment.

More Answers (1)

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 Comments

Is it possible to do it without extra toolboxes?
Yes. The equations are that easy that they can be solved using pencil and paper.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!