Challenge

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

function [central_diff,skewed_right,skewed_left,central_error,skewed_right_error,skewed_left_error] = taylor_scheme(x,dx)
% f(x) = exp(x)*cos(x)
% simplify the equation 
d1 = exp(x)*cos(x); %f(i)
d2 = exp(x+dx)*cos(x+dx); %f(i+1)
d3 = exp(x+(2*dx))*cos(x+(2*dx)); %f(i+2)
d4 = exp(x+(3*dx))*cos(x+(3*dx)); %f(i+3)
d5 = exp(x+(4*dx))*cos(x+(4*dx)); %f(i+4)
d6 = exp(x-(4*dx))*cos(x-(4*dx)); %f(i-4)
d7 = exp(x-(3*dx))*cos(x-(3*dx)); %f(i-3)
d8 = exp(x-(2*dx))*cos(x-(2*dx)); %f(i-2)
d9 = exp(x-dx)*cos(x-dx); %f(i-1)
d10 = exp(x-5*dx)*cos(x-5*dx); %f(i-5)
d11 = exp(x+5*dx)*cos(x+5*dx); %f(i+5)
% Analytical Derivative
exact_derivative = -2*exp(x)*sin(x);
% Central Difference (i-2,i-1,1,i+1,i+2)
% CD = (af(i-2) + bf(i-1) + cf(i) + df(i+1) + ef(i+2))/(dx^2)
c = [-1/12; 4/3; -5/2; 4/3; -1/12];
central_diff = (c(1)*d8 + c(2)*d9 + c(3)*d1 + c(4)*d2 + c(5)*d3)/(dx^2);
% Skewed Right Side Difference (i,i+1,1,i+2,i+3,i+4,i+5)
% SRD = (af(i) + bf(i+1) + cf(i+2) + df(i+3) + ef(i+4) + gf(i+5))/(dx^2)
m = [15/4; -77/6; 107/6; -26/2; 61/12; - 5/6];
skewed_right = (m(1)*d1 + m(2)*d2 + m(3)*d3 + m(4)*d4 + m(5)*d5 + m(6)*d11)/(dx^2);
% Skewed Left Side Difference (i-5,i-4,i-3,1,i-2,i-1,i)
% SLD = (af(i) + bf(i-1) + cf(i-2) + df(i-3) + ef(i-4) + gf(i-5))/(dx^2)
w = [15/4; -77/6; 107/6; -26/2; 61/12; - 5/6];
skewed_left = (w(1)*d1 + w(2)*d9 + w(3)*d8 +w(4)*d7 +w(5)*d6 +w(6)*d10)/(dx^2);
% Error of Numerical Discretization
central_error = abs(central_diff - exact_derivative);
skewed_right_error = abs(skewed_right - exact_derivative);
skewed_left_error = abs(skewed_left - exact_derivative);
end
close all
clc
% Input Variables
x = pi/3;
dx = linspace(pi/40,pi/400,100); % step size
% Iterative Loop 
for i = 1: length(dx)
    [central_diff(i),skewed_right(i),skewed_left(i),central_error(i),skewed_right_error(i),skewed_left_error(i)]=taylor_scheme(x,dx(i));
end
figure(1)
loglog(dx,skewed_left_error,'r')
hold on
loglog(dx,skewed_right_error,'b')
hold on
loglog(dx,central_error,'g')
legend('Skewed Right-Sided Difference','Skewed Left-Sided Difference','Central Difference')
xlabel('Length of Increment dx')
ylabel('Truncation Error')

The fourth order approximation is evaluated for the second derivative of the given function by implementing Taylor’s table method and compared with the exact derivative. The central difference, skewed right-sided, and skewed left-sided difference schemes are implemented.

Function: f(x) = exp(x)*cos(x)

Derivative: f’’(x) = -2*exp(x)*sin(x)

Number of Nodes: Central Difference – 5 nodes, Skewed Right Difference & Skewed Left Difference – 6

Skewed Scheme

