Challenge

Taylor table method and Matlab code

The objective of this Challenge is to calculate errors for three different schemes such as the left-skewed, right-skewed and central differencing and to compare which one provides the least error and vice versa. This is done using a Simplification technique called Taylor table method.

The fourth-order approximation of second-order derivative is calculated here.

NUMERICAL STENCIL AND ITS GEOMETRY ON VARIOUS SCHEMES:

The numerical stencil is a boundary within which the node values are calculated.

In case of a Central Differencing Scheme, The stencil is symmetric. Since it has two nodes on the left and two nodes on the right.

In Skewed right, as the name suggests it goes towards the right

In Skewed left, it goes towards left.

The Taylor series is calculated in the form of Taylor Table.

CENTRAL DIFFERENCING:

The formula to find the number of nodes to be calculated for Central Differencing is p + q - 1

P - Order of derivative (2)

q - Order of Approximation(4)

So the number of nodes for central Differencing is 5

We need to find the coefficients a,b,c,d,e.

The A matrix is obtained by using the results below.

AC = B

C = [a b c d e f]'

C = inv(A)*B

SKEWED RIGHT SCHEME

The formula to find the number of nodes to be calculated for Central Differencing is p + q

P - Order of derivative (2)

q - Order of Approximation(4)

So the number of nodes for central Differencing is 6

The A matrix is obtained by using the results below.

AC = B

C = [a b c d e f]'

C = inv(A)*B

So the coefficients are found by calling the index of C

SKEWED LEFT SCHEME:

The formula to find the number of nodes to be calculated for Central Differencing is p + q

P - Order of derivative (2)

q - Order of Approximation(4)

So the number of nodes for central Differencing is 6

The A matrix is obtained by using the results below.

AC = B

C = [a b c d e f]'

C = inv(A)*B

So, for each scheme, we could see that the coefficients a,b,c,d,e,g are calculated and applied in the hypothesis, from which we obtain a numerical solution for the respective schemes.

And then the error is obtained from the difference of analytical derivative. The code and results obtained are as follows.

Thus the error in various schemes is calculated by the difference of analytical derivative. In this case, it is -2*exp(x)*sin(x).

USAGE OF ANONYMOUS FUNCTION TO CALCULATE f(x):

The values for x, dx, coefficients and functions are found.

MAIN CODE FOR TAYLOR TABLE METHOD OF APPROXIMATIONS:

clear all
close all
clc

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

for i = 1:length(dx)
    
    central_differencing_scheme(i) = central_diff(x,dx(i));
    skewed_left_scheme(i) = skewed_left(x,dx(i));
    skewed_right_scheme(i) = skewed_right(x,dx(i));
end


figure(1)

loglog(dx,central_differencing_scheme,'b')
hold on
loglog(dx,skewed_left_scheme,'y')
hold on
loglog(dx,skewed_right_scheme,'k')
xlabel('dx');
ylabel('error')
legend('Central Differencing scheme' , 'Left Skewed scheme' , 'Right Skewed scheme')

FUNCTION CODE FOR CENTRAL DIFFERENCING:

function out = central_diff(x,dx)


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

f = @(x) exp(x)*cos(x);

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]';


C = inv(A)*B;

a = C(1);
b = C(2);
c = C(3);
d = C(4);
e = C(5);
numerical_derivative = (a*f(x-2*dx) + b*f(x-dx) + c*f(x) + d*f(x+dx) + e*f(x+2*dx))/(dx^2);

out = abs(analytical-numerical_derivative);

end

FUNCTION CODE FOR SKEWED RIGHTSCHEME:

function out = skewed_right(x,dx)


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

f = @(x) exp(x)*cos(x);

A = [1 1 1 1 1 1
     0 1 2 3 4 5 
     0 1/2 4/2 9/2 16/2 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];
 
B = [0 0 1 0 0 0]';


C = inv(A)*B ; 

a = C(1);
b = C(2);
c = C(3);
d = C(4);
e = C(5);
g = C(6);

numerical_derivative_right = (a*f(x*dx) + b*f(x+1*dx) + c*f(x+2*dx) + d*f(x+3*dx) + e*f(x+4*dx) + g*f(x+5*dx))/(dx^2);

out = abs(analytical-numerical_derivative_right);

end

FUNCTION CODE FOR SKEWED LEFT SCHEME:

function out = skewed_left(x,dx)


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

f = @(x) exp(x)*cos(x);

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


C = inv(A)*B ; 

a = C(1);
b = C(2);
c = C(3);
d = C(4);
e = C(5);
g = C(6);

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

out = abs(analytical-numerical_derivative_left);

end

OBTAINED RESULT:

CONCLUSIONS:

  • We could see from the graph that the Central differencing scheme produces less error when compared to other schemes.
  • Central Differencing< Skewed Left
  • And we could also say that the symmetric stencil provides less error when compared to skewed ones.
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.