Euler Method Approximation Not Working

6 ビュー (過去 30 日間)
Ali
Ali 2021 年 12 月 17 日
コメント済み: Ali 2021 年 12 月 17 日
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

回答 (1 件)

Voss
Voss 2021 年 12 月 17 日
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 件のコメント
Ali
Ali 2021 年 12 月 17 日
Thank you! I didn't notice that.

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

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by