Project

Taylor Table for fourth order accurate finite difference approximation using Matlab.

Matlab Code to evaluate the second order derivative of the analytical function exp(x)*cos(x) by Central and Skewed Scheme.

Taylor's Table:

Taylor's series expansion of functions about a fixed point provides a means for constructing finite-difference operator of any order. A simple and straight forward way to carry out this is to construct Taylor's table. The table is constructed to simplify the algebra and reduce the operation time.

Taylor's table for 4th order approximations of the second order derivative:

1. Central difference Taylor's table

f(i)

Δx*f’(i)

Δx2*f’’(i)

Δx3*f’’’(i)

Δx4*fiv(i)

a*f(i)

a

-2*a

2*a

-(4/3) *a

(2/3) *a

b*f(i+1)

b

-b

b/2

-b/6

b/24

c*f(i+2)

c

0

0

0

0

d*f(i+3)

d

d

d/2

(d/6)

d/24

e*f(i+4)

e

2*e

2*e

(4/3) *e

(2/3 *e

2. Right-skewed difference Taylor's table

f(i)

Δx*f’(i)

Δx2*f’’(i)

Δx3*f’’’(i)

Δx4*fiv(i)

Δx5*fv(i)

Δx6*fvi(i)

a*f(i)

a

0

0

0

0

0

0

b*f(i+1)

b

b

b/2

b/6

b/24

b/120

b/720

c*f(i+2)

c

2*c

2 *c

(4/3) *c

(2/3) *c

(4/15) *c

(4/45) *c

d*f(i+3)

d

3 *d

(9/2) *d

(9/2) *d

(27/8) *d

(81/40) *d

(81/80) *d

e*f(i+4)

e

4 *e

8 *e

(32/3) *e

(32/3) *e

(128/15) *e

(256/45) *e

g*f(i+5)

g

5 *g

(25/2) *g

(125/6) *g

(625/24) *g

(625/24) *g

(3125/144) *g

3. Left-skewed difference Taylor's table

f(i)

Δx*f’(i)

Δx2*f’’(i)

Δx3*f’’’(i)

Δx4*fiv(i)

Δx5*fv(i)

Δx6*fvi(i)

a*f(i)

a

0

0

0

0

0

0

b*f(i+1)

b

-b

b/2

-b/6

b/24

-b/120

b/720

c*f(i+2)

c

-2*c

2 *c

-(4/3) *c

(2/3) *c

-(4/15) *c

(4/45) *c

d*f(i+3)

d

-3 *d

(9/2) *d

-(9/2) *d

(27/8) *d

-(81/40) *d

(81/80) *d

e*f(i+4)

e

-4 *e

8 *e

-(32/3) *e

(32/3) *e

-(128/15) *e

(256/45) *e

g*f(i+5)

g

-5 *g

(25/2) *g

-(125/6) *g

(625/24) *g

-(625/24) *g

(3125/144) *g

After deriving the Taylor table, the code is prepared to give the error analysis of all the schemes. The function is used to allow the code to run simultaneously and for loop is used for a range of dx values to compare the error. The graph will give an idea about which scheme is better to follow for the fourth order accurate approximation of 2nd order derivative. The log-log plot is also created to compare all the scheme for the same range of dx.

function out = fourth_central_approximation(x,dx)

% Analytical function = e^x*cos(x)
% Analytical 2nd order derivative = -2*e^x*sin(x)

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

% numerical derivative

A = [1 1 1 1 1; -2 -1 0 1 2; 2 0.5 0 0.5 2; -4/3 -1/6 0 1/6 4/3; 2/3 1/24 0 1/24 2/3];
C = [0;0;1;0;0];
B = inv(A)*C;
a = B(1);
b = B(2);
c = B(3);
d = B(4);
e = B(5);

fourth_order_central_differencing = ((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*dx)
                                
out = abs(fourth_order_central_differencing - analytical_derivative)
end

#########################################################

function out = fourth_R_skewed_approximation(x,dx)

% Analytical function = e^x*cos(x)
% Analytical 2nd order derivative = -2*e^x*sin(x)

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

% numerical derivative

A = [1 1 1 1 1 1; 0 1 2 3 4 5; 0 0.5 2 4.5 8 12.5; 0 (1/6) (4/3) 4.5 (32/3) (125/6);...
    0 (1/24) (2/3) (27/8) (32/3) (625/24); 0 (1/120) (4/15) (81/40) (128/15) (625/24)];
C = [0;0;1;0;0;0];
B = inv(A)*C;
a = B(1);
b = B(2);
c = B(3);
d = B(4);
e = B(5);
g = B(6);

fourth_order_R_skewed_differencing = ((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*dx)
                                
out = abs(fourth_order_R_skewed_differencing - analytical_derivative)
end

#################################################################

function out = fourth_L_skewed_approximation(x,dx)

% Analytical function = e^x*cos(x)
% Analytical 2nd order derivative = -2*e^x*sin(x)

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

% numerical derivative

A = [1 1 1 1 1 1; 0 -1 -2 -3 -4 -5; 0 0.5 2 4.5 8 12.5; 0 -(1/6) -(4/3) -4.5 -(32/3) -(125/6);...
    0 (1/24) (2/3) (27/8) (32/3) (625/24); 0 -(1/120) -(4/15) -(81/40) -(128/15) -(625/24)];

C = [0;0;1;0;0;0];
B = inv(A)*C;
a = B(1);
b = B(2);
c = B(3);
d = B(4);
e = B(5);
g = B(6);

fourth_order_L_skewed_differencing = ((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*dx)
                                
out = abs(fourth_order_L_skewed_differencing - analytical_derivative)
end

##########################################################

clear all
close all
clc

x = pi/3;
dx = linspace(pi/4,pi/4000,20);
 
 for i = 1:length(dx)
     
    fourth_central_error(i) = fourth_central_approximation(x,dx(i))
    fourth_R_skewed_error(i) = fourth_R_skewed_approximation(x,dx(i))
    fourth_L_skewed_error(i) = fourth_L_skewed_approximation(x,dx(i))
    
 end
figure(1)
plot(dx,fourth_central_error)
xlabel('dx')
ylabel('error')
title('Fourth order central difference')
figure(2)
plot(dx,fourth_R_skewed_error,'color','r')
xlabel('dx')
ylabel('error')
title('Fourth order Right skewed difference')
figure(3)
plot(dx,fourth_L_skewed_error,'color','k')
xlabel('dx')
ylabel('error')
title('Fourth order Left skewed difference')
figure(4)
loglog(dx,fourth_central_error)
hold on
loglog(dx,fourth_R_skewed_error,'color','r')
hold on
loglog(dx,fourth_L_skewed_error,'color','k')
legend('central scheme','Right skewed scheme','Left skewed scheme')
xlabel('dx')
ylabel('error')

1. dx vs error for the central difference.

2. dx vs error for the right-skewed difference.

3. dx vs error for the left-skewed difference.

4. Log-log plot for error comparison.

Observations:

1. The left skewed scheme seems to be more unstable compared to other schemes.

2. For a certain range of values, the error given by the central scheme is smaller than the other two schemes.

3. The central difference scheme is more stable as compared to the other two schemes and error is in fixed proportion with the dx.

Why skewed scheme?

Skewed schemes are used where the central difference scheme can't give enough accuracy. For eg. When we need to solve the flow from any boundary and when there are no cells either at left or right, skewed schemes can give a more accurate solution. With the central scheme, we have to approximate the cells, which can go wrong and it would take more time to converge the solution. If the scheme is higher order then we have to consider more imaginary cells beyond the boundary, and that can complicate the analysis.

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.