Challenge

Week 3.5 - Deriving 4th order approximation of a 2nd order derivative using Taylor Table method

Theory

AIM:

Derive the following 4th order approximations of the second-order derivative. 

1. Central difference

2. Skewed right-sided difference

3. Skewed left-sided difference

To evaluate the second-order derivative of the analytical function exp(x)*cos(x) and compare it with the 3 numerical approximations that you have derived.

Provide a plot that compares the absolute error between the above-mentioned schemes

Numerical schemes:

1. Fourth order  Central difference approximation of the second-order derivative of a function:

Consider a function f(x) whose numerical approximated second-order derivative is given by :

The order of accuracy for the central difference method is found as:

P = N - Q + 1

Where,

P is The order of accuracy 

N is the number of nodes in the stencil 

Q is the order of derivative 

Therefore the number of nodes to be considered for a fourth-order accurate approximation of a second-order derivative by central difference method is given by:

4 = N -2+1

N = 5.

For a central difference method, a function at a node can be evaluated by considering the information on both sides in the numerical stencil. Hence the numerical stencil of a function f(x) with fourth-order approximation with 5 nodes at appointing ‘i’ can be represented as:

Let the hypothetical second order derivative of f(x) at ‘i’ considering the fourth order central difference approximation in terms of information from surrounding nodes be written as:

..(1)

Where a, b, c, d, e are coefficients of approximations.

Let ‘dx’ be the discretisation size , then from Taylor series expansion of the first term a* f(i-2) gives,

Simplifying the above equation,

 Similarly Taylor series expansion of the second term b* f(i-1) gives,

Similarly Taylor series expansion of the fourth term d* f(i+1) gives,

Similarly Taylor series expansion of the fifth term e* f(i+2) gives,

also, c*f(i) = c*f(i)

All the above expansions can be represented in tabular form called Taylor’s table as shown below:

Since only the second derivative of the function is to be found out, the sum of the coefficients of    

 

alone equal to 1 and rest all are zero.  Since we require only 5 equations to find the 5 coefficients, the last two columns are ignored for now.

Therefore considering the coefficients of the five columns 

we have:

a+b+c+d+e = 0

-2a-b+d+2e = 0

4a/2+b/2+d/2+4e/2 =1

-8a/6-b/6+d/6+8e/6 = 0

2a/3+b/24+d/24+2e/3 =0

Converting the above equations, into matrix form:

Solving the matrix by inversion leads to:

a =-0.08333333333333333333333333

b = 1.33333333333333333333333333

c =-2.50000000000000000000000000

d =1.333333333333333333333333333

e = -0.0833333333333333333333333

hence a= e , b=d

substituting these values in the Taylor table leads to:

sum of column 2 = a+b+c+d+e =0

similarly ,

sum of  coefficients of column 3 = -2a-b+d+2e = -2a-b+b+2a = 0

sum of coefficients of column 4 = 4a/2+b/2+d/2+4e/2= 4a/2+b/2+b/2+4a/2 =4a+b = 4(-0.0833)+1.3333= 1

sum of coefficients  of column 5 = -8a/6-b/6+d/6+8e/6  = -8a/6-b/6+b/6+8a/6 = 0

sum of coefficients of column 6 = 2a/3+b/24+d/24+2e/3 = 2a/3+b/24+b/24+2a/3 = 0

sum of coefficients of column 7 = (-4a/15)+(-b/120)+(d/120)+(4e/15) =(-4a/15)+(-b/120)+(b/120)+(4a/15) =0

sum of coefficients of column 8 = (4a/45)+(b/720)+(d/720)+(4e/45) =(4a/45)+(b/720)+(b/720)+(4a/45) = 8a/45+b/360

Rewriting equation (1) in terms of Taylor series expansion of individual terms by substituting the coefficient values of a, b, c, d, e leads to:

Dividing both sides by   and rewriting as:

Hence, the above equation represents the fourth order central difference approximation for 

and the term  represents truncation error of fourth order. Therefore,

..(2)

Substituting the values a, b, c, d, e in above equation we can find the fourth order central difference approximation of second derivative of a function.

2. Skewed right-sided difference method:

The order of accuracy for the Skewed right-sided method is found as:

P = N - Q 

Where,

P is The order of accuracy 

N is the number of nodes in the stencil 

Q is the order of derivative 

Therefore the number of nodes to be considered for a fourth-order accurate approximation of a second-order derivative by Skewed right-sided method is given by:

4 = N-2

N = 6.

