Challenge

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

In this project, a function's second order derivative is calculated using a numerical approximation method and then its precise derivative is compared.

For a required order of precision and required scheme, the expression for a function's second order derivative is derived using Taylor's table approach. (Centre difference or right- or left-skewing scheme).

4th Order approximation of a 2nd Order derivative-

(1) Central difference scheme:

To derive for an error of order 4, 2nd order derivative can be approximated as-

making the coefficient of 2nd derivative of x as 1, the equation becomes-

While all other derivatives have a coefficient of zero and the sixth derivative of x has an unknown finite term (which is regarded as the error term), the coefficient of the second derivative of x is 1.

The power of delta(x) in the error term is therefore 4. This supports the estimate being of order 4.

The following is a description of Taylor's table for the Central Difference Scheme's 4th order approximation of the 2nd ord

er derivative:

The final equation is as follows after obtaining the coefficients for each term and solving the equations:

(2) Skewed forward difference scheme:

To derive for an error of order 4, 2nd order derivative can be approximated as-

making the coefficient of 2nd derivative of x as 1, the equation becomes-

While all other derivatives have a coefficient of zero and the sixth derivative of x has an unknown finite term (which is regarded as the error term), the coefficient of the second derivative of x is 1.

The power of delta(x) in the error term is therefore 4. This supports the estimate being of order 4.

Using the Skewed Forward Difference Scheme, Taylor's table for the 4th order approximation of the 2nd order derivative is:

Solving the equations, coefficients of each term is obtained and the final equation is as follows-

(3) Skewed backward difference scheme:

To derive for an error of order 4, 2nd order derivative can be approximated as-

making the coefficient of 2nd derivative of x as 1, the equation becomes-

While all other derivatives have a coefficient of zero and the sixth derivative of x has an unknown finite term (which is regarded as the error term), the coefficient of the second derivative of x is 1.

The power of delta(x) in the error term is therefore 4. This supports the estimate being of order 4.

The following is a description of Taylor's table for the Skewed Forward Difference Scheme's 4th order approximation of the 2nd order derivative:

Solving the equations, coefficients of each term is obtained and the final equation is as follows-

A Matlab programme was created to solve the algebraic equations and identify the coefficients of the equation.

coefficients.m

clc
clear all
close all

syms a b c d e g

% For skewed forward difference
eqn1 = a+b+c+d+e+g==0;
eqn2 = b+(2*c)+(3*d)+(4*e)+(5*g)==0;
eqn3 = b+(4*c)+(9*d)+(16*e)+(25*g)==2;
eqn4 = b+(8*c)+(27*d)+(64*e)+(125*g)==0;
eqn5 = b+(16*c)+(81*d)+(256*e)+(625*g)==0;
eqn6 = b+(32*c)+(243*d)+(1024*e)+(3125*g)==0;

% For skewed backward difference
%eqn1 = a+b+c+d+e+g==0;
%eqn2 = -b-(2*c)-(3*d)-(4*e)-(5*g)==0;
%eqn3 = b+(4*c)+(9*d)+(16*e)+(25*g)==2;
%eqn4 = -b-(8*c)-(27*d)-(64*e)-(125*g)==0;
%eqn5 = b+(16*c)+(81*d)+(256*e)+(625*g)==0;
%eqn6 = -b-(32*c)-(243*d)-(1024*e)-(3125*g)==0;

% For central difference
%eqn1 = a+b+c+d+e==0;
%eqn2 = b+(2*c)-(d)-(2*e)==0;
%eqn3 = b+(4*c)+(d)+(4*e)==2;
%eqn4 = b+(8*c)-(d)-(8*e)==0;
%eqn5 = b+(16*c)+(d)+(16*e)==0;

% solution for forward and backward difference
eqns=[eqn1 eqn2 eqn3 eqn4 eqn5 eqn6];
%solution for central difference
%eqns=[eqn1 eqn2 eqn3 eqn4 eqn5];
S=solve(eqns);

coeff_a = S.a
coeff_b = S.b
coeff_c = S.c
coeff_d = S.d
coeff_e = S.e
coeff_g = S.g

The main program compares the error obtained by various approximation schemes.

taylor_table_main.m

clc
clear all
close all

%Function is f(x)=exp(x)*cos(x)
x=pi/4;
dx=linspace(pi/20,pi/400,40);

%Exact derivative
exact_derivative=-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),exact_derivative);
    
end

figure(1)
loglog(dx,central_error,'r-')
hold on 
loglog(dx,forward_error,'b-')
hold on
loglog(dx,backward_error,'g-')
xlabel('log(dx)')
ylabel('log(error)')
legend('Central difference scheme','Forward difference scheme','Backward difference scheme','Location','best')
title('Comparision of errors for a 2nd order derivative 4th order approximation using different approximation schemes (on log-log scale)')

figure(2)
plot(dx,central_error,'r-')
hold on 
plot(dx,forward_error,'b-')
hold on
plot(dx,backward_error,'g-')
xlabel('dx')
ylabel('error')
legend('Central difference scheme','Forward difference scheme','Backward difference scheme','Location','best')
title('Comparision of errors for a 2nd order derivative 4th order approximation using different approximation schemes')

taylor_table_scheme.m

function[central_diff,forward_diff,backward_diff,central_error,forward_error,backward_error]=taylor_table_scheme(x,dx,exact_derivative)

%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 difference 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 difference 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));

%Errors of numerical discretization
central_error=abs(exact_derivative-central_diff);
forward_error=abs(exact_derivative-forward_diff);
backward_error=abs(exact_derivative-backward_diff);
end

The results are as follows:

CONCLUSIONS:

(1) The results show that the central difference method's error is significantly lower than that of the skewed techniques. Additionally, CDS is preferred over skewed approach because to its quicker convergence rates.

(2) For a small value of time-step, the results of the forward and backward difference methods are nearly identical in terms of inaccuracy.

(3) The lack of simultaneous forward and backward nodes prevents CDS from being used at boundary nodes. Depending on how the information is flowing, a forward or backward differencing system is employed in these situations.

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.