Project

HIGHER ORDER APPROXIMATIONS USING SKEWED AND SYMMETRICAL STENCILS

OBJECTIVES

1. Perform a compasison on nd order derrivative using 4th order approximations by the following numerical schemes

1.1. Central Difference Scheme

1.2. Right Sided Skewed Scheme

1.3. Left Sided Skewed Scheme

2. Comparison of the above mentioned schemes with forward differencing and backward differencing numerical schemes.

KEY POINTS FOR THIS ANALYSIS

1. Given Function:

`f(x) = exp(x)*cos(x)`

2. Analytical value of the 2nd derrivative

`f\'\'(x) = -2*exp(x)*sin(x)`

3. The derrivatives found using any of the numerical scheme is only the approximate value.

CENTRAL DIFFERENCING SCHEME FOR SECOND ORDER DERRIVATIVE

Selected points on the numerical stencil are i-2, i-1, i, i+1, i+2.

The Taylor Series Scheme Can be written as:

`(∂^2 f)/(∂x^2 )=a.f(i-2)+b.f(i-1)+c.f(i)+d.f(i+1)+e.f(i+2)`

The corrosponding Taylor\'s Table for the above Taylor Series is as follows:

f(i)

f’(x)*dx

f’’(x)*(dx)2

f’’’(x)*(dx)3

f’’’’(x)*(dx)4

a*f(i-2)

a

-2a

2a

-4a/3

2a/3

b*f(i-1)

b

-b

b/2

-b/6

b/24

c*f(i)

c

0

0

0

0

d*f(i+1)

d

d

d/2

d/6

d/24

e*f(i+2)

e

2e

2e

4e/3

2e/3

TOTAL

0

0

1

0

0

From the above table we get a system of linear equations:

a + b + c + d + e = 0

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

2a+ b/2 + 0 + d/2 + 2e = 1

-4a/3 - b/6 + 0 + d/6 + 4e/3 = 0

2a/3 + b/24 + 0 + d/24 + 2e/3 = 0

Solving the above system of linear equations in MATLAB we get:

a = -0.0833, b = 1.3333, c = -2500, d = 1.3333, e = -0.0833

Replacing the above values in the original taylor series and replacing the function with the given function and its corrosponding derrivative and the point notations with variable \'x\' and increment/decrement \'dx\' we get:

`(∂^2 f)/(∂x^2 ) = ((-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 + O(dx^4)`

RIGHT SIDED SKEWED SCHEME FOR SECOND ORDER DERRIVATIVE

Selected points on the numerical stencil are i, i+1, i+2, i+3, i+4.

The Taylor Series Scheme Can be written as:

`(∂^2 f)/(∂x^2 )=a.f(i)+b.f(i+1)+c.f(i+2)+d.f(i+3)+e.f(i+4)`

The corrosponding Taylor\'s Table for the above Taylor Series is as follows:

f(i)

f’(x)*dx

f’’(x)*(dx)2

f’’’(x)*(dx)3

f’’’’(x)*(dx)4

a*f(i)

a

0

0

0

0

b*f(i+1)

b

b

b/2

b/6

b/24

c*f(i+2)

c

2c

2c

4c/3

2c/3

d*f(i+3)

d

3d

9d/2

9d/2

27d/8

e*f(i+4)

e

4e

8e

32e/3

32e/3

TOTAL

0

0

1

0

0

From the above table we get a system of linear equations:

a + b + c + d + e = 0

0 + b + 2c + 3d + 4e = 0

0 + b/2 +2c + 9d/2 + 8e = 1

0 + b/6 + 4c/3 + 9d/2 + 32e/3 = 0

0 + b/24 + 2c/3 + 27d/8 + 32e/3 = 0

Solving the above system of linear equations in MATLAB we get:

a = 2.9167, b = -8.6667, c = 9.500, d = -4.6667, e = 0.9167

Replacing the above values in the original taylor series and replacing the function with the given function and its corrosponding derrivative and the point notations with variable \'x\' and increment/decrement \'dx\' we get:

`(∂^2 f)/(∂x^2 ) = ((2.9167*exp(x)*cos(x))+(-8.6667*exp(x+dx)*cos(x+dx))+(9.5000*exp(x+2*dx)*cos(x+2*dx))+(-4.6667*exp(x+3*dx)*cos(x+3*dx))+(0.9167*exp(x+4*dx)*cos(x+4*dx)))/(dx)^2 + O(dx^4)`

