## Taylor table Method and MATLAB Code

• CENTRAL DIFFERENCE SCHEME:

Central differencing scheme takes information from both right and left hand sides nodes and it is widely used for caluculating second order derivatives of nth order approximatons. This schemes are very simpleand esay to understand and gives results in very less computing time.

f’’ (x)= 4th order accurate approximation using i-2,i-1,i,i+2 and i+2,

Using Taylor series method,

f’’(x) =  afi-2+ bfi-1+ cfi+ dfi+1+ efi+2;

From Taylor series expansion for each ‘f’ term we can form a Taylor table,

If we look the above table, for the fifth and sixth order derivatives we put question mark because we don’t know what exactly the term is equal to. As we are calculating up to fourth order approximations only.

By converting the above information into matrix form,

From above matrix,

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/6 = 0,

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

By solving above linear equations in ocatve using matrix inversion method (A*X=B), we get

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

By dividing the entire eq.1 with dx2 , because we are approximating for fourth order approximation and for second order derivative. So,

• Skewed Right-Sided approximation:

This scheme is very useful when we dont have information from corresponding left sided nodes, then we will take information from neighbouring right handed side nodes. To caluculate the second order deriavtive of that node we have to take more corresponding nodes information, then the approximation is called skewed right sided approximation.

f’’(x) = 4th order accurate approximation using i, i+1, i+2, i+3 and i+4,

Using Taylor series method,

f’’(x) = afi+ bfi+1+ cfi+2+ dfi+3+ efi+4;

From Taylor series expansion for each ‘f’ term we can form a Taylor table,

If we look the above table, for the fifth and sixth order derivatives we put question mark because we don’t know what exactly the term is equal to. As we are calculating up to fourth order approximations only.

By converting the above information into matrix form,

From above matrix,

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.

By solving above linear equationsin ocatve using matrix inversion method (A*X=B), we get

a= 1.3396, b = -2.9892, c = 1.9301, d = -0.2508, e = -0.0295

By dividing the entire eq.1 with dx2 , because we are approximating for fourth order approximation and for second order derivative. So,

• SKEWED LEFT-SIDED DIFFERENCE SCHEME:

This scheme is very useful when we dont have information from corresponding right sided nodes, then we will take information from neighbouring left handed side nodes. To caluculate the second order deriavtive of that node we have to take more corresponding nodes information, then the approximation is called skewed left sided approximation.

f’’(x) = 4th order accurate approximation using i-4,i-3,i-2,i-1 and i,

Using Taylor series method,

f’’(x) = afi-4+ bfi-3+ cfi-2+ dfi-1+ efi;

From Taylor series expansion for each ‘f’ term we can form a Taylor table,

If we look the above table, for the fifth and sixth order derivatives we put question mark because we don’t know what exactly the term is equal to. As we are calculating up to fourth order approximations only.

By converting the above information into matrix form,

From above matrix,

a+b+c+d+e = 0,

-4a-3b-2c-d+0 = 0,

8a+9b/2+2c-d+0 = 1,

-32a/3-9b/2+2c+d/2+0 = 0,

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

By solving above linear equations,in ocatve using matrix inversion method (A*X=B), we get

a= 0.9166, b = -4.6666, c = 9.5000, d = -8.6666, e = 2.9166

By dividing the entire eq.1 with dx2 , because we are approximating for fourth order approximation and for second order derivative. So,

Program for the central differencing scheme:

function output = central_diff(x,dx)
%analytical function f(x) = exp(x)*cos(x)
%f = @(x)exp(x)*cos(x);
%second order derivative of analytical function = -2*e^x*sin(x);
analytical_derivative = -2*e^x*sin(x);
%function
f = @(x)exp(x)*cos(x);
%set of equations obtained from taylor table
%eq1=a+b+c+d+e = 0,
%eq2=-2a-b+0+d+2e = 0,
%eq3=2a+b/2+0+d/2+2e = 1,
%eq4=-4a/3-b/6+0+d/6+4e/6 = 0,
%eq5=2a/3+b/24+0+d/24+2e/3 = 0.
% matrtix form for above eqns,
A = [1,1,1,1,1;-2,-1,0,1,2;4/2,1/2,0,1/2,4/2;-8/6,-1/6,0,1/6,8/6;16/24,1/24,0,1/24,16/24];
X = ['a';'b';'c';'d';'e'];
B = [0;0;1;0;0];
%extracting constants
X = inv(A)*B;
%extracting constants
a = X(1);
b = X(2);
c = X(3);
d = X(4);
e = X(5);
central_differencing = (a*f(x-2*dx)+b*f(x-dx)+c*f(x)+d*f(x+dx)+e*f(x+2*dx))/(dx^2);