For a Skewed right-sided method, a function at a node can be evaluated by considering the information on the right side in the numerical stencil. Hence the numerical stencil of a function f(x) with fourth-order approximation with 6 nodes at a point  ‘i’ can be represented as:

The hypothetical second order derivative of function f(x) at ‘i’ considering the fourth order right sided skewed difference approximation in terms of information from right side nodes be written as:

..(3)

Let g* f(i) = g* f(i),

Let ‘dx’ be the discretisation size then from Taylor series expansion of the second term h* f(i+1) gives,

Simplifying the above equation,

Similarly Taylor series expansion of the third term j* f(i+2) gives,

Similarly Taylor series expansion of the fourth  term k* f(i+3) gives,

Similarly Taylor series expansion of the fifth term l* f(i+4) gives,

Similarly Taylor series expansion of the sixth term m* f(i+5) gives,

All the above expansions can be represented in tabular form called Taylor’s table as shown below:

Since only the second derivative of the function is to be found out, the sum of the coefficients of.  

 

alone equal to 1 and rest all are zero.  Since we require only 6 equations to find the 6 coefficients, the last one columns is ignored for now.

Therefore considering the coefficients of the six columns 

we have:

g+h+j+k+l+m =0

h+2j+3k+4l+5m = 0

h/2 + 4j/2 +9k/2+16l/2+25m/2 = 1

h/6+8j/6+27k/6+64l/6+125m/6 =0

h/24+2j/3+81k/24+256l/24+625m/24=0

h/120+4j/15+81k/40+128l/15+3125m/120=0

Converting the above equations, into matrix form:

Solving the matrix by inversion leads to:

  g =  3.750000000000000000000000

  h =  -12.8333333333333333333333

  j =  17.83333333333333333333333

  k = -13.00000000000000000000000

  l =  5.083333333333333333333333

  m =  -0.83333333333333333333333

substituting these values in the Taylor table leads to:

sum of column 2 coefficients = g+h+j+k+l+m = 0

sum of column 3 coefficients = h+2j+3k+4l+5m = 0

sum of column 4 coefficients = h/2 + 4j/2 +9k/2+16l/2+25m/2 = 1

sum of column 5 coefficients = h/6+8j/6+27k/6+64l/6+125m/6 = 0

sum of column 6 coefficients = h/24+2j/3+81k/24+256l/24+625m/24= 0

sum of column 7 coefficients = h/120+4j/15+81k/40+128l/15+3125m/120 = 0

sum of column 8 coefficients = h/720+4j/45+81k/80+256l/45+3125m/144 = non zero number

Rewriting equation (5) in terms of Taylor series expansion of individual terms by substituting the coefficient values of g, h, j, k, l, m leads to:

Dividing both sides by   and rewriting as:

Hence, the above equation represents the fourth order skewed right sided difference approximation for and the term  represents truncation error of fourth order. Therefore,

..(4)

3. Skewed left-sided difference method:

The order of accuracy for the Skewed left sided method is found as:

P = N - Q 

Where,

P is The order of accuracy 

N is the Number of nodes in the stencil 

Q is the order of derivative 

Therefore the number of nodes to be considered for a fourth order accurate approximation of a second order derivative by Skewed left sided method is given by:

4 = N-2

N = 6.

For a Skewed left-sided method, a function at a node can be evaluated by considering the information on the left side in the numerical stencil. Hence the numerical stencil of a function f(x) with fourth-order approximation with 6 nodes at  point ‘i’ can be represented as:

The hypothetical second order derivative of function f(x) at ‘i’ considering the fourth order left sided skewed difference approximation in terms of information from left side nodes be written as:

..(5)

Let t* f(i) = t* f(i),

Let ‘dx’ be the discretisation size then from Taylor series expansion of the first term n* f(i-5) gives,

Simplifying the above equation,

Similarly Taylor series expansion of the second term p*f(i-4) gives,

Similarly Taylor series expansion of the third  term q*f(i-3) gives,

Similarly Taylor series expansion of the fourth term r* f(i-2) gives,

Similarly Taylor series expansion of the fifth term s* f(i-1) gives,

All the above expansions can be represented in tabular form called Taylor’s table as shown below:

Since only the second derivative of the function is to be found out, the sum of the coefficients of  

alone equal to 1 and rest all are zero.  Since we require only 6 equations to find the 6 coefficients, the last one columns is ignored for now.

Therefore, considering the coefficients of the six columns 

we have:

n+p+q+r+s+t =0

-5n-4p-3q-2r-s = 0

25n/2+16p/2+9q/2+4r/2+s/2 = 1

-125n/6-64p/6-27q/6-8r/6-s/6=0

