Challenge

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

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

1. Central difference

2. Skewed right-sided difference

3. Skewed left-sided difference

Objective :

The given equation analytical function exp(x)*cos(x)

solving:

Skewed right sided diffence :

The order of accuracy P = Number of nodes in the stencil N - order of derivative Q for one-sided/skewed schemes

where N = Q + P

N = 4+2

N = 6 no of node were we calculated

the equation

`Deltax^2 frac(d^2u)(dx^2) = af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+gf(i+5)`

Now we are using Tylor's table to find co-efficients

Tylor table for skewed right difference :

`f(i)` `Deltaxf^'(i)` `Deltax^2f^''(i)` `Deltax^3f^'''(i)` `Deltax^4f''''(i)` `Deltax^5f'''''(i)`
`af(i)` `a` `0` `0` `0` `0` `0`
`bf(i+1)` `b` `b` `b/2` `b/6` `b/24` `b/120`
`cf(i+2)` `c` `2c` `c4/2` `c8/6` `c16/24` `c32/120`
`df(i+3)` `d` `3d` `d9/2` `d27/6` `d81/24` `d243/120`
`ef(i+4)` `e` `4e` `e16/2` `e64/6` `e256/24` `e1024/120`
`gf(i+5)` `g` `5g` `g25/2` `g125/6` `g625/24` `g3125/120`

`af(i)`= `af(i)`

`bf(i+1)`= `b[f(i)+Deltaxf^'(i)+Deltafrac(x^2)2f^''(i)+Deltafrac(x^3)3f^'''(i)+Deltafrac(x^4)4f^''''(i)+Deltafrac(x^5)5f^'''''(i)]`

`cf(i+2)`= `c[f(i)+2Deltaxf^'(i)+2Deltafrac(x^2)2f^''(i)+2Deltafrac(x^3)3f^'''(i)+2Deltafrac(x^4)4f^''''(i)+2Deltafrac(x^5)5f^'''''(i)]`


`df(i+3)`= `d[f(i)+3Deltaxf^'(i)+3Deltafrac(x^2)2f^''(i)+3Deltafrac(x^3)3f^'''(i)+3Deltafrac(x^4)4f^''''(i)+3Deltafrac(x^5)5f^'''''(i)]`

`ef(i+4)`=`e[f(i)+4Deltaxf^'(i)+4Deltafrac(x^2)2f^''(i)+4Deltafrac(x^3)3f^'''(i)+4Deltafrac(x^4)4f^''''(i)+4Deltafrac(x^5)5f^'''''(i)]`

`gf(i+5)=g[f(i)+5Deltaxf^'(i)+5Deltafrac(x^2)2f^''(i)+5Deltafrac(x^3)3f^'''(i)+5Deltafrac(x^4)4f^''''(i)+5Deltafrac(x^5)5f^'''''(i)]`

`a+b+c+d+e+g = 0`

`b+2c+3d+4e+5g = 0`

`b/2+c4/2+d9/2+e16/2+g25/2 = 1`

`b/6+c8/6+d27/6+e64/6+g125/6 =0`

`b/24+c16/24+d81/24+e256/24+g625/24 = 0`

`b/120+c32/120+d243/120+e1024/120+g3125/120 = 0`

Using matrix method and computing required co-efficients

`frac(d^2u)(dx^2) = frac(af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+gf(i+5))(Deltax^2)`

substitute coefficients in above equation

in form of `f(x) = e^xcosx`

Skewed left sided difference :

The order of accuracy P = Number of nodes in the stencil N - order of derivative Q for one-sided/skewed schemes

where N = Q + P

N = 4+2

N = 6 no of nodes were we calculated

the equation

`Deltax^2 frac(d^2u)(dx^2) = af(i)+bf(i-1)+cf(i-2)+df(i-3)+ef(i-4)+gf(i-5)`

Now we are using Tylor's table to find co-efficients

Tylor table for skewed left sided difference :