LEFT SIDED SKEWED SCHEME FOR SECOND ORDER DERRIVATIVE

Selected points on the numerical stencil are i, i-1, i-2, i-3, i-4.

The Taylor Series Scheme Can be written as:

`(∂^2 f)/(∂x^2 )=a.f(i)+b.f(i-1)+c.f(i-2)+d.f(i-3)+e.f(i-4)`

The corrosponding Taylor\'s Table for the above Taylor Series is as follows:

f(i)

f’(x)*dx

f’’(x)*(dx)2

f’’’(x)*(dx)3

f’’’’(x)*(dx)4

a*f(i)

a

0

0

0

0

b*f(i-1)

b

-b

b/2

-b/6

b/24

c*f(i-2)

c

-2c

2c

-4c/3

2c/3

d*f(i-3)

d

-3d

9d/2

-9d/2

27d/8

e*f(i-4)

e

-4e

8e

-32e/3

32e/3

TOTAL

0

0

1

0

0

From the above table we get a system of linear equations:

a + b + c + d + e = 0

0 - b - 2c - 3d - 4e = 0

0 + b/2 +2c + 9d/2 + 8e = 1

0 - b/6 - 4c/3 - 9d/2 - 32e/3 = 0

0 + b/24 + 2c/3 + 27d/8 + 32e/3 = 0

Solving the above system of linear equations in MATLAB we get:

a = 2.9167, b = -8.6667, c = 9.500, d = -4.6667, e = 0.9167

Replacing the above values in the original taylor series and replacing the function with the given function and its corrosponding derrivative and the point notations with variable \'x\' and increment/decrement \'dx\' we get:

`(∂^2 f)/(∂x^2 ) = ((2.9167*exp(x)*cos(x))+(-8.6667*exp(x-dx)*cos(x-dx))+(9.5000*exp(x-2*dx)*cos(x-2*dx))+(-4.6667*exp(x-3*dx)*cos(x-3*dx))+(0.9167*exp(x-4*dx)*cos(x-4*dx)))/(dx)^2 + O(dx^4)`

FORWARD AND BACKWARD DIFFERENCING SCHEME FOR 2nd ORDER DERRIVATIVE

Ususlly we consider the following when we talk about forward/backward differencing schemes:

Forward:

`(∂f)/(∂x) = ((f(x+dx)- f(x)))/(dx)`

Backward:

`(∂f)/(∂x) = (f(x) - (f(x-dx)))/(dx)`

These expressions are valid for first order derrivatives.

But as we are caliberating the second order derrivative in all the above numerical schemes, it would be appropriate to compare the above schemes with second order forward and backward differencing schemes.

2nd Order Forward Difference Scheme Corrosponding to Given Function:

`((exp(x+2*dx)*cos(x+2*dx))-2*(exp(x+dx)*cos(x+dx))+(exp(x)*cos(x)))/(dx)^2 + O(dx)`

2nd Order Backward Difference Scheme Corrosponding to Given Function:

`((exp(x)*cos(x))-2*((exp(x-dx)*cos(x-dx)))-((exp(x-2*dx)*cos(x-2*dx))))/(dx)^2 + O(dx)`

MATLAB CODE FOR MAIN ANALYSIS

close all
clear all
clc

%%Assuming x=pi/6
x = pi/6;

%%Defining the grid size
dx=linspace(pi/10,pi/250,10);

%Calculating error
for i = 1:length(dx)
    error_central(i) =central_difference(x,dx(i));
    error_right(i) = skew_right(x,dx(i));
    error_left(i)=skew_left(x,dx(i));
    error_forward(i)=forward_difference(x,dx(i));
    error_backward(i)=backward_difference(x,dx(i));
end