output = abs(central_differencing-analytical_derivative);
endfunction

Program for the Skewed Right sided differencing scheme:

function output = skewed_right_diff(x,dx)
%analytical function f(x) = exp(x)*cos(x)
%f = @(x)exp(x)*cos(x);
%second order derivative of analytical function = -2*e^x*sin(x);
analytical_derivative = -2*e^x*sin(x);
%function
f = @(x)exp(x)*cos(x);
%set of equations obtained from taylor table
%eq1 =a+b+c+d+e = 0;
%eq2=0+b+2c+3d+4e = 0;
%eq3=0+b/2+2c+9d/2+8e = 1;
%eq4=0+b/6+4c/3+9d/2+32e/3 = 0;
%eq5=0+b/24+2c/3+27d/8+32e/3 = 0;

% matrtix form for above eqns,
A = [1,1,1,1,1;0,1,2,3,4;0,1/2,2,9/2,16/2;0,1/6,8/6,27/6,64/6;0,1/24,16/24,81/24,256/24];
X = ['a';'b';'c';'d';'e'];
B = [0;0;1;0;0];
%extracting constants
X = inv(A)*B;
%extracting constants
a = X(1);
b = X(2);
c = X(3);
d = X(4);
e = X(5);
skewed_right_differencing = (a*f(x)+b*f(x+dx)+c*f(x+2*dx)+d*f(x+3*dx)+e*f(x+4*dx))/(dx^2);

output = abs(skewed_right_differencing-analytical_derivative);
endfunction

Program for the Skewed Right sided differencing scheme:

function output = skewed_left_diff(x,dx)
%analytical function f(x) = exp(x)*cos(x)
%f = @(x)exp(x)*cos(x);
%second order derivative of analytical function = -2*e^x*sin(x);
analytical_derivative = -2*e^x*sin(x);
%function
f = @(x)exp(x)*cos(x);
%set of equations obtained from taylor table
%eq1=a+b+c+d+e =0;
%eq2=-4a-3b-2c-d+0 =0;
%eq3=8a+9b/2+2c-d+0 =1;
%eq4=-32a/3-9b/2+2c+d/2+0 =0;
%eq5=32a/3+27b/8+2c/3+d/24+0 =0;

% matrtix form for above eqns,
A = [1,1,1,1,1;-4,-3,-2,-1,0;16/2,9/2,2,1/2,0;-64/6,-27/6,-8/6,-1/6,0;256/24,81/24,16/24,1/24,0];
X = ['a';'b';'c';'d';'e'];
B = [0;0;1;0;0];
%extracting constants
X = inv(A)*B;
%extracting constants
a = X(1);
b = X(2);
c = X(3);
d = X(4);
e = X(5);
skewed_left_differencing = (a*f(x-2*dx)+b*f(x-dx)+c*f(x)+d*f(x+dx)+e*f(x+2*dx))/(dx^2);

output = abs(skewed_left_differencing-analytical_derivative);
endfunction

And the main program:

clear all
close all
clc
%function
f = @(x)exp(x)*cos(x);
%input values
x = pi/3;
dx = linspace(pi/10,pi/4000,20);

for i = 1:length(dx)
central_diff_error(i)= central_diff(x,dx(i));
skewed_right_diff_error(i)=skewed_right_diff(x,dx(i));
skewed_left_diff_error(i)=skewed_left_diff(x,dx(i));
endfor
%plotting
figure(1)
loglog(dx,central_diff_error,'r','linewidth',3);
hold on
loglog(dx,skewed_right_diff_error,'b','linewidth',3);
hold on
loglog(dx,skewed_left_diff_error,'g','linewidth',3);
xlabel('dx');
ylabel('Differencing errors');
legend('Central differencing','Skewed right sided','Skewed Left sided');
title('Comparison between differencing schemes')



Plotting the result:

output:

Conclusion:

By looking the above plot and output, we can came to know that the central differencing scheme is accurate comapring to other two schemes. So using the Central differencing schemes gives accurate solutions.

b) Skewed schemes can be useful when we the node is starting or ending, at that node we are unable to caluculate the second order deriavtives at the starting node or ending node by usind central differencing schemes. As the CDS requries information from both left and right sided nodes.