`f(i)` `Deltaxf^'(i)` `Deltax^2f^''(i)` `Deltax^3f^'''(i)` `Deltax^4f^''''(i)` `Deltax^5f^'''''(i)`
`af(i)` `a` `0` `0` `0` `0` `0`
`bf(i-1)` `b` `-b` `b/2` `-b/6` `b/24` `-b/120`
`cf(i-2)` `c` `-2c` `c4/2` `-c8/6` `c16/24` `-c32/120`
`df(i-3)` `d` `-3d` `d9/2` `-d27/6` `d81/24` `-d243/120`
`ef(i-4)` `e` `-4e` `e16/2` `-e64/6` `e256/24` `-e1024/120`
`gf(i-5)` `g` `-5g` `g25/2` `g125/6` `g625/24` `-g3125/120`

`af(i)= af(i)`

`bf(i-1)=b[f(i)-Deltaxf^'(i)+Deltafrac(x^2)2f^''(i)-Deltafrac(x^3)3f^'''(i)+Deltafrac(x^4)4f^''''(i)-Deltafrac(x^5)5f^'''''(i)]`

`cf(i-2)=c[f(i)-2Deltaxf^'(i)+2Deltafrac(x^2)2f^''(i)-2Deltafrac(x^3)3f^'''(i)+2Deltafrac(x^4)4f^''''(i)-2Deltafrac(x^5)5f^'''''(i)]`

`df(i-3)=d[f(i)-3Deltaxf^'(i)+3Deltafrac(x^2)2f^''(i)-3Deltafrac(x^3)3f^'''(i)+3Deltafrac(x^4)4f^''''(i)-3Deltafrac(x^5)5f^'''''(i)]`

`ef(i-4)=e[f(i)-4Deltaxf^'(i)+4Deltafrac(x^2)2f^''(i)-4Deltafrac(x^3)3f^'''(i)+4Deltafrac(x^4)4f^''''(i)-4Deltafrac(x^5)5f^'''''(i)]`

`gf(i-5)=g[f(i)-5Deltaxf^'(i)+5Deltafrac(x^2)2f^''(i)-5Deltafrac(x^3)3f^'''(i)+5Deltafrac(x^4)4f^''''(i)-5Deltafrac(x^5)5f^'''''(i)]`

`a+b+c+d+e+g = 0`

`b-2c-3d-4e-5g = 0`

`b/2+c4/2+d9/2+e16/2+g25/2 = 1`

`-b/6-c8/6-d27/6-e64/6-g125/6 =0`

`b/24+c16/24+d81/24+e256/24+g625/24 = 0`

