Clear Filters
Clear Filters

need to use this analytical solution to plot the absolute percent error in the concentration A at each time step for Euler's method

1 view (last 30 days)
%% Euler
nsteps = 12;
t = zeros (nsteps,1);
A = zeros (nsteps,1);
B = zeros(nsteps, 1);
P = zeros(nsteps,1);
A(1) = 1;
B(1) = 3;
C(1) = 0;
K = 5*10^-5;
for k = 2:13
t(k) = t(k-1)+3600;
A(k) = A(k-1)+(-K*A(k-1)*B(k-1))*3600;
B(k) = B(k-1)+(-Yb*(K*A(k-1)*B(k-1)))*3600;
P(k)= P(k-1)+ Yp*(K*A(k-1)*B(k-1))*3600;
end
timestep is 3600 sec
  3 Comments
Naveen Krish
Naveen Krish on 18 Mar 2022
I need to plot the absolute percent error in the concentration of A at each timestep of 3600sec from ti=0 to tf=12 hours using Eulers method
Sam Chak
Sam Chak on 18 Mar 2022
Edited: Sam Chak on 18 Mar 2022
You have been posting some "questions" in the last few days. If you don't want the question get "pruned" again, I suggest you to use the following template:
––– Template begins –––
Hi, I'm doing a <project> to solve this <problem> with the following <Math Equations> (in LaTeX form or in image). I have wrriten the code but I ran into some issues and received an error message OR need to do some <additional tasks>. I have tried searching in MATLAB Answers pertaining to my topics of study, but to no avail. The MATLAB code and the error message are shown below:
Click the circled icon to insert the MATLAB code and error message.
I have also attached the code/data for your convenience. I appreciate your help with troubleshooting the <problem>. OR Thanks for considering my request.
––– Template ends –––

Sign in to comment.

Answers (1)

VBBV
VBBV on 29 Mar 2022
nsteps = 12;
t = zeros (nsteps,1);
A = zeros (nsteps,1);
B = zeros(nsteps, 1);
P = zeros(nsteps,1);
A(1) = 1;
B(1) = 3;
C(1) = 0;
K = 5*10^-5;
Yp = 1.34; % e,g, values
Yb = Yp/2; % e.g
for k = 2:13
t(k) = t(k-1)+3600;
Aold = A(k-1);
A(k) = A(k-1)+(-K*A(k-1)*B(k-1))*3600;
B(k) = B(k-1)+(-Yb*(K*A(k-1)*B(k-1)))*3600;
P(k)= P(k-1)+ Yp*(K*A(k-1)*B(k-1))*3600;
Aps(k) = abs((A(k)-Aold)/A(k))*100; % absolute percent error
end
plot(Aps);title('Absoulte % error'); ylabel('%'); xlabel('timestep')
Check with values for Yb and Yp . i have used them here for e.g. purpose
  1 Comment
Torsten
Torsten on 29 Mar 2022
A(k-1) and A(k) are the solutions for A at times t(k-1) and t(k). Thus the difference A(k) - A(k-1) is an indicator on how much the solution changes with time (so something like A'(t(k))*dt), but not an error in the Euler integration.

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!