Week 3.5 - Deriving 4th order approximation of a 2nd order derivative using Taylor Table method
Deriving 4th order approximation of a 2nd order derivative using Taylor Table method
The 4rth order approximation and 2nd order derivativs equation for three different scheme is,
`text(Central Differncing Scheme)`
`(∂^2 f)/(∂x^2 )=af(i)+bf(i+1)+cf(i+2)+df(i-1)+ef(i-2)`
`text( After solving taylor table using central differncing for the the coefficients, the value of a,b,c,d,e is,)`
`a=-1 ,b=16, c=-30,d=16,e=-1`
`text(then the final equation for the central differencing is,)`
`(∂^2 f)/(∂x^2 )=(-f(i)+16f(i+1)-30f(i+2)+16f(i-1)-f(i-2))/(12*Deltax^2)`
`text(Skewed Forward Differencing Scheme)`
`(∂^2 f)/(∂x^2 )=af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+gf(i+5)`
`text(After solving taylor table using forward differncing for the the coefficients, the value of a,b,c,d,e,g is,)`
`a=45,b=-154, c=214,d=-156,e=61,g=-10`
`text(then the final equation for the forward differencing is,)`
`(∂^2 f)/(∂x^2 )=(45f(i)-154f(i+1)+214f(i+2)-156f(i+3)+61f(i+4)-10f(i+5))/(12*Deltax^2)`
`text(Skewed Backward Differncing Scheme)`
`(∂^2 f)/(∂x^2 )=af(i)+bf(i-1)+cf(i-2)+df(i-3)+ef(i-4)+gf(i-5)`
`text(After solving taylor table using backward differncing for the the coefficients, the value of a,b,c,d,e,g is,)`
`a=45,b=-154, c=214,d=-156,e=61,g=-10`
`text(then the final equation for the backward differencing is,)`
`(∂^2 f)/(∂x^2 )=(45f(i)-154f(i-1)+214f(i-2)-156f(i-3)+61f(i-4)-10f(i-5))/(12*Deltax^2)`
Code
close all
clear all
clc
%function is f(x)= exp(x)*cos(x)
x=pi/3;
dx= linspace(pi/4000,pi/40,20);
% analytical derivatives
analytical_derivatives = -2*exp(x)*sin(x);
for i=1:length(dx)
[central_diff(i),forward_diff(i),backward_diff(i),central_error(i),forward_error(i),backward_error(i)]=taylor_table_scheme(x,dx(i),analytical_derivatives);
end
%plot
figure(1)
plot (dx, central_error, 'r-');
hold on
plot (dx, backward_error, 'g-');
hold on
plot (dx, forward_error, 'b-');
xlabel('dx');
ylabel('error');
legend('Central difference scheme', 'Forward difference scheme', 'Backward difference scheme');
title('Comparision of errors for a 2nd order derivative 4th order approximation)');
%log plot between log(dx) vs log(eror)
figure(2)
loglog(dx,central_error,'r-');
hold on
grid on
loglog(dx,forward_error,'b-');
hold on
grid on
loglog (dx,backward_error,'g-');
xlabel('log (dx)');
ylabel('log(error)');
legend('Central difference scheme', 'Forward difference scheme', 'Backward difference scheme');
title('Comparision of errors for a 2nd order derivative 4th order approximation');
Function code
function[central_diff,forward_diff,backward_diff,central_error,forward_error,backward_error]=taylor_table_scheme(x,dx,analytical_derivatives)
%Numerical derivative
%Central difference scheme
central_diff=((-1*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))+(-1*exp(x-(2*dx))*cos(x-(2*dx))))./(12*(dx.^2));
%skewed forward dcifference scheme
forward_diff = ((45*exp(x)*cos(x))+(-154*exp(x+dx)*cos(x+dx))+(214*exp(x+(2*dx))*cos(x+(2*dx)))+(-156*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));
%skewed backward differnce scheme
backward_diff= ((45*exp(x)*cos(x))+(-154*exp(x-dx)*cos(x-dx))+(214*exp(x-(2*dx))*cos(x-(2*dx)))+(-156*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 of numerical descretization
central_error = abs(analytical_derivatives-central_diff);
forward_error = abs(analytical_derivatives-forward_diff);
backward_error = abs(analytical_derivatives-backward_diff);
end
Graph
Figure(1)

Figure(2)

Conclusion
1. From the above figure, the error in central diffrence method isvery less than the skewed method. It has faster convergence rate that's why central difference scheme is prefered over skewed scheme.
2. Forward and backward diffrence scheme both gives same results in terms of error even in very small time interval.
3. central difference scheme can not be assign at boundary nodes due to the absence of forward and backward difference nodes at the same time. In such cases, forward and backward scheme is used depending on the direction of flow of infomation.