Project

Use of Taylor table method and Matlab code to derive the 4th order approximations of the second-order derivative by the different methods

In this challenge, we are going to derive the 4th order approximations of the second-order derivative by the following methods.

The methods we are going to use are:

1. Central Difference

2. Skewed right-sided difference

3. Skewed left-sided difference

First, we use here is

1. Central Difference:

Numerical stencil we will consider here is

`(i-2)--(i-1)--(i)--(i+1)--(i+2)`

2nd order derivative can be written as

`(d^2u)/(dx)^2=a*f(i-2)+b*f(i-1)+c*f(i)+d*f(i+1)+e*f(i+2)`------equation 1

Taylor's Table we can compute is as

As we are dealing with five unknowns we have five equations to find the unknowns which can be done by using simple MATLAB code.

`A=[[1,1,1,1,1],[-2,-1,0,1,2],[2,1/2,0,1/2,2],[-8/6,-1/6,0,1/6,8/6],[16/24,1/24,0,1/24,16/24]]`

`B=[[0,0,1,0,0]]`

X=A/B

After solving the values we will get is

a=-0.0833

b=1.3333

c=-2.5

d=1.3333

e=-0.0833

We can clearly see that a=e and b=d

Expansion of equation we have

by putting all the values of all variable in equation 1.

we can rewrite equation 1 as

`(d^2u)/(dx)^2=((-0.0833*f(i-2)+1.3333*f(i-1)+2.5*f(i)+1.3333*f(i+1)-0.0833*f(i+2))/dx^2)+ O(x^4)`

where, O(x^4)- Truncation error of 4th order scheme

Above equation represents 4th order approximation scheme of 2nd order derivative by CENTRAL DIFFERENCE METHOD.

2. Skewed right-sided difference:

Numerical stencil we will consider here is

`(i)---(i+1)---(i+2)---(i+3)---(i+4)`

The equation we need to expand is

`(d^2u)/(dx)^2=a*f(i)+b*f(i+1)+c*f(i+2)+d*f(i+3)+e*f(i+4)`------equation 2

Taylor's table expansion will be as

we are going to find out five unknowns with five equations as

`A=[[1,1,1,1,1],[0,1,2,1,4],[0,1/2,2,1/2,16/2],[0,1/6,8/6,1/6,64/6],[0,1/24,16/24,81/24,256/24]]`

`B=[[0,0,1,0,0]]`

X=A/B

the values for unknowns we wil get is

a=2.9167

b=-8.6667

c=9.5

d=-4.6667

e=0.9167

Put all above values in equation 2

`(d^2u)/(dx)^2=((2.9167*f(i)-8.6667*f(i+1)+9.5*f(i+2)-4.6667*f(i+3)+0.9167*f(i+4))/dx^2)+ O(x^4)`

where, O(x^4)- Truncation error of 4th order scheme

The above equation represents the 4th order approximation scheme of 2nd order derivative by Skewed right-sided difference.

3. Skewed left-sided difference:

Numerical stencil we will consider here is

`(i-4)---(i-3)---(i-2)---(i-1)---(i)`

The equation we need to expand is

`(d^2u)/(dx)^2=a*f(i)+b*f(i-1)+c*f(i-2)+d*f(i-3)+e*f(i-4)`------equation 3

Taylor\'s table expansion will be as

we are going to find out five unknowns with five equations as

`A=[[1,1,1,1,1],[0,-1,-2,-1,-4],[0,1/2,2,1/2,16/2],[0,-1/6,-8/6,-1/6,-64/6],[0,1/24,16/24,81/24,256/24]]`

`B=[[0,0,1,0,0]]`

X=A/B

the values for unknowns we wil get is

a=2.9167

b=-8.6667

c=9.5

d=-4.6667

e=0.9167

Put all above values in equation 2

`(d^2u)/(dx)^2=((2.9167*f(i)-8.6667*f(i-1)+9.5*f(i-2)-4.6667*f(i-3)+0.9167*f(i-4))/dx^2)+ O(x^4)`

where, O(x^4)- Truncation error of 4th order scheme

The above equation represents the 4th order approximation scheme of 2nd order derivative by Skewed left-sided difference.

Function codes for each scheme are as follows to get the idea of the behavior of error for known values of x and dx.

%Function code for Central Difference:

function e1= CD_second_order_derivative(x,dx)

analytical_derivative=-exp(x)*2*sin(x);

a=exp(x-2*dx)*cos(x-2*dx);
b=exp(x-1*dx)*cos(x-1*dx);
c=exp(x)*cos(x);
d=exp(x+1*dx)*cos(x+1*dx);
e=exp(x+2*dx)*cos(x+2*dx);

% Central differencing for fourth-order approximation of 2nd Order
% derivative

central_differencing=(-0.08333*a+1.3333*b+2.5*c+1.3333*d-0.08333*e)/dx^2;

e1=abs(analytical_derivative-central_differencing);

end

%Function code for Skewed right-sided difference:

function e2= SRSD_second_order_derivative(x,dx)

analytical_derivative=-exp(x)*2*sin(x);

