Challenge

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

Objective :-

To derive the following 4th order approximation of the second order derivative using

  • Central difference
  • Skewed rigth-sided difference
  • Skewed left-sided difference

First step is to find the number of nodes required for deriving each difference method using the given formula.

After deriving all the difference method, the other step is to compare all the derived difference method with the exact solution & to find the range of error for each difference method using MATLAB.

SCHEME DERIVATIONS :-

1. Central difference :-

Finding the no of nodes:

The order of accuracy P = Number of nodes in the stencil N - order of derivative Q + 1 (symmetric/central schemes)

Then the Number of nodes N = The order of accuracy P + Order of derivative Q - 1

N = 4+2-1

N = 5

function output = cd_diff_4th_order(x,dx)
A=[1 1 1 1 1;
-2 -1 0 1 2;
4 1 0 1 4;
-8 -1 0 1 8;
16 1 0 1 16]
B =[0;
0;
2 ;
0;
0]
sol =inv(A)*B
a =sol(1)
b =sol(2)
c =sol(3)
d =sol(4)
e= sol(5)
%% numeric sol
output =(a*f(x-2*dx)+b*f(x-dx)+c*f(x)+d*f(x+dx)+e*f(x+2*dx))/dx^2
function y= f(x)
y= exp(x)*cos(x)
end
end

Derivation :-

To derive for fourth order approximations for second order derivative, we start with these approximations,

Taylor Series Expansion for the above terms :-

Representing in the form of a table:

2. Right Side Difference :-

The order of accuracy P = Number of nodes in the stencil N - order of derivative Q for one-sided/skewed schemes.

Here N = 6

% righgt matrix
function output = right_diff_4th_order(x,dx)
A =[1 1 1 1 1 1;
0 1 2 3 4 5;
0 1 4 9 16 25;
0 1 8 27 64 125;
0 1 16 81 256 625;
0 1 32 243 1024 3125]
B= [0;
0;
2;
0;
0;
0]
sol= inv(A)*B
a = sol(1)
b = sol(2)
c= sol(3)
d= sol(4)
e= sol(5)
g= sol(6)
output =(a*f(x)+b*f(x+dx)+c*f(x+2*dx)+d*f(x+3*dx)+e*f(x+4*dx)+g*f(x+5*dx))/dx^2
function y =f(x)
y = exp(x)*cos(x)
end
end

Reprsenting in the form of a table :


3. Skewed left-sided difference :-

The order of accuracy P = Number of nodes in the stencil N - order of derivative Q for one-sided/skewed schemes.

Here N = 6.

% left matrix
function output =left_diff_4th_order(x,dx)
A=[ 1 1 1 1 1 1
-5 -4 -3 -2 -1 0
25 16 9 4 1 0
-125 -64 -27 -8 -1 0
625 256 81 16 1 0
-3125 -1024 -243 -32 -1 0]
B= [0
0
2
0
0
0]
sol =inv(A)*B
a= sol(1)
b= sol (2)
c= sol(3)
d= sol(4)
e= sol(5)
g= sol(6)
output= (a*f(x-5*dx)+b*f(x-4*dx)+c*f(x-3*dx)+d*f(x-2*dx)+e*f(x-dx)+g*f(x))/dx^2
function y =f(x)
y = exp(x)*cos(x)
end
end

Representing in the form of a table :


MATLAB CODE :-

clear all
close all
clc
%% 4th order cd diff
%% 4th order right diff
%% 4th order left diff
x= pi/4;
dx_array = linspace(pi/10000,pi/10,20);
%% analatic approach
analytical_sol = -2*exp(x)*sin(x);
%% numerical approach
%% cd sol
%% right sol
%% left sol
for i = 1:length (dx_array)
dx = dx_array(i)
numeric_sol_cd= cd_diff_4th_order(x,dx)
numeric_sol_right=right_diff_4th_order(x,dx)
numeric_sol_left =left_diff_4th_order(x,dx)
error_40_cd(i)= abs(analytical_sol-numeric_sol_cd)
error_40_right(i)= abs(analytical_sol-numeric_sol_right)
error_40_left(i)= abs(analytical_sol-numeric_sol_left)
end
loglog(dx_array,error_40_cd)
hold on
loglog(dx_array,error_40_right)
hold on
loglog(dx_array,error_40_left)
hold on
xlabel('dx')
ylabel("error_40")
legend("cd","right","left")

Output Graph :-


Conclusion :-

As seen from the plot the central differencing scheme has very less error when compared to the other schemes. Therefore, central differencing scheme is better than other schemes because it uses information from both the sides from the point of consideration to give appropriate and accurate values.

As the left & right skewed difference methods uses data only from one side of point of interest it gives less approximation & accurate values about that point.

The system which doesn't contain sufficient in format ion on both the side of the point of interest in such cases we cannot use central differencing scheme so skewed scheme is useful in such cases.

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.