Derivation of Fourth Order Approximation for the Second Order Derivation and comparison of analytical solutions to the numerical solutions for Central Right Sided and Left Sided Difference Schemes
Objective
To derive the 4th order approximations of the second order derivative for central difference scheme, skewed right sided difference scheme and skewed left sided difference scheme.
A program is then to be written in Matlab and plots are to plotted to evaluate the second order derivative of the analytical function exp(x)*cos(x) and compare it with the 3 numerical approximations that has been derived and also with the forward and backward difference schemes.
Central Difference Scheme
The stencil used for this scheme to derive fourth order approximation of the second order derivative is shown in figure 1.1
Figure 1.1
Equation 1.1, shown below is used to find the coefficients a, b, c, d and e for this scheme
`(d^2 f)/(dx^2 ) ~~ af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2)`
Equation 1.1
After discretising equation 1.1, the Taylor\'s table method is used to derive equations to find coefficients a, b, c, d, e and f. Table 1.1 shows the Taylor\'s table.
Table 1.1
After forming five equations and five unkown values for coefficients, matlab code 1.1 is used to find the values of coefficients. The vales found in matlab for the coefficients for central difference scheme is shown in table 1.2.
Table 1.2
Equation 1.1 is then solved using the values of the coefficients and a new equation 1.2 is formed.
`(d^2 f)/(dx^2 ) ~~ af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2)~~ /_\\x^2 f\'\'(x)(2a+b/2+d/2+ 2e)+O(/_\\x^6)`
Equation 1.2
Equation 1.2 is then divided by `/_\\x^2`, hence a new equation 1.3 is formed.
`(d^2 f)/(dx^2 ) ~~ (af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2))/(/_\\x^2 ) ~~ f\'\' (x)(…)+O(/_\\x^4)`
Equation 1.3
This new equation (1.3) shows the order of the truncation error as `/_\\x^4`, hence it can be said that the truncation error is of fourth order approximation for central difference scheme.
Skewed Right Sided Difference Scheme
The stencil used to find the fourth order approximation for skewed right sided difference is given below in figure 2.1.
Figure 2.1
The equation for skewed right sided differecing is shown in equation 2.1
`(d^2 f)/(dx^2 ) ~~ af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+zf(i+5)`
Equation 2.1
Taylor\'s table is made after deriving the equation 2.1. The table for the results of the derivation is shown in table 2.1
Table 2.1
Similar to how the coefficinets were found for the central difference scheme, matlab code 1.1 is used to find the values of coefficients for this scheme. The coefficents can be found in table 2.2
Table 2.2
Similarly equation is solved after computing the values for the coefficients and a new equation (2.2) is formed
`(d^2 f)/(dx^2 ) ~~ af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+zf(i+5)~~ /_\\x^2 f\'\' (x)(b/2+2c+9d/2+ 8e+25z/2)+O(/_\\x^6)`
Equation 2.2
After dividing the whole equation 2.2 by `/_\\x^2`, the final equation 2.3 is derived.
`(d^2 f)/(dx^2 ) ~~ (af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+zf(i+5))/(/_\\x^2 ) ~~ f\'\' (x)(…)+O(/_\\x^4)`
Equation 2.3
The order of the truncation error in equation 2.3 is `/_\\x^4`, hence it is proved that the truncation error is of fourth order approximation for skewed right sided difference scheme.
Skewed Left Sided Difference Scheme
The stencil used for skewed left sided scheme is shown in figure 3.1
Figure 3.1
The equation for derivation is the equation given below (equation 3.1).
`(d^2 f)/(dx^2 ) ~~ af(i)+bf(i-1)+cf(i-2)+df(i-3)+ef(i-4)+zf(i-5)`
Equation 3.1
Taylor\'s Table is then generated after discretizing the equation 3.1. Taylor\'s table for this scheme is shown in table 3.1
Table 3.1
Coefficients are found using the same matlab code 1.1 as in previous schemes. The Coefficients, as seen in table 3.2 are found to be exactly same as in the right sided difference scheme.
Table 3.2
The coefficients are substituted in the discretized equation 3.1 and equation 3.2 is formed.
`(d^2 f)/(dx^2 ) ~~ af(i)+bf(i-1)+cf(i-2)+df(i-3)+ef(i-4)+zf(i-5)~~/_\\x^2 f\'\' (x)(b/2+2c+9d/2+ 8e+25z/2)+O(/_\\x^6)`
Equation 3.2
Equation 3.2 is further divided by `/_\\x^2`, and equation 3.3 is formed.
`(d^2 f)/(dx^2 ) ~~ (af(i)+bf(i-1)+cf(i-2)+df(i-3)+ef(i-4)+zf(i-5))/(/_\\x^2 ) ~~ f^\'\' (x)(…)+O(/_\\x^4)`
Equation 3.3
Hence, it is proved that the truncation error is of fourth order approximation for skewed left sided difference scheme as the order of the truncation error in equation 3.3 is `/_\\x^4`.
Forward and backward difference scheme formula- second order
The general formula for forward difference scheme is given in equation 4.1 whereas, equation 4.2 represents the formula for backward difference scheme.
`f\'\'(x)~~(f(x+2dx)-2f(x+dx)+f(x))/dx^2`
Equation 4.1
`f\'\' (x)~~(f(dx)-2f(x-dx)+f(x-2dx))/dx^2`
Equation 4.2
Code for coefficients
Matlab Code 1.1 for finding the coefficients
clear all
close all
clc
%Solving coefficients for Skewed Central Difference Scheme
A= [1 1 1 1 1;
-2 -1 0 1 2;
2 1/2 0 1/2 2;
-4/3 -1/6 0 1/6 4/3;
2/3 1/24 0 1/24 2/3;];
B=[0; 0; 1; 0; 0;];
Central=A\\B
%Solving coefficients for Skewed Right Sided difference Scheme
C= [1 1 1 1 1 1;
0 1 2 3 4 5;
0 1/2 2 9/2 8 25/2;
0 1/6 4/3 9/2 32/3 125/6;
0 1/24 4/6 27/8 32/3 625/24;
0 1/120 4/15 81/40 128/15 625/24];
D=[0; 0; 1; 0; 0;0];
Right=C\\D
%Solving coefficients for Skewed Left Sided difference Scheme
E= [1 1 1 1 1 1;
0 -1 -2 -3 -4 -5;
0 1/2 2 9/2 8 25/2;
0 -1/6 -4/3 -9/2 -32/3 -125/6;
0 1/24 4/6 27/8 32/3 625/24;
0 -1/120 -4/15 -81/40 -128/15 -625/24];
F=[0; 0; 1; 0; 0;0];
Left=E\\F
Code 1.1
Matlab Program
Following matlab code is written (code 1.2) after deriving and finding the coefficients, The code is written to compare the numerical and analytical solutions for all schemes, including the forward and backward difference schmes and to generate two different plots. Plot 1 is used to compare the absolute error difference for central, skewed right and left sided schemes, whereas, plot 2 is used to compare the schemes of plot 1 with forward and backward difference schemes. It is to be noted that the coefficients and the number of terms used for right and left sided schemes are same, therefore, the naming for the coefficients in matlab used are same: a, b, c, d, e and z. The names of coefficients for central difference scheme is chosen to be k, l, m, n and o.
clear all
close all
clc
x=pi/3;
%Grid size range
dx=linspace(pi/4, pi/400, 20);
%Function f(x)= exp(x)*cos(x)
%Analytical derivative of the function- second order
analytical_derivative= -2*exp(x)*sin(x);
%Coefficients a, b, c, d, e and z for Skewed right and left sided difference
a=3.7500;
b=-12.8333;
c=17.8333;
d=-13.0000;
e= 5.0833;
z=-0.8333;
%Coefficients k, l, m, n, and o for Central Difference
k= -0.0833;
l= 1.3333;
m= -2.5000;
n= 1.3333;
o=-0.0833;
for i=1:length(dx)
% Central difference
central_difference(i)= (((k*exp(x-2*dx(i)))*(cos((x-2*dx(i)))))+((l*exp(x-1*dx(i)))*(cos((x-1*dx(i)))))+(m*exp(x)*cos(x))+((n*exp(x+1*dx(i)))*(cos((x+1*dx(i)))))+((o*exp(x+2*dx(i)))*(cos((x+2*dx(i))))))/dx(i)^2;
% Skewed right sided difference
skewed_right(i)= ((a*exp(x)*cos(x))+((b*exp(x+1*dx(i)))*(cos((x+1*dx(i)))))+((c*exp(x+2*dx(i)))*(cos((x+2*dx(i)))))+((d*exp(x+3*dx(i)))*(cos((x+3*dx(i)))))+((e*exp(x+4*dx(i)))*(cos((x+4*dx(i)))))+((z*exp(x+5*dx(i)))*(cos((x+5*dx(i))))))/dx(i)^2;
% Skewed left sided difference
skewed_left(i)= ((a*exp(x)*cos(x))+((b*exp(x-1*dx(i)))*(cos((x-1*dx(i)))))+((c*exp(x-2*dx(i)))*(cos((x-2*dx(i)))))+((d*exp(x-3*dx(i)))*(cos((x-3*dx(i)))))+((e*exp(x-4*dx(i)))*(cos((x-4*dx(i)))))+((z*exp(x-5*dx(i)))*(cos((x-5*dx(i))))))/dx(i)^2;
%Forward difference scheme
forward_difference(i)=((cos(x+2*dx(i))*exp(x+2*dx(i)))-2*(cos(x+1*dx(i))*exp(x+1*dx(i)))+(cos(x)*exp(x)))/dx(i)^2;
%Backward difference scheme
backward_difference(i)=((cos(x)*exp(x))-2*(cos(x-1*dx(i))*exp(x-1*dx(i)))+(cos(x-2*dx(i))*exp(x-2*dx(i))))/dx(i)^2;
end
% Central Difference error
central_error= abs(central_difference-analytical_derivative);
% Skewed right sided difference error
right_error= abs(skewed_right- analytical_derivative);
% Skewed left sided difference error
left_error= abs(skewed_left- analytical_derivative);
% Forward difference scheme error
forward_error= abs(forward_difference- analytical_derivative);
% Backward difference scheme error
backward_error= abs(backward_difference- analytical_derivative);
%plot of Grid Size vs Absolute Errors Between Central, Skewed Right and Skewed Left Sided Difference Schemes
figure(1)
loglog(dx, central_error,\'r\')
hold on
loglog(dx, right_error,\'g\')
hold on
loglog(dx, left_error,\'b\')
hold on
grid on
xlabel(\'Grid size\')
ylabel(\'Absolute error\')
legend(\'Central Difference Scheme Error\',\'Skewed Right Sided Error\',\'Skewed Left Sided Error\')
title (\'Comparing Absolute Errors Between Central, Skewed Right and Skewed Left Sided Difference Schemes\')
%Plot Comparing Absolute Errors Of Central, Skewed Right and Skewed Left Sided Difference Schemes with Forward and Backward Difference Schemes
figure(2)
loglog(dx, central_error,\'r\')
hold on
loglog(dx, right_error,\'g\')
hold on
loglog(dx, left_error,\'b\')
hold on
loglog(dx, forward_error,\'--r\')
hold on
loglog(dx, backward_error,\':b\',\'linewidth\',1)
hold on
grid on
xlabel(\'Grid size\')
ylabel(\'Absolute error\')
legend(\'Central Difference Scheme Error\',\'Skewed Right Sided Error\',\'Skewed Left Sided Error\',\'Forward Difference Scheme\',\'Backward Difference Scheme\')
title (\'Comparing Absolute Errors Of Central, Skewed Right and Skewed Left Sided Difference Schemes with Forward and Backward Difference Schemes\')
Code 1.2
Results
Plot 1
Plot one compares how the absolute error differs between the three schemes for the same function. It is observed that the error values decrease for all the schemes as the grid size is increased. Overall decrease in error is largest for the right sided schemes as compares to the others. Central difference scheme saw the least decrease in the error. This may be due to lesser number of neighbouring points used, four compared to five in right and left sided schemes.
Skewed difference schemes compared to central difference scheme
It is also significant that for any given function, skewed scheme will be more accurate as the neighbouring points used for central scheme will be less for any given order approximation as compared to what it will be for right and left sided schemes. Central difference scheme cannot be used for higher order approximations in problems related to heat conduction, for example, where the values will always travel in one direction. Central difference schemes cannot be used for first order approximation.
Plot 2
The forward and backward difference scheme does not provide lower error values than the skewed schemes for the same grid size and the same order derivation. Thus, it is evident that the skewed difference scheme and the central difference scheme provides more accurate data for the same order derivative than the forward or the backward difference scheme.
Conclusion
Fourth order approximations for the second order derivation has been derived for skewed and central difference schemes. Codes are written and different schemes are compared. Advantages of one scheme over another has been discussed. It is found that the skewed schemes have certain advantages for particular real life problems over the central scheme and it is also seen how the skewed scheme is advantageous at finding accurate data over the forward and backward schemes.