4th order approximations of the second-order derivative
Here we are supposed to find the error of forward backward and central diffrencing scheme in a second order derivative with fourth order accuracy.
Using Taylor table we can derive the function of second order derivative with forward backward and central differencing. The values of coefficients can be found by converting the equations into matrix format.
value of coefficients for FDE
a = 2.9167
b = -8.6667
c = 9.5000
d = -4.6667
e = 0.9167
Value of coefficients for BDE
a = 2.9167
b = -8.6667
c = 9.5000
d = -4.6667
e = 0.9167
Value of coefficients for CD
a = -0.0833
b = 1.3333
c = -2.5000
d = 1.3333
e = -0.0833
Using these values we can derive the equation for FDE,BDE and CD.
close all
clear all
clc
%second_order_derivative_with_fourth_order_approximations_by_skewed_right_sided_difference
M = [1 1 1 1 1;0 1 2 3 4;0 1/2 2 9/2 8;0 1/6 4/3 9/2 32/3;0 1/24 2/3 27/8 32/3];
C = [0;0;1;0;0];
a = inv(M)*C;
%second_order_derivative_with_fourth_order_approximations_by_central_difference
M1 = [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];
C1 = [0;0;1;0;0];
a1 = inv(M1)*C1;
%second_order_derivative_with_fourth_order_approximations_by_skewed_left_sided_difference
M2 = [1 1 1 1 1;0 -1 -2 -3 -4;0 1/2 2 9/2 8;0 -1/6 -4/3 -9/2 -32/3;0 1/24 2/3 27/8 32/3];
C2 = [0;0;1;0;0];
a2 = inv(M)*C;
x = pi/3;
dx = linspace(pi/4,pi/4000,100);
%skewed_right_sided_difference_equation
%forward_differencing = ((35*(exp(x)*cos(x)))-(104*(exp(x+dx)*cos(x+dx)))+(114*(exp(x+2*dx)*cos(x+2*dx)))-(56*(exp(x+3*dx)*cos(x+3*dx)))+(11*(exp(x+4*dx)*cos(x+4*dx))))/(12*(dx^2));
%skewed_left_sided_difference_equation
%backward_differencing = ((35*(exp(x)*cos(x)))-(104*(exp(x-dx)*cos(x-dx)))+(114*(exp(x-2*dx)*cos(x-2*dx)))-(56*(exp(x-3*dx)*cos(x-3*dx)))+(11*(exp(x-4*dx)*cos(x-4*dx))))/(12*(dx^2));
%central_difference
%central_differencing = (-(exp(x-2*dx)*cos(x-2*dx))-(exp(x+2*dx)*cos(x+2*dx))-(30*(exp(x)*cos(x)))+(16*(exp(x-dx)*cos(x-dx)))+(16*(exp(x+dx)*cos(x+dx))));
Analytical_value = -2*(exp(x)*sin(x));
for i = 1:length(dx)
forward_differencing(i) = ((35*exp(x)*cos(x))-(104*exp(x+dx(i))*cos(x+dx(i)))+(114*exp(x+2*dx(i))*cos(x+2*dx(i)))-(56*exp(x+3*dx(i))*cos(x+3*dx(i)))+(11*exp(x+4*dx(i))*cos(x+4*dx(i))))/(12*(dx(i)^2));
error_forward_differencing(i) = abs(Analytical_value - forward_differencing(i));
backward_differencing(i) = ((35*exp(x)*cos(x))-(104*exp(x-dx(i))*cos(x-dx(i)))+(114*exp(x-2*dx(i))*cos(x-2*dx(i)))-(56*exp(x-3*dx(i))*cos(x-3*dx(i)))+(11*exp(x-4*dx(i))*cos(x-4*dx(i))))/(12*(dx(i)^2));
error_backward_differencing(i) = abs(Analytical_value - backward_differencing(i));
central_differencing(i) = (-(exp(x-2*dx(i))*cos(x-2*dx(i)))-(exp(x+2*dx(i))*cos(x+2*dx(i)))-(30*exp(x)*cos(x))+(16*exp(x-dx(i))*cos(x-dx(i)))+(16*exp(x+dx(i))*cos(x+dx(i))))/(12*(dx(i)^2));
error_central_differencing(i) = abs(Analytical_value - central_differencing(i));
end
figure(1)
loglog(dx,error_forward_differencing,\'color\',\'r\');
hold on
loglog(dx,error_backward_differencing,\'color\',\'b\');
hold on
loglog(dx,error_central_differencing,\'color\',\'g\');
xlabel(\'Spacing between node points\');
ylabel(\'Error Value\');
title(\'Comparission between forward backward and central diffrencing\');
legend(\'error forward differencing\',\'error backward differencing\',\'error central differencing\');
error with respect to the analytical solution is found out here using this code.
The results obtained are as follows,
We all know that central diffrencing scheme is always faster and posses a higher rate in order to reach the true value since it takes the information from both the sides, where as the forward or backward schemes take information from one direction.
Even though in many cases FDE and BDE are choosen because in many situations we cannot have information from both the directions,hence uni-directional FDE or BDE play a major role.