Challenge

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

Numerical approximation using Taylor’s table

Stencil

Considering stencil for three different schemes

For central difference scheme = P+Q -1 =5

For Skewed difference scheme = P+Q=6

For central difference scheme

i-2(a) i-1(b) i(c) i+1(d) i+2(e)

For Skewed right difference scheme

i(a) i+1(b) i+2(c) i+3(d) i+4(e) i+5(g)

For Skewed left difference scheme

i-5(a) i-4(b) i-3(c) i-2(d) i-1(e) i(g)

  1. Central difference scheme

f(x)

fI(x)dx

fII(x)Δx2/2

fIII(x)Δx3/6

fIII(x)Δx4/24

f(x-2dx)

a

-2a

4a

-8a

16a

f(x-dx)

b

-b

b

-b

b

f(x)

c

0

0

0

0

f(x+dx)

d

d

d

d

d

f(x+2dx)

e

2e

4e

8e

16e

Summation of coefficients

0

0

1

0

0

Taylor’s table

Using matrix AX=B, we can find the value of a= -0.08333, b = 1.333, c=-2.5, d=1.333 and e =-0.08333

  1. Skewed right difference scheme

+gf(x+5dx)

Taylor’s table

f(x)

fI(x)dx

fII(x)Δx2/2

fIII(x)Δx3/6

fIII(x)Δx4/24

fIII(x)Δx5/120

f(x)

a

0

0

0

0

0

f(x+dx)

b

b

b

b

b

b

f(x+2dx)

c

2c

4c

8c

16c

32c

f(x+3dx)

d

3d

9d

27d

81d

243d

f(x+4dx)

e

4e

16e

64e

256e

1024e

f(x+5dx)

g

5g

25g

125g

625g

3125g

Summation of coefficients

0

0

1

0

0

0

Using matrix AX=B, we can find the value of a= 3.75, b =-12.833, c=17.833, d=-13, e = 5.0833 and g = -0.8333

  1. Skewed left difference scheme

+gf(x)

Taylor’s table

f(x)

fI(x)dx

fII(x)Δx2/2

fIII(x)Δx3/6

fIII(x)Δx4/24

fIII(x)Δx5/120

f(x-5dx)

a

-5a

-25a

-125a

-625a

-3125a

f(x-4dx)

b

-4b

-16b

-64b

-256b

-1024b

f(x-3dx)

c

-3c

-9c

-27c

-81c

-243c

f(x-2dx)

d

-2d

-4d

-8d

-16d

-32d

f(x-dx)

e

-e

-e

-e

-e

-e

f(x)

g

0

0

0

0

0

Summation of coefficients

0

0

1

0

0

0

Using matrix AX=B, we can find the value of a= 0.0032, b =0.4823, c=-2.9615, d=6.9582 e = -6.9775 and g = 2.4952

Matrices calculation for coefficients

clc

close all

clear all

% martix for central difference

A =[1 1 1 1 1;-2 -1 0 1 2;2 0.5 0 0.5 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 =inv(A)*B

% martix for skewed right difference

C =[1 1 1 1 1 1;0 1 2 3 4 5;0 0.5 2 9/2 8 25/2;0 1/6 8/6 27/6 64/6 125/6;0 1/24 16/24 81/24 256/24 625/24;0 1/120 32/120 243/120 1024/120 3125/120]

D =[0;0;1;0;0;0]

X2 =inv(C)*D

% martix for skewed left difference

E =[1 1 1 1 1 1;-5 -4 -3 -2 -1 0;25/2 8952 9/2 2 0.5 0;-125/6 -64/6 -27/6 -8/6 -1/6 0;625/4 256/24 81/24 16/24 1/24 0;-3125/120 -1024/120 -243/120 -32/120 -1/120 0]

F =[0;0;1;0;0;0]

X3 =inv(E)*F

%Function for fourth order derivative of the second oder

%Function for fourth order derivative of the second oder

%Central difference scheme

function central_difference_error=central_difference(x,dx)

%Analytical function f(x) = exp(x)*cos(x);

%fII(x)=2ex sinx

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

%coefficients from taylor’s table

a= -0.08333;

b= 1.333;

c= 2.5;

d= 1.333;

e= -0.08333;

%central differencing= af(x-2dx)+bf(x-dx)+cf(x)+df(x+dx)+ef(x+2dx) /dx2

%central difference fourth order approximation

Cdfoa=((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;

central_difference_error= abs(Cdfoa-analytical_derivative);

end

%Skewed right difference scheme

function skewed_right_difference_error=right_difference(x,dx)

%Analytical function f(x) = exp(x)*cos(x);

%fII(x)=2ex sinx

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

%coefficients from taylor’s table

a= 3.75;

b= -12.833;

c= 17.833;

d = -13;

e= -5.0833;

g= -0.8333;

%Skewed right differencing af(x)+bf(x+dx)+cf(x+2dx)+df(x+3dx)+ef(x+4dx)+gf(x+5dx)/dx2

%Skewed right difference fourth order approximation

srdfoa=((a*exp(x)*cos(x))+(b*exp(x+dx)*cos(x+dx))+(c*exp(x+2*dx)*cos(x+2*dx))+(d*exp(x+3*dx)*cos(x+3*dx)+(e*exp(x+4*dx)*cos(x+4*dx))+ (g*exp(x+5*dx)*cos(x+5*dx))))/dx^2

skewed_right_difference_error = abs(srdfoa-analytical_derivative);

end

%Skewed left difference scheme

function skewed_left_difference_error=left_difference(x,dx)

%Analytical function f(x) = exp(x)*cos(x);

%fII(x)=2ex sinx

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

%coefficients from taylor’s table

a= 0.0032;

b= 0.4823;

c= -2.9615;

d = 6.9582;

e= -6.9775;

g= 2.4952;

%Skewed right differencing af(x-5dx)+bf(x-4dx)+cf(x-3dx)+df(x-2dx)+ef(x-dx)+gf(x)/dx2

%Skewed right difference fourth order approximation

sldfoa=((a*exp(x-5*dx)*cos(x-5*dx))+(b*exp(x-4*dx)*cos(x-4*dx))+(c*exp(x-3*dx)*cos(x-3*dx))+(d*exp(x-2*dx)*cos(x-2*dx)+(e*exp(x-1*dx)*cos(x-1*dx))+ (g*exp(x)*cos(x+5))))/dx^2;

skewed_left_difference_error= abs(sldfoa-analytical_derivative);

end

.

%Program for calculation of fourth order approximation of second order

clc

close all

clear all

x=pi/3;

dx=linspace(pi/4,pi/40000,40);

%loop

for i=1:length(dx)

central_difference_error(i) = central_difference(x,dx(i));

skewed_right_difference_error(i) = right_difference(x,dx(i));

skewed_left_difference_error(i) = left_difference(x,dx(i));

end

%plotting results

figure(1)

plot(dx,central_difference_error,'linewidth',2)

hold on

grid on

plot(dx,skewed_right_difference_error,'r', 'linewidth',2)

hold on

plot(dx,skewed_left_difference_error,'g', 'linewidth',2)

xlabel('dx')

ylabel('error')

legend('central difference error','skewed right difference error','skewed left difference error')

title('normal result')

figure(2)

loglog(dx,central_difference_error,'linewidth',2)

hold on

grid on

loglog(dx,skewed_right_difference_error,'r', 'linewidth',2)

hold on

loglog(dx,skewed_left_difference_error,'g', 'linewidth',2)

xlabel('dx')

ylabel('error')

legend('central difference error','skewed right difference error','skewed left difference error')

title('log 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.