Euler Method Approximation Not Working

5 vues (au cours des 30 derniers jours)
Ali
Ali le 17 Déc 2021
Commenté : Ali le 17 Déc 2021
Hello,
I am trying to plot a loglog error of the Euler IVP method. Everything works except for one issue. My approximation is always zero. I don't know why.
function error_euler
yfun = dsolve('Dy=t+y', 'y(0)=0', 't');
disp(['The IVP is Dy = t+y and the solution of IVP is ']);
disp(['y='])
disp(yfun);
f=@(t,y)t+y;
t0=0;
w0=0;
disp(['--------------------'])
disp([' Step Size Exact Solution Approximation Error'])
disp(['--------------------'])
for i=1:6
a(i)=0;
w1(i)=0;
h(i)=0;
t(i)=0;
end
for k=0:5
h(k+1)=h(k+1)+0.1*2^-k;
t=h(k+1);
a(k+1)=a(k+1)+eval(yfun);
w1(k+1)=w1(k+1)+h(k+1)*f(t0,w0);
end
for k=1:6
disp([h(k) a(k) w1(k) abs(a(k)-w1(k))])
end
loglog(h,abs(a-w1))
xlabel('h')
ylabel('Error')
grid on
title('Global Error Log-Log Plot')
end
You can see below that the approximation is zero. What am I doing wrong?
--------------------
Step Size Exact Solution Approximation Error
--------------------
0.100000000000000 0.005170918075648 0 0.005170918075648
0.050000000000000 0.001271096376024 0 0.001271096376024
0.025000000000000 0.000315120524429 0 0.000315120524429
0.012500000000000 0.000078451540634 0 0.000078451540634
0.006250000000000 0.000019572003762 0 0.000019572003762
0.003125000000000 0.000004887902739 0 0.000004887902739

Réponses (1)

Voss
Voss le 17 Déc 2021
Take a look at how the approximation is calculated:
w1(k+1)=w1(k+1)+h(k+1)*f(t0,w0);
You are always evaluating f at t0,w0.
  1 commentaire
Ali
Ali le 17 Déc 2021
Thank you! I didn't notice that.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by