Problem 1 - Finite differences

advertisement
Problem 1 - Finite differences
Table of Contents
Defining test functions and a reference point ........................................................................... 1
Approximate
by FD methods .................................................................................. 2
Approximation order
and
.............................................................................
Exercise 1.1: validate other approximations .............................................................................
Round-off error ...................................................................................................................
Exercise 1.2: Second order derivatives ....................................................................................
3
4
6
9
In this problem we will perform a convergence study of forward, backward and central difference schemes with
a trigonometrical function. We will introduce the approximation order of truncation error, and the round-off error of
floating-point machines.
by Alfonso Rodriguez-Molares (alfonsom@ntnu.no) 27.04.2015
Defining test functions and a reference point
To test the performance of the three FD schemes we define an arbitrary trigonometric function,
,
and some of its derivatives which are,
,
,
,
f = inline('cos(2*pi*x)+sin(4*pi*x+0.25)');
df= inline('-sin(2*pi*x)*2*pi+cos(4*pi*x+0.25)*4*pi');
ddf= inline('-cos(2*pi*x)*(2*pi)^2-sin(4*pi*x+0.25)*(4*pi).^2');
dddf= inline('sin(2*pi*x)*(2*pi)^3-cos(4*pi*x+0.25)*(4*pi).^3');
We define the point
in which the derivative will be calculated
x0=0.58;
Let us plot the function and the derivative at
x=linspace(0,1,100);
figure;
1
Problem 1 - Finite differences
plot(x,f(x)); hold on; grid on;
plot(x0,f(x0),'ro');
plot(x0+[-1 1],f(x0)+df(x0)*[-1 1],'r--');
axis([0 1 min(f(x)) max(f(x))]);
xlabel('x');
ylabel('f(x)');
legend('f(x)','x0','df(x0)');
set(gca,'FontSize', 12);
Approximate
by FD methods
Now we apply the forward, backward and central difference schemes to approximate
must select a step value
. First we
, but instead of doing it for a single value we will check how the error depends
on the step size. Because of that, we define
as a vector
h=logspace(-2,-5,50);
And we apply the forward, backward and central difference formulae to
FD=(f(x0+h)-f(x0))./h;
BD=(f(x0)-f(x0-h))./h;
CD=(f(x0+h)-f(x0-h))/2./h;
% forward difference
% backward difference
% central difference
The absolute error of each approximation is simply calculated as
2
Problem 1 - Finite differences
error_FD=abs(FD-df(x0));
error_BD=abs(BD-df(x0));
error_CD=abs(CD-df(x0));
which we can plot by
figure;
loglog(h,error_FD,'b.-'); hold on; grid on;
loglog(h,error_BD,'ro--');
loglog(h,error_CD,'gs-');
legend('Forward difference','Backward difference','Central difference','Location','
xlabel('h');
ylabel('Absolute Error');
set(gca,'FontSize', 12);
Approximation order
and
By looking at the previous plot it seems obvious that the central difference scheme converges faster than
the other two. In addition, if we plot the functions
of FD and BD is the same as
and
we then realize that the convergence slope
, whilst CD has the same convergence slope as
figure;
loglog(h,error_FD,'b.-'); hold on; grid on;
loglog(h,error_BD,'ro--');
loglog(h,error_CD,'gs-');
3
.
Problem 1 - Finite differences
loglog(h,(abs(ddf(x0))/2).*h,'k--','linewidth',2);
loglog(h,(abs(dddf(x0))/6).*h.^2,'k:','linewidth',2);
legend('Forward difference','Backward difference','Central difference','|1/2 df^2(x
xlabel('h');
ylabel('Absolute Error');
set(gca,'FontSize', 12);
This convergence slope is referred to as the approximation order and is often written together with the
schemes as
Forward difference:
Backward difference:
Central difference:
Exercise 1.1: validate other approximations
Implement the following FD schemes and perform a convergence study. Estimate the order of the local
truncation error for each formula.
4
Problem 1 - Finite differences
a)
b)
c)
d)
We implement the schemes
A=(-f(x0+2*h)+4*f(x0+h)-3*f(x0))/2./h;
B=(f(x0+2*h)+f(x0+h)-f(x0)-f(x0-h))./h/4;
C=(2*f(x0+h)+3*f(x0)-6*f(x0-h)+f(x0-2*h))/6./h;
D=(-f(x0+2*h)+8*f(x0+h)-8*f(x0-h)+f(x0-2*h))/12./h;
and plot the error directly
figure;
loglog(h,abs(A-df(x0)),'m^-'); hold on; grid on;
loglog(h,abs(B-df(x0)),'gs-');
loglog(h,abs(C-df(x0)),'b.-');
loglog(h,abs(D-df(x0)),'ro-');
loglog(h,(abs(ddf(x0))/2)*h,'k--'); text(h(25),5*100*h(25),'O(h)','FontSize',12);
loglog(h,(abs(dddf(x0))/3)*h.^2,'k--'); text(h(25),20*200*h(25).^2,'O(h^2)','FontSi
loglog(h,2000*h.^3,'k--'); text(h(25),40*2000*h(25).^3,'O(h^3)','FontSize',12);
loglog(h,3000*h.^4,'k--'); text(h(25),60*3000*h(25).^4,'O(h^4)','FontSize',12);
legend('scheme a)','scheme b)','scheme c)','scheme d)','Location','southeast');
axis([1e-5 1e-2 1e-15 1]);
xlabel('h');
ylabel('Absolute Error');
set(gca,'FontSize', 12);
5
Problem 1 - Finite differences
Round-off error
Even if you got the order right something went definitely wrong with last exercise. At a certain point the
methods with
and
start to go nuts! Why is the whole universe against me??!!
Perhaps we can get a hint of what is going on if we step back a little. Let us go back to the FD, BD and
CD schemes, but this time let us increase the range of the convergence study
% new vector with increase range
h=logspace(-2,-16,100);
% calculate approximations
FD=(f(x0+h)-f(x0))./h;
% forward difference
BD=(f(x0)-f(x0-h))./h;
% backward difference
CD=(f(x0+h)-f(x0-h))/2./h;
% central difference
% calculate error
error_FD=abs(FD-df(x0));
error_BD=abs(BD-df(x0));
error_CD=abs(CD-df(x0));
% and plot
figure;
loglog(h,error_FD,'b.-'); hold on; axis tight; grid on;
loglog(h,error_BD,'ro--');
loglog(h,error_CD,'gs-');
legend('Forward difference','Backward difference','Central difference','Location','
xlabel('h');
6
Problem 1 - Finite differences
ylabel('Absolute Error');
set(gca,'FontSize', 12);
Ok, so it is not just
and
that suffer from that weird artifact, also
and
but for smaller . Interestingly enough the increasing error of the three methods seems to align. It almost
seems there is a limit beyond which we cannot get more precision.
Ok. :| I know it is in the section title.
That is limit set by the quantization or round-off error of your machine. Even floating-point machines
cannot represent all real number perfectly and hence there would be a very small error between the number
you want and the number stored in the computer memory. Matlab allows us to check how much that error
is with the function eps. For instance, the representation error of the number 5 is
eps(5)
ans =
8.8818e-16
Not a big quantity, but small uncertainties can have significant effects in division operations, specially if
the handled quantities are very small. Making use of function eps we can apply the theory for uncertainty
propagation and calculate what would be the uncertainty of the estimation of the three methods FD, BD
and CD
7
Problem 1 - Finite differences
% FD
numerator_rel_error=(eps(f(x0))+eps(f(x0+h)))./abs(f(x0+h)-f(x0));
denominator_rel_error=eps(h)./h;
FD_roundoff_error=FD.*(numerator_rel_error+numerator_rel_error);
% BD
numerator_rel_error=(eps(f(x0))+eps(f(x0-h)))./abs(f(x0)-f(x0-h));
denominator_rel_error=eps(h)./h;
BD_roundoff_error=BD.*(numerator_rel_error+numerator_rel_error);
% CD
numerator_rel_error=(eps(f(x0+h))+eps(f(x0-h)))./abs(f(x0+h)-f(x0-h));
denominator_rel_error=eps(h)./h;
CD_roundoff_error=CD.*(numerator_rel_error+numerator_rel_error);
% and plot
figure;
loglog(h,error_FD,'b.-'); hold on; axis tight; grid on;
loglog(h,FD_roundoff_error,'b--');
loglog(h,error_BD,'ro-');
loglog(h,BD_roundoff_error,'r--');
loglog(h,error_CD,'gs-');
loglog(h,CD_roundoff_error,'g--');
legend('FD total error','FD round-off error','BD total error','BD round-off error',
xlabel('h');
ylabel('Absolute Error');
set(gca,'FontSize', 12);
8
Problem 1 - Finite differences
Exercise 1.2: Second order derivatives
Just with single derivatives we wont get very far in the realm of wave propagation. Let us introduce the
following FD approximations for the second order derivative
Forward:
Backward:
Central:
Central
(
And let us perform a convergence study to validate those. We start by selecting a propor
):
vector
h=logspace(-2,-4,50);
We implement the FD approximations
FDxx=(f(x0+2*h)-2*f(x0+h)+f(x0))./h.^2;
BDxx=(f(x0)-2*f(x0-h)+f(x0-2*h))./h.^2;
CDxx=(f(x0+h)-2*f(x0)+f(x0-h))./h.^2;
CDxxO4=(-f(x0+2*h)+16*f(x0+h)-30*f(x0)+16*f(x0-h)-f(x0-2*h))./h.^2/12;
and we plot the error
figure;
loglog(h,abs(FDxx-ddf(x0)),'b.-'); hold on; axis tight; grid on;
loglog(h,abs(BDxx-ddf(x0)),'rs-');
loglog(h,abs(CDxx-ddf(x0)),'g^-');
loglog(h,abs(CDxxO4-ddf(x0)),'mo--');
loglog(h,8e2*h,'k--');
loglog(h,2e3*h.^2,'k--');
loglog(h,4e4*h.^4,'k--');
legend('FD','BD','CD','CD h^4','O(h)','O(h^2)','O(h^4)','Location','southeast');
xlabel('h');
ylabel('Absolute Error');
set(gca,'FontSize', 12);
9
Problem 1 - Finite differences
Published with MATLAB® R2014b
10
Download