Deriving the fourth order approximations of second order derivative in different methods and compare using MATLAB
Objective:
To determine the fourth order approximation of second order derivative, through central difference, Right side skewed and Left side skewed schemes. It is followed by evaluating the function \'`exp(x)*cos(x)`, both analytically and then by numerically, through 3 different schemes derived, and to compare the errors produced by these numerical methods against analytically derived value, and also to analyze which method is more useful and helpful.
Procedure:
1. Central Difference:
For Central difference scheme, the grid should have points or nodes on either side of the point or node considered. For second order derivative, the stencil for central difference gives:
`f\'\'(x)=af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2)`
Now, the equation is expanded and the Taylor\'s table is formed, then it gives out a set of linear equations as follows:
`a+b+c+d+e=0`
`(-2a-b+d+2e)Deltax=0`
`(4/2a+b/2+d/2+4/2e)(Deltax^2)=1`
`(-8/6a-b/6+d/6+8/6e)(Deltax^3)=0`
`(16/24a+b/24+d/24+16/24e)(Deltax^4)=0`
Then, Solving these equations in the form of matrix, to obtain the unknowns, through Matlab, we get
clear all
close all
clc
%Input
A =[1 1 1 1 1;-2 -1 0 1 2;4/2 1/2 0 1/2 4/2;-8/6 -1/6 0 1/6 8/6;16/24 1/24 0 1/24 16/24]
B=[0;0;1;0;0]
X= A\\B
format rational
%Output
X =
-1/12
4/3
-5/2
4/3
-1/12
Equating with a, b, c, d, e, respectively, and substituting the values in the equation from the stencil, we get fourth order approximation through central difference as follows:
`f\'\'(i)=(-1/12f(i-2)+4/3f(i-1)-5/2f(i)+4/3f(i+1)-1/12f(i+2))//Deltax^2`
Simplifying this equation, we get,
`f\'\'(i)= (f(i-2)-16f(i-1)+30f(i)-16f(i+1)+f(i+2))//12Deltax^2`
Hence the fourth order approximation of second order derivative, through central difference method is obtained.
2.Skewed Right side difference:
For Skewed right side difference, the grid should have points or nodes on right side of the point or node considered. For second order derivative, the stencil for Skewed right side difference gives:
`f\'\'(i)=af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)
Now, the equation is expanded and the Taylor\'s table is formed, then it gives out a set of linear equations as follows:
`a+b+c+d+e+h=0`
`(b+2c+3d+4e+5h)=0`
`(b+4c+9d+16e+25h)//2=1`
`(b+8c+27d+64e+125h)//6=0`
`(b+16c+81d+256e+625h)//24=0`
`(b/120+32c//120+243d//120+1024e//120+3125h/120)=0`
Then, Solving these equations in the form of matrix, to obtain the unknowns, through Matlab, we get
clear all
close all
clc
A=[1 1 1 1 1 1;0 1 2 3 4 5;0 1/2 4/2 9/2 16/2 25/2;0 1/6 8/6 27/6 64/6 125/6;0 1/24 16/24 81/24 256/24 625/24;0 1/120 32/120 243/120 1024/120 3125/120];
B=[0;0;1;0;0;0];
X=A\B
%Output
X =
15/4
-77/6
107/6
-13
61/12
-5/6
Equating with a, b, c, d, e, respectively, and substituting the values in the equation from the stencil, we get fourth order approximation through Skewed right side difference as follows:
`f\'\'(i)=(15/4f(i)-77/6f(i+1)+107/6f(i+2)-13f(i+3)+61/12f(i+4)-5/6f(i+5))//Deltax^2`
Simplifying this equation, we get,
`f\'\'(i)=(45f(i)-154(i+1)+214f(i+2)-26f(i+3)+61f(i+4)-10f(i+5))//12Deltax^2`
Hence the fourth order approximation of second order derivative, through Skewed right side difference method is obtained.
3.Skewed Left side difference:
For Skewed left side difference, the grid should have points or nodes on left side of the point or node considered. For second order derivative, the stencil for Skewed left side difference gives:
``Deltax^2*`f\'\'(i)=af(i)+bf(i-1)+cf(i-2)+df(i-3)+ef(i-4)+hf(i-5)`
Now, the equation is expanded and the Taylor\'s table is formed, then it gives out a set of linear equations as follows:
`a+b+c+d+e=h=0`
`(-b-2c-3d-4e-5h)=0`
`(b+4c+9d+16e+25h)//2=1`
`(-b-8c-27d-64e-125h)//6=0`
`(b+16c+81d+256e+625h)//24=0`
`(-b/120-32c//120-243d//120-1024e//120-3125h/120)=0`
Then, Solving these equations in the form of matrix, to obtain the unknowns, through Matlab, we get
clear all
close all
clc
A=[1 1 1 1 1 1;0 -1 -2 -3 -4 -5;0 1/2 4/2 9/2 16/2 25/2;0 -1/6 -8/6 -27/6 -64/6 -125/6;0 1/24 16/24 81/24 256/24 625/24;0 -1/120 -32/120 -243/120 -1024/120 -3125/120];
B=[0;0;1;0;0;0];
X=A\B
%Output
X =
15/4
-77/6
107/6
-13
61/12
-5/6
Equating with a, b, c, d, e, respectively, and substituting the values in the equation from the stencil, we get fourth order approximation through Skewed left side difference as follows:
`f\'\'(i)=(15/4f(i)-77/6f(i-1)+107/6f(i-2)-13f(i-3)+61/12f(i-4)-5/6f(i-5))//Deltax^2`
Simplifying this equation, we get,
`f\'\'(i)=(45f(i)-154(i-1)+214f(i-2)-26f(i-3)+61f(i-4)-10f(i-5))//12Deltax^2`
Hence the fourth order approximation of second order derivative, through Skewed left side difference method is obtained.
Evaluating the function and comparisons:
Given function is:
`f(x)=exp(x)*cos(x)`
Second order Analytical Derivative:
`f\'\'(x)=-2*exp(x)*sin(x)`
The function for Central difference method of fourth order approximation of Second order derivative:
function out=Central_difference(x,dx)
%Analytical derivative
analytical_derivative=-2*exp(x)*sin(x);
%Numerical Derivative
%Central difference
central_difference=(exp(x-(2*dx))*cos(x-(2*dx))-16*exp(x-dx)*cos(x-dx)+30*exp(x)*cos(x)-16*exp(x+dx)*cos(x+dx)+exp(x+(2*dx))*cos(x+(2*dx)))/12*(dx^2);
%Error
out=abs(analytical_derivative-central_difference);
end
The function for Skewed left side difference method of fourth order approximation of Second order derivative:
function out=Skewed_left_side(x,dx)
%Analytical derivative
analytical_derivative=-2*exp(x)*sin(x);
%Numerical Derivative
%Left side skewed
left_side_skewed=(45*exp(x)*cos(x)-154*exp(x-dx)*cos(x-dx)+214*exp(x-(2*dx))*cos(x-(2*dx))-26*exp(x-(3*dx))*cos(x-(3*dx))+61*exp(x-(4*dx))*cos(x-(4*dx))-10*exp(x+(5*dx))*cos(x+(5*dx)))/12*(dx)^2;
%Error
out=abs(analytical_derivative-left_side_skewed);
end
The function for Skewed right side difference method of fourth order approximation of Second order derivative:
function out=Skewed_right_side(x,dx)
%Analytical derivative
analytical_derivative=-2*exp(x)*sin(x);
%Numerical Derivative
%Right side skewed
right_side_skewed=(45*exp(x)*cos(x)-154*exp(x+dx)*cos(x+dx)+214*exp(x+(2*dx))*cos(x+(2*dx))-26*exp(x+(3*dx))*cos(x+(3*dx))+61*exp(x+(4*dx))*cos(x+(4*dx))-10*exp(x+(5*dx))*cos(x+(5*dx)))/12*(dx^2);
%Error
out=abs(analytical_derivative-right_side_skewed);
end
The Main program to have the difference comparison:
clear all
close all
clc
%Inputs
x=pi/3;
dx=linspace(pi/4,pi/4000,20);
for i=1:length(dx)
Central_difference_error(i)=Central_difference(x,dx(i));
Skewed_right_side_error(i)=Skewed_right_side(x,dx(i));
Skewed_left_side_error(i)=Skewed_left_side(x,dx(i));
end
figure(1)
loglog(dx,Central_difference_error)
hold on
loglog(dx,Skewed_right_side_error,\'r\')
hold on
loglog(dx,Skewed_left_side_error,\'g\')
legend(\'Central difference error\',\'Skewed right side error\',\' Skewed left side error\')
The Plots obtained:
From the graph, it can be observed that the Central difference scheme shows lesser error than the other two.
Error comparison of FDS and BDS:
As we compare the FDS and BDS, with Central, Right side skewed and Left side skewed, we can say that FDS and BDS show more error than that of the other three.Thus we could get more accuracy by these Central, Right side skewed and Left side skewed difference method.
Skewed or Central:
When comparing skewed and central difference schemes, Skewed schemes are far better than that of Central, because of the condition, that, a Central difference scheme can be used to calculate a point or node, only if the neighbouring points or nodes must be present on both sides. While coming to Skewed, it can be used to calculate the value everywhere in the grid, even at the boundary. So, Skewed schemes can be more useful.
Conclusion:
Thus, the necessary equations were derived, compared as our wish and conclusions were provided as asked.