`-b/120-c32/120-d243/120-e1024/120-g3125/120 = 0``

Using matrix method and computing required co-efficients

`frac(d^2u)(dx^2) = frac(af(i)+bf(i-1)+cf(i-2)+df(i-3)+ef(i-4)+gf(i-5))(Deltax^2)`

substitute coefficients in above equation

in form of `f(x) = e^xcosx`

Central difference :

The order of accuracy P = Number of nodes in the stencil N - order of derivative Q + 1 for symmetric/central schemes

N = P+Q-1

N = 4+2-1

N = 5

no of nodes were we calculated

the equation `Deltax^2 frac(d^2u)(dx^2) = af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2)`

Now we are using Tylor's table to find co-efficients`Deltax^4f''''(i)`

Tylor table for Central difference :

`f(i)` `Deltaxf'(i)` `Deltax^2f''(i)` `Deltax^3f'''(i)` `Deltax^4f''''(i)`
`af(i-2)` `a` `-2a` `a4/2` `-a8/6` `a16/24`
`bf(i-1)` `b` `-b` `b1/2` `-b/6` `b/24`
`cf(i)` `c` `0` `0` `0` `0`
`df(i+1)` `d` `d` `d1/2` `d/6` `d/24`
`ef(i+2)` `e` `2e` `e4/2` `e8/6` `e16/24`

`af(i-2)=a[f(i)-2Deltaxf^'(i)+2Deltafrac(x^2)2f^''(i)-2Deltafrac(x^3)3f^'''(i)+2Deltafrac(x^4)4f^''''(i)]`

`bf(i-1)=b[f(i)-Deltaxf^'(i)+Deltafrac(x^2)2f^''(i)-Deltafrac(x^3)3f^'''(i)+Deltafrac(x^4)4f^''''(i)]`

`cf(i) = cf(i)`

`df(i+1)=d[f(i)+Deltaxf^'(i)+Deltafrac(x^2)2f^''(i)+Deltafrac(x^3)3f^'''(i)+Deltafrac(x^4)4f^''''(i)]`

`ef(i+2)=e[f(i)+2Deltaxf^'(i)+2Deltafrac(x^2)2f^''(i)+2Deltafrac(x^3)3f^'''(i)+2Deltafrac(x^4)4f^''''(i)]`

`a+b+c+d+e=0`

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

`4/2+b/2+0+d/2+4e/2=1`

`-a8/6-b/6+0+d/6+e8/6=0`

`16/24+b/24+0+d/24+16e/24=0`

Using matrix method and computing required co-efficients

`frac(d^2u)(dx^2) = frac(af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2))(Deltax^2)`

substitute coefficients in above equation

in form of `f(x) = e^xcosx`

Now we are coding a program to know errors,

Code :

Main code

close
clear
clc

% inputs
x = pi/3;
dx = linspace(pi/4,pi/4000,20);
analytical_derivative = -2*exp(x)*sin(x);

for i=1:length(dx)
    
    central_error(i)=central_differ(x,dx(i));
    skewed_right_error(i)=forward_differ(x,dx(i));
    skewed_left_error(i)=backward_differ(x,dx(i));
    
end
% plotting to know error 
loglog(dx,central_error,'color','g')
hold on
loglog(dx,skewed_right_error,'color','b')
loglog(dx,skewed_left_error,'color','r')
legend('central_error','skewed_right_error','skewed_left_error')
xlabel('dx')
ylabel('absolute error')
title('consistency of different scheme')

Function code for skewed right sided difference

function skewed_right_error = forward_differ(x,dx)
         %deriving eqution
        analytical_derivative = -2*exp(x)*sin(x);
        % computing co-efficients with numerical equation
        Numerical_derivative = ((3.7500)*exp(x)*cos(x)+(-12.8333)*exp(x+dx)*cos(x+dx)+(17.8333)*exp(x+2*dx)*cos(x+2*dx)+(-13.0000)*exp(x+3*dx)*cos(x+3*dx)+(5.0833)*exp(x+4*dx)*cos(x+4*dx)+(-0.8333)*exp(x+5*dx)*cos(x+5*dx))/(dx)^2;
        skewed_right_error = abs(Numerical_derivative-analytical_derivative);
end

Function code for skewed left sided difference

function skewed_left_error = backward_differ(x,dx)
          % deriving equation
        analytical_derivative = -2*exp(x)*sin(x);
        % computing co-efficients with numerical equation
        Numerical_derivative = ((3.7500)*exp(x)*cos(x)+(-12.8333)*exp(x-dx)*cos(x-dx)+(17.8333)*exp(x-2*dx)*cos(x-2*dx)+(-13.0000)*exp(x-3*dx)*cos(x-3*dx)+(5.0833)*exp(x-4*dx)*cos(x-4*dx)+(-0.8333)*exp(x-5*dx)*cos(x-5*dx))/((dx)^2);
        skewed_left_error = abs(Numerical_derivative-analytical_derivative);
end

Function code for central diffence

function central_error = central_differ(x,dx)
         %deriving eqution
        analytical_derivative = -2*exp(x)*sin(x);
        % computing co-efficients with numerical equation
        Numerical_derivative = ((-0.0833)*exp(x-2*dx)*cos(x-2*dx)+(1.3333)*exp(x-dx)*cos(x-dx)+(-2.5000)*exp(x)*cos(x)+(1.3333)*exp(x+dx)*cos(x+dx)+(-0.0833)*exp(x+2*dx)*cos(x+2*dx))/(dx)^2;
        central_error = abs(Numerical_derivative-analytical_derivative);
end

Result :

Central diffence<Right skewed difference~Left skewed difference

Why Skewed scheme is useful ?

Ans - The skewed scheme is useful because it is necessary to compute the grids at the corners also which central difference scheme is unable to do.

Conclusion :

Therefore finded the coefficients and plotted the graph to know erros ,here central difference is less erros compare to other two difference.

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.