Global error and local error of euler method
28 views (last 30 days)
Show older comments
The local error is proportional to h^2. I did not understand the relationship between the h and the error. Is it as h increase the error increase? Also the global error is proportional to h , how is that?
1 Comment
Torsten
on 29 Jan 2023
These questions are answered in every standard book about the numerical treatment of ordinary differential equations.
Answers (1)
Sulaymon Eshkabilov
on 28 Jan 2023
Yes, error increase if h step size increases. See this simple example:
dy = y(t) with y(0) = 3;
y(n+1) =y(n)+h*f(t(n), y(n));
from the givend ICs: f(t(0), y(0)) = 3, and therefore, y(1+1) = y(1)+h*3; Let's see in simulation:
h=0.1; % Case 1
f=@(t,y)y; % from the given exercise dy = y(t) and thus f(t,y) = dy;
t=0:h:5;
y(1)=3; % Initial Condition
for ii=2:numel(t) % calculations
y(ii) = y(ii-1)+h*f(t(ii-1),y(ii-1));
end
t=t.';
Solution = array2table(t);
Solution.Y = y.';
plot(t, y, 'r-', 'DisplayName', 'Case 1: h=0.1'), hold on
% Analytical solution:
syms t y(t)
Sol=dsolve(diff(y(t), t)==y, y(0)==3);
Sol_yt = vectorize(Sol)
t=(0:h:5).';
Sol = (eval(Sol_yt));
Error = abs(Sol)-Solution.Y; % Error = Analytical solution - Numerical Solution
Solution.Error=Error
Case1 = [t, Error];
clearvars y
h = 0.5; % Case 2
f=@(t,y)y; % from the given exercise dy = y(t) and thus f(t,y) = dy;
t=0:h:5;
y(1)=3; % Initial Condition
for ii=2:numel(t) % calculations
y(ii) = y(ii-1)+h*f(t(ii-1),y(ii-1));
end
plot(t, y, 'b-', 'DisplayName', 'Case 2: h=0.5'), grid on
t=t.';
Solution = array2table(t);
Solution.Y = y.';
t=(0:h:5).';
Sol = (eval(Sol_yt));
Error = abs(Sol)-Solution.Y; % Error = Analytical solution - Numerical Solution
Solution.Error=Error
Case2 = [t, Error];
clearvars y Solution
h = 1; % Case 3
f=@(t,y)y; % from the given exercise dy = y(t) and thus f(t,y) = dy;
t=0:h:5;
y(1)=3; % Initial Condition
for ii=2:numel(t) % calculations
y(ii) = y(ii-1)+h*f(t(ii-1),y(ii-1));
end
plot(t, y, 'k-', 'DisplayName', 'Case 3: h=1')
legend('show', 'location', 'NW')
xlabel('time')
ylabel('Solution, y(t)')
t=t.';
Solution = array2table(t);
Solution.Y = y.';
t=(0:h:5).';
Sol = (eval(Sol_yt));
Error = abs(Sol)-Solution.Y; % Error = Analytical solution - Numerical Solution
Solution.Error=Error
Case3 = [t, Error];
figure
plot(Case1(:,1), Case1(:,2), 'r'), hold on
plot(Case2(:,1), Case2(:,2), 'b')
plot(Case3(:,1), Case3(:,2), 'k'), grid on
legend('Case1: h=0.1', 'Case2: h=0.5', 'Case3: h=1', 'location', 'NW')
xlabel('time')
ylabel('Error')
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!