Derivation of 4th order approximations of the second order derivative using Taylor Table method and MATLAB
AIM:
To write and execute a MATLAB program for 4th order approximations of the second order derivative using Taylor's Table method of the function exp(x)*cos(x) and compare the results with central difference approcimation and skewed schemes.
Curiosity: Why is higher order approximation desirable at the first place?
For the same mesh, we can get better accuracy, reduced number of mesh points, reduced computational time and storage. Therefore, any computational that requires resolution of small length scales(i.e. important physics occurs at small length scales) or has very sharp spatial gradients is an ideal candidate for higher order differencing.
examples includes:
- Direct Numerical simulation (DNS) of turbulent flow.
- Large Eddy Simulation (LES) of turbulent flow.
- Acoustic calculations.
- Electromagnetic calculation (Maxwell Equation)
Introduction:
In this demostration we will evaluate the 4th order approximation of diffrent schemes for second derivative of the function exp(x)*cos(x) and
- plot that will compare the absolute errors between the schemes,
- comparision of the errors with FDS abd BDS approximations
- To conclude, how skewed schemes are useful and benifit of it than central scheme
- MATLAB codes with required functions
Formulation for the Taylor table methos for CDS
`f''(x)=(af(x-2h)+bf(x-h)+cf(x)+df(x+h)+e(x+2h))/h^2`
Formulation for the Taylor table method of left Skewed schme
`f''(x) = (af(x)+bf(x-h)+cf(x-2h)+df(x-3h)+ef(x-4h)+ff(x-5h))/h^2`
Formulation for the Taylor table method of Right-Skewed schme
`f''(x)= (af(x)+bf(x+h)+cf(x+2h)+df(x+3h)+ef(x+4h)+ff(x+5h))/h^2`
Forward difference schme
`f''(x) = (af(x) +bf(x+h) +cf(x+2h))/h^2`
Backward difference schme
`f''(x)= (af(x) +bf(x-h) +cf(x-2h))/h^2`
The Taylor's Table is made by expanding the well known Taylor's Series. In above equations we need to find the coefficients a,b,c,d and e for each schemes by comparing the coefficents of taylor's Series expansion to LHS. To make our life little easier, I have inserted the MATLAB program for execution of these coeffiicients.
When solving: by X=A\B, we get the following results for different schemes'
1. 4th order CDS
a=-0.0833 b=1.333 c=-2.5 d=1.3333 e=-0.8333
2. 4th order Right sided skewed scheme
a=2.91667 b=-8.6667 c=9.5 d=-4.6667 e=0.91667
3. 4th order Left sided skewed scheme
a=0.91667 b=-4.6667 c=9.5 d=-8.6667 e=2.9167
Note: the values of approximations will solely depends on the step size we chose.
MATLAB PROGRAM
clear all
close all
clc
x = pi/3;
dx = linspace(0.01,1,15);
for i=1:length(dx)
err_secondOrderForward(i) = second_order_forward_approx(x,dx(i));
err_secondOrder(i) = second_order_approx(x,dx(i));
err_centralDiff(i) = fourth_central(x,dx(i));
err_skewedRight(i) = fourth_skewed_right(x,dx(i));
err_skewedLeft(i) = fourth_skewed_left(x,dx(i));
end
loglog(dx,err_centralDiff,'r',dx,err_skewedRight,'b',dx,err_skewedLeft,'k',dx,err_secondOrderForward,'m',dx,err_secondOrder,'g');
title('Fourth order approximation');
grid on
xlabel('step size "dx"');
ylabel('Errors dx')
legend('Central','Right','Left','FDS','BDS')
Function for 4th order Central Difference scheme;
function err_centralDiff = fourth_central(x,dx)
% Analytical function = exp(x)*cos(x)
% f''(x) = -2*sin(x)*exp(x)
analytical_derivative = -2*sin(x)*exp(x);
%numerical derivative
central_diff = ((-0.0833*cos(x-2*dx)*exp(x-2*dx))+(1.3333*cos(x-dx)*exp(x-dx))+(-2.5*cos(x)*exp(x))+(1.3333*cos(x+dx)*exp(x+dx))+(-0.0833*cos(x+2*dx)*exp(x+2*dx)))/dx^2;
err_centralDiff = abs(analytical_derivative - central_diff);
end
Function for 4th order Right Skewed Difference Scheme;
function err_skewedRight = fourth_skewed_right(x,dx)
% Analytical function = exp(x)*cos(x)
% f''(x) = -2*sin(x)*exp(x)
analytical_derivative = -2*sin(x)*exp(x);
%numerical derivative
skewedRight = ((2.9167*cos(x)*exp(x))+(-8.667*cos(x+dx)*exp(x+dx))+(9.5*cos(x+2*dx)*exp(x+2*dx))+(-4.667*cos(x+3*dx)*exp(x+3*dx))+(0.9167*cos(x+4*dx)*exp(x+4*dx)))/dx^2;
err_skewedRight = abs(analytical_derivative - skewedRight);
end
Function for 4th order Left Skewed Difference Scheme;
function err_skewedLeft = fourth_skewed_left(x,dx)
% Analytical function = exp(x)*cos(x)
% f''(x) = -2*sin(x)*exp(x)
analytical_derivative = -2*sin(x)*exp(x);
%numerical derivative
skewedLeft = ((0.91667*cos(x)*exp(x))+(-4.667*cos(x-dx)*exp(x-dx))+(9.5*cos(x-2*dx)*exp(x-2*dx))+(-8.667*cos(x-3*dx)*exp(x-3*dx))+(2.9167*cos(x-4*dx)*exp(x-4*dx)))/dx^2;
err_skewedLeft = abs(analytical_derivative - skewedLeft);
end
Function for FDS;
function err_secondOrderForward = second_order_forward_approx(x,dx)
% Analytical function = exp(x)*cos(x)
% f''(x) = -2*sin(x)*exp(x)
analytical_derivative = -2*sin(x)*exp(x);
%numerical derivative
secondOrderForward = ((exp(x+2*dx)*cos(x+2*dx))+(-2*exp(x+dx)*cos(x+dx)) + (exp(x)*cos(x)))/dx^2;
err_secondOrderForward = abs(analytical_derivative - secondOrderForward);
end
Function for BDS;
function err_secondOrder = second_order_approx(x,dx)
% Analytical function = exp(x)*cos(x)
% f''(x) = -2*sin(x)*exp(x)
analytical_derivative = -2*sin(x)*exp(x);
%numerical derivative
secondOrder = ((exp(x-2*dx)*cos(x-2*dx))+(-2*exp(x-dx)*cos(x-dx)) + (exp(x)*cos(x)))/dx^2;
err_secondOrder = abs(analytical_derivative - secondOrder);
end
loglog plot of the results;
Conclusion;
As seen on the above plot, the central differencing scheme has very less error compared to other schemes, obviously it gathers informations from both neihbouring nodes. The FDS and BDS are for 1st order approximations of 1st derivative but Fourth-order Centred and Skewed are for fourth order approximation for second derivatives.
Why Skewed schemes are useful?
When we dont have any nodes at the boundary then skewed schemes are useful to approximate to improve the accuracy but when we have internal nodes available then Centered ones are good though.