625n/24+256p/24+81q/24+2r/3+s/24 =0

-3125n/120-128p/15-81q/40-4r/15-s/120=0

Converting the above equations, into matrix form:

Solving the matrix by inversion leads to:

n = -0.833333333333333333333333

 p = 5.0833333333333333333333333

 q = -13.00000000000000000000000

 r = 17.833333333333333333333333

 s = -12.83333333333333333333333

 t = 3.7500000000000000000000000

substituting these values in the Taylor table leads to:

sum of column 2 coefficients = n+p+q+r+s+t = 0

sum of column 3 coefficients =  -5n-4p-3q-2r-s = 0

sum of column 4 coefficients = 25n/2+16p/2+9q/2+4r/2+s/2 = 1

sum of column 5 coefficients = -125n/6-64p/6-27q/6-8r/6-s/6=0

sum of column 6 coefficients = 625n/24+256p/24+81q/24+2r/3+s/24 =0

sum of column 7 coefficients = -3125n/120-128p/15-81q/40-4r/15-s/120=0

sum of column 8  coefficients = 3125n/144+256p/45+81q/80+4r/45+s/720=non zero number

Rewriting equation (5) in terms of Taylor series expansion of individual terms by substituting the coefficient values of n, p, q, r, s, t leads to:

Dividing both sides by   and rewriting as:

Hence, the above equation represents the fourth order skewed left sided difference approximation for and the term  represents truncation error of fourth order. Therefore,

..(6)

II Objectives:

  1. To write a program in Matlab to evaluate the second-order derivative of the function exp(x)*cos(x) and compare the errors of the 3 numerical approximations derived for fourth-order accuracy.
  2. Plot the graph that compares the error for the numerical schemes derived.

MATLAB code

Function code for fourth_order_central_difference_approximation:

function [Error_FCDA] = central_diff(x,dx)

%analytical/exact second  derivative of function f(x)=exp(x)*cos(x)
Exact_Derivative_Function = -2*exp(x)*sin(x);

%numerical approximations of second derivative of a function

%Fourth order Central Difference Approximation (FCDA)