%figure(1) Truncation error Comparison between the three derrived schemes
figure(1)
loglog(dx,error_central ,\'r\',\'linewidth\', 2);
hold on
loglog(dx,error_right ,\'b\',\'linewidth\', 2);
hold on
loglog(dx,error_left ,\'g\',\'linewidth\', 2);

xlabel(\'step size\')
ylabel(\'Error due to truncation\')
title(\'Comparision between different schemes for a second order derivative of 4th order accuracy\')
legend(\'Central difference\',\'Skewed right sided difference\',\' Skewed left sided difference\')

%%Figure(2) Truncation error comparison between the three derrived schemes
%%and the forward and backward differencing schemes
figure(2)
loglog(dx,error_central ,\'r\',\'linewidth\', 1.5);
hold on
loglog(dx,error_right ,\'b\',\'linewidth\', 1.5);
hold on
loglog(dx,error_left ,\'g\',\'linewidth\', 1.5);
hold on
loglog(dx,error_forward ,\'y\',\'linewidth\', 0.5);
hold on
loglog(dx,error_backward ,\'p--\',\'linewidth\', 0.5);
xlabel(\'step size\')
ylabel(\'Error due to truncation\')
title(\'Comparision between different schemes for a second order derivative of 4th order accuracy and FD and BD\')
legend(\'Central difference\',\'Skewed right sided difference\',\' Skewed left sided difference\',\'Forward difference\',\'Backward difference\')

MATLAB FUNCTION CODE FOR CENTRAL DIFFERENCING SCHEME

function [Error1] = central_difference(x,dx)

%Analyical Value of derrivative
analytical_derrivative = -2*exp(x)*sin(x);

%2nd order Derrivative Using Central Differencing of 4th Order approximation
central_derrivative = ((-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;

%Truncation Error Calculation
Error1 = abs(analytical_derrivative-central_derrivative);                 % Central Scheme Error
end

MATLAB FUNCTION CODE FOR RIGHT SIDED SKEWED SCHEME

function [Error2] = skew_right(x,dx)

%Analyical Value of derrivative
analytical_derrivative = -2*exp(x)*sin(x);

%2nd order Derrivative Using Skewed Right Side Differencing Scheme of 4th Order approximation
skew_right_derrivative = ((2.9167*exp(x)*cos(x))+(-8.6667*exp(x+dx)*cos(x+dx))+(9.5000*exp(x+2*dx)*cos(x+2*dx))+(-4.6667*exp(x+3*dx)*cos(x+3*dx))+(0.9167*exp(x+4*dx)*cos(x+4*dx)))/(dx)^2;

%Truncation Error Calculation
Error2 = abs(analytical_derrivative-skew_right_derrivative);            % Skewed right Error
end

MATLAB FUNCTION CODE FOR LEFT SIDED SKEWED SCHEME

function [Error3] = skew_left(x,dx)

%Analyical Value of derrivative
analytical_derrivative = -2*exp(x)*sin(x);

%2nd order Derrivative Using Skewed Left Side Differencing Scheme of 4th Order approximation
skew_left_derrivative = ((2.9167*exp(x)*cos(x))+(-8.6667*exp(x-dx)*cos(x-dx))+(9.5000*exp(x-2*dx)*cos(x-2*dx))+(-4.6667*exp(x-3*dx)*cos(x-3*dx))+(0.9167*exp(x-4*dx)*cos(x-4*dx)))/(dx)^2;

%Truncation Error Calculation
Error3 = abs(analytical_derrivative-skew_left_derrivative);             % Skewed Left Error
end

MATLAB FUNCTION CODE FOR FORWARD DIFFERENCING

function error4 = forward_difference(x,dx)

%%Analytical value
analytical_derrivative = -2*exp(x)*sin(x);

%%Forward difference to caliberate 2nd order derrivative
forward= ((exp(x+2*dx)*cos(x+2*dx))-2*(exp(x+dx)*cos(x+dx))+(exp(x)*cos(x)))/(dx)^2;

%Truncation Error
error4 = abs(forward-analytical_derrivative);

end

MATLAB CODE FOR BACKWARD DIFFERENCING

function error4 = forward_difference(x,dx)

%%Analytical value
analytical_derrivative = -2*exp(x)*sin(x);

%%Forward difference to caliberate 2nd order derrivative
forward= ((exp(x+2*dx)*cos(x+2*dx))-2*(exp(x+dx)*cos(x+dx))+(exp(x)*cos(x)))/(dx)^2;

%Truncation Error
error4 = abs(forward-analytical_derrivative);

end

ANALYSIS OUTPUTS

\"\"

\"\"

COMPARISON OF SKEWED SCHEMES WITH FORWARD AND BACKWARD SCHEMES

Clearly Skewed Schemes have less error magnitude compared to forward and backward difference schemes when compared over the same step sizes. This is due to the simple fact that in skewed schemes more neighbouring points are considered compared to dorward or backward schemes and hence the deviation form the analytical value is lesser in skewed schemes.

ADVANTAGE OF SKEWED SCHEMES OVER CENTRAL DIFFERENCE SCHEMES

Central Differencing schemes can\'t be applied to boundaries due to unavailability of grid points. This is because CDS requires grid points on both sides of reference points depending on the order of approximation scheme and order of derrivative to be evaluated.

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.