Euler Method Approximation Not Working

조회 수: 13 (최근 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 CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by