%f''(x) = ((a* f(i-2) +b*f(i-1) + c*f(i)+d*f(i+1) +e*f(i+2))/(dx)^2
%a,b,c,d,e are coefficients of approximation by Taylor's table method
a =-0.08333333333333333333333333
b = 1.33333333333333333333333333
c =-2.50000000000000000000000000
d =1.333333333333333333333333333
e = -0.0833333333333333333333333

FCDA = (a*exp(x-2*dx)*cos(x-2*dx)+b*exp(x-dx)*cos(x-dx)+c*exp(x)*cos(x)+d*exp(x+dx)*cos(x+dx)+e*exp(x+2*dx)*cos(x+2*dx))/(dx^2)

%Errors in fourth order central difference approximation
Error_FCDA = abs(FCDA-Exact_Derivative_Function);

end

Function code for fourth_order_skewed_right_sided_difference_approximation:

function [Error_FSRSDA] =  skewed_right_sided_diff(x,dx)

%analytical/exact second  derivative of function f(x)=exp(x)*cos(x)
Exact_Derivative_Function = -2*exp(x)*sin(x);

%numerical approximations of second derivative of a function
%Fourth order Skewed Right Sided Difference Approximation (FSRSDA)
%f''(x) = ((g* f(i)) +(h*f(i+1)) + (j*f(i+2))+k*f(i+3) +l*f(i+4)+m*f(i+5))/(dx)^2

%g,h,j,k,l,m are coefficients of approximation by Taylor's table method
  g =  3.750000000000000000000000
  h =  -12.8333333333333333333333
  j =  17.83333333333333333333333
  k = -13.00000000000000000000000
  l =  5.083333333333333333333333
  m =  -0.83333333333333333333333


FSRSDA = ((g*(exp(x)*cos(x)))+(h*(exp(x+dx)*cos(x+dx)))+(j*(exp(x+2*dx)*cos(x+2*dx)))+(k*(exp(x+3*dx)*cos(x+3*dx)))+(l*(exp(x+4*dx)*cos(x+4*dx)))+(m*(exp(x+5*dx)*cos(x+5*dx))))/(dx)^2

%Errors in FSRSDA approximation
Error_FSRSDA = abs(FSRSDA-Exact_Derivative_Function);


end

Function code for fourth_order_skewed_left_sided_difference_approximation:

function [Error_FSLSDA] =  skewed_left_sided_diff(x,dx)

%analytical/exact second  derivative of function f(x)=exp(x)*cos(x)
Exact_Derivative_Function = -2*exp(x)*sin(x);
%Fourth order Skewed Left Sided Difference Approximation(FSLSDA)
%f''(x) = ((n* f(i-5)) +(p*f(i-4)) + (q*f(i-3))+r*f(i-2) +s*f(i-1)+t*f(i))/(dx)^2

%n,p,q,r,s,t are coefficients of approximation by Taylor's table method
 n = -0.833333333333333333333333
 p = 5.0833333333333333333333333
 q = -13.00000000000000000000000
 r = 17.833333333333333333333333
 s = -12.83333333333333333333333
 t = 3.7500000000000000000000000

FSLSDA = ((n*(exp(x-5*dx)*cos(x-5*dx)))+(p*(exp(x-4*dx)*cos(x-4*dx)))+(q*(exp(x-3*dx)*cos(x-3*dx)))+(r*(exp(x-2*dx)*cos(x-2*dx)))+(s*(exp(x-1*dx)*cos(x-1*dx)))+(t*(exp(x)*cos(x))))/(dx)^2

%Errors in FSLSDA approximation
Error_FSLSDA = abs(FSLSDA-Exact_Derivative_Function);
end

MAIN MATLAB CODE

4th order approximations of the second-order derivative (second_order_derivative_maincode.m)

%Derive the following 4th order approximations of the second-order derivative. 
% Central difference
% Skewed right-sided difference
% Skewed left-sided difference
%To evaluate the second-order derivative of the analytical function exp(x)*cos(x) 
% and compare it with the 3 numerical approximations that you have derived.
clear all
close all
clc

%Input analytical function
%f(x) = exp(x)*cos(x);

%Input value of x
x = pi/3;

% Discretisation size in x 
dx = linspace(pi/4000,pi/40,20);

%Error in numerical approximations values over the entire dx range computed by calling the appropriate function handles
for i = 1:length(dx)
Error_FCDA(i) = central_diff(x,dx(i)); % Error in fourth order central difference approximation.
Error_FSRSDA(i) = skewed_right_sided_diff(x,dx(i)); % Error in fourth order skewed right sided difference approximation. 
Error_FSLSDA(i) = skewed_left_sided_diff(x,dx(i)); % Error in fourth order skewed left sided approximation.
end

%loglog plot of errors vs dx
figure(1)
loglog(dx,Error_FCDA,'g-*')
hold on
loglog(dx,Error_FSRSDA,'b-*')
loglog(dx,Error_FSLSDA,'r-*')
xlabel('Discretisation size (dx)')
ylabel('Error in approximation')
legend('Error FCDA','Error FSRSDA','Error FSLSDA')
title('Error for Different Approximation Schemes')

Code Explanation:

Step1: clear all –  command to clear the workspace.

Step 2: close all – command to close all opened previous plots.

Step 3: clc – command to clear the command window.

Step 4: Define the input value of 'x =pi/3' to be solved for the function.

Step 5: defining the discretisation range as array  'dx =(pi/4000, pi/40, 20)'.

Step 6: computing the numerical approximation error value of the second derivative of the given function by calling a function code 'central_diff' , 'skewed_right_sided_diff' and 'skewed_left_sided_diff' for fourth-order accurate entered, right-skewed, and left-skewed difference methods in a for loop to index over the entire range of dx. The function codes are given above:

The function codes take x and dx as input arguments and output the error in approximation with respect to the analytical/exact second derivative of the function 'f(x) = exp(x)*cos(x)'

The numerical approximations are found using the derived equations and coefficients from the Taylor's table method.

These function codes are called in the main program to calculate the errors for the entire range of dx using for loop.

step 7: plotting the error in approximations in the log-log plot.

Result

The result of the program is the comparison of error values of different approximation schemes for the second derivative of function exp(x)*cos(x) using fourth-order central difference, right-skewed difference and left-skewed difference methods with respect to exact derivative. The comparison is shown in the plot shown below:

It is observed from the above plot, the central difference scheme has the lowest error when compared to the right and left-skewed difference methods. The error values decrease rapidly as the discretisation size decreases up to some extent and thereafter, the error starts increasing. This change in the trend of the error is to the round off error occurring in computational limits. Hence, while choosing the discretisation size, care should be taken to account for round off errors due to computational limits.

Although the central difference method has the lowest error values when compared to skewed method, skewed methods are more useful because it can predict the derivative of a function at a point by knowing the values of the function only on one side of the point (left or right). This is of greater benefit when we have information about the function on only one side of the point in the numerical stencil. Whereas, the central difference method requires information on both sides of the point in the numerical stencil.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.