c=exp(x)*cos(x);
d=exp(x+1*dx)*cos(x+1*dx);
e=exp(x+2*dx)*cos(x+2*dx);
f=exp(x+3*dx)*cos(x+3*dx);
g=exp(x+4*dx)*cos(x+4*dx);


% Skewed Right side difference for fourth-order approximation of 2nd Order
% derivative

SRSD_differencing=(2.9167*c-8.6667*d+9.5*e-4.6667*f+0.9167*g)/dx^2;

e2=abs(analytical_derivative-SRSD_differencing);

end

%Function code for Skewed left-sided difference:

function e3= SLSD_second_order_derivative(x,dx)

analytical_derivative=-exp(x)*2*sin(x);


j=exp(x-4*dx)*cos(x-4*dx);
h=exp(x-3*dx)*cos(x-3*dx);
a=exp(x-2*dx)*cos(x-2*dx);
b=exp(x-1*dx)*cos(x-1*dx);
c=exp(x)*cos(x);


% Central differencing for fourth-order approximation of 2nd Order
% derivative

SLSD_differencing=(2.9167*c-8.6667*b+9.5*a-4.6667*h+0.9167*j)/dx^2;

e3=abs(analytical_derivative-SLSD_differencing);

end

Main Program file:

clear all
close all
clc

% function f(x)=exp(x)*cos(x)
% Double derivative of function f\"(x)= -exp(x)*2*sin(x)

% lets assume
x=pi/2;
dx=linspace(pi/4,pi/400,10);

% Analytical D.derivative of function is
analytical_derivative=-exp(x)*2*sin(x);

% For 4th order approximation

for i=1:length(dx)

    j(i)=exp(x-4.*dx(i))*cos(x-4.*dx(i));
    h(i)=exp(x-3.*dx(i))*cos(x-3.*dx(i));
    a(i)=exp(x-2.*dx(i))*cos(x-2.*dx(i));
    b(i)=exp(x-1.*dx(i))*cos(x-1.*dx(i));
    c(i)=exp(x)*cos(x);
    d(i)=exp(x+1.*dx(i))*cos(x+1.*dx(i));
    e(i)=exp(x+2.*dx(i))*cos(x+2.*dx(i));
    f(i)=exp(x+3.*dx(i))*cos(x+3.*dx(i));
    g(i)=exp(x+4.*dx(i))*cos(x+4.*dx(i));


    ERROR1(i)=CD_second_order_derivative(x,dx(i))
    ERROR2(i)=SRSD_second_order_derivative(x,dx(i))
    ERROR3(i)=SLSD_second_order_derivative(x,dx(i))

    % FORWARD DIFFERENCING

    forward_differencing=(c-2*d+e)/dx(i).^2;
    ERROR_FDS(i)=abs(forward_differencing(i)-analytical_derivative)

    % BACKWARD DIFFERENCING

    backward_differencing=(c-2*b+a)/dx(i).^2;
    ERROR_BDS(i)=abs(backward_differencing(i)-analytical_derivative)

end

figure(1)
loglog(dx,ERROR1,\'b\',\'linewidth\',1.5)
hold on
loglog(dx,ERROR2,\'r\',\'linewidth\',1.5)
hold on
loglog(dx,ERROR3,\'y\',\'linewidth\',1.5)
hold on
xlabel(\'dx\');
ylabel(\'Absolute Errors\');
title(\'Comparision between CD/SRSD/SLSD schemes of 4th order approximation of 2nd order derivative\')
legend(\'CD\',\'SRSD\',\'SLSD\');

figure(2)
loglog(dx,ERROR1,\'b\',\'linewidth\',1.5)
hold on
loglog(dx,ERROR2,\'r\',\'linewidth\',1.5)
hold on
loglog(dx,ERROR3,\'y\',\'linewidth\',1.5)
hold on
loglog(dx,ERROR_FDS,\'k--\',\'linewidth\',0.5)
hold on
loglog(dx,ERROR_BDS,\'k-o\',\'linewidth\',0.5)
hold on
xlabel(\'dx\');
ylabel(\'Absolute Errors\');
title(\'Comparision between FDS/BDS & CD/SRSD/SLSD schemes of 4th order approximatio of 2nd order derivative\')
legend(\'CD\',\'SRSD\',\'SLSD\',\'FDS\',\'BDS\');

The plots that we have is as below:

From the graph, we can conclude that:

1. Initially, for the higher value of DX, SRSD gives a higher value of error comparatively to other approximations schemes. but CD gives consistently lower value for the error.

2. After some value of DX, errors computing through SRSD and SLSD coming closer but the value of error for CD goes on increasing but still it is able to maintain an error lower than other schemes.

3. After many manipulations in the value of X, we will have completely different results. In that case, CD scheme error drastically gives rise whereas SLSD & SRSD able to give very much closer value to exact value.

4. Talking about FDS & BDS, both are very consistent in their error and closer to each other although they are providing higher value of error than the rest of schemes. They are having very lower accuracy but high precision we can expect.

5. When we compare CD & SRSD/SLSD, as the value of X changing nature of the CD curve will become more sensitive i.e. we can\'t expect greater accuracy. But if we consider the same with SRSD/SLSD initially they will have a higher value for error, as DX is decreasing it will start giving more accurate results and the curve will tend to move towards the exact result.

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.

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.