The system which doesn’t contain sufficient information on both the side of the point of interest. In such cases, we can’t use central differencing scheme. Therefore, skewed scheme is used. The central differencing can’t be employed at the boundary nodes due to unavailability of right or left nodes at the same time. And here skewed schemes are used to approximate to improve the accuracy.

Formulation of Taylor’s Table Method:

Central Difference:

(`partial`^2 u)/( `partial`x^2) = af(i-2) + bf(i-1) + cf(i) + df(i+1) + ef(i+2)

Coefficients

f(i)

Δxf’(i)

Δx^2f’’(i)

Δx^3f’’’(i)

Δx^4f’’’’(i)

f(i-2)

a

-2a

4a/2

-8a/6

16a/24

f(i-1)

b

-b

b/2

-b/6

b/24

f(i)

c

0

0

0

0

f(i+1)

d

d

d/2

d/6

d/24

f(i+2)

e

2e

4e/2

-8e/6

16e/24

Sum of Coefficients

0

0

1

0

0


Solving the following table, we get the linear equation which is evaluated using the matrix inversion method.

The coefficients are as:

a = -0.0833

b = 1.3333

c = -2.500

d = 1.3333

e = -0.0833.

Central Difference constants = [-1/12, 4/3, -5/2, 4/3, -1/12]

Skewed Right-Sided Difference:

(`partial`^2 u)/( `partial`x^2) = af(i) + bf(i+1) + cf(i+2) + df(i+3) + ef(i+4) + gf(i+5)

Coefficients

f(i)

Δxf’(i)

Δx^2f’’(i)

Δx^3f’’’(i)

Δx^4f’’’’(i)

Δx^5f'’’’’(i)

f(i)

a

0

0

0

0

0

f(i+1)

b

b

b/2

b/6

b/24

b/120

f(i+2)

c

2c

4c/2

8c/6

16c/24

32c/120

f(i+3)

d

3d

9d/2

27d/6

81d/24

243d/120

f(i+4)

e

4e

16e/2

64e/6

256e/24

1024e/120

f(i+5)

g

5g

25g/2

125g/6

625g/24

3125g/120

Sum of Coefficients

0

0

1

0

0

?

Solving the following table, we get the linear equation which is evaluated using the matrix inversion method.

The coefficients are as:

a = 3.7500

b = -12.833

c = 17.833

d = -13.000

e = 5.0833

g = -0.0833

Skewed Right Difference Constants = [15/4, -77/6, 107/6, -26/2, 61/12, -5/6]

Skewed Left-Sided Difference

(`partial`^2 u)/( `partial`x^2) = af(i) + bf(i-1) + cf(i-2) + df(i-3) + ef(i-4) + gf(i-5)

Coefficients

f(i)

Δxf’(i)

Δx^2f’’(i)

Δx^3f’’’(i)

Δx^4f’’’’(i)

Δx^5f'’’’’(i)

f(i)

a

0

0

0

0

0

f(i+1)

b

-b

b/2

-b/6

b/24

-b/120

f(i+2)

c

-2c

4c/2

-8c/6

16c/24

-32c/120

f(i+3)

d

-3d

9d/2

-27d/6

81d/24

-243d/120

f(i+4)

e

-4e

16e/2

-64e/6

256e/24

-1024e/120

f(i+5)

g

-5g

25g/2

-125g/6

625g/24

-3125g/120

Sum of Coefficients

0

0

1

0

0

?

Solving the following table, we get the linear equation which is evaluated using the matrix inversion method.

The coefficients are as:

a = 3.7500

b = -12.833

c = 17.833

d = -13.000

e = 5.0833

g = -0.0833

Skewed Left Difference Constants = [15/4, -77/6, 107/6, -26/2, 61/12, -5/6]

Results

As seen from the plot, the central differencing scheme has very less error compared to other schemes. Therefore, central difference is better than skewed schemes because it uses information from both the sides from the point of consideration to give approximative and accurate values. As the skewed left and right difference methods use data only from one side of the point of interest it gives less approximative and accurate values about the point.

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.