Project

Taylor s table fourth order approximation for second order derivative

Objective – To derive 4th order approximation for the second order derivative of function using

  1. Central difference
  2. Skewed right sided difference
  3. Skewed left sided difference

write a program in Matlab to evaluate the second order derivative of the analytical function exp(x)*cos(x) and compare it with the 3 numerical approximations that you have derived.

Solution – In order to derive the fourth order approximation using central differencing method we need two values to the right and two values to the left from the point at which we need to calculate approximation.

Here we consider ‘0’ as the centre value and 1,2 and -1,-2 as the two points on either side of zero. Equally spaced point with 1 as difference between two consecutive points.

If we consider x as the centre point then on right side we will have (x+1) and (x+2) while on left hand side we will have (x-1) and (x-2.)

Expanding these function using taylor series, we create a table

Writing the above table in matrix form, we get the values for a,b,c,d,e.

Formula for central differencing scheme is

(a*f(x-2)+b*f(x-1)+c*f(x)+d*f(x+1)+e*f(x+2))/dx^2

Here x^2 terms are neglected and this forms a truncation error.

function out = central_differencing_scheme1(x,dx)

%analytical Function = exp(x)*cos(x)
%analytical derivative = -2*exp(x)*sin(x)

% central_differencing_scheme
A = [1,1,1,1,1;-2,-1,0,1,2;2,1/2,0,1/2,2;-4/3,-1/6,0,1/6,4/3;2/3,1/24,0,1/24,2/3];
C = [0,0,1,0,0]\';
B = inv(A)*C;
a = B(1);
b = B(2);
c = B(3);
d = B(4);
e = B(5);
I =  -2*exp(x)*sin(x);
J = (a*exp(x-2*(dx))*cos(x-2*(dx))+b*exp(x-dx)*cos(x-dx)+c*exp(x)*cos(x)+d*exp(x+dx)*cos(x+dx)+e*exp(x+2*(dx))*cos(x+2*(dx)))/(dx^2);
out = abs(I-J);
end

Similarly for the right hand side skewed and the left hand side skewed method we require 6 points.

We know that number of points is equal to sum of order of approximation and the order of derivative 4+2=6.

Table for Right side skewed method is

Writing the above table in matrix form, we get the values for a,b,c,d,e,f.

Formula for Forward differencing scheme is

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

function out = forward_differencing_scheme1(x,dx)

%analytical Function = exp(x)*cos(x)
%analytical derivative = -(2*exp(x))*sin(x)

% forward_differencing_scheme
A = [1 1 1 1 1 1;0,1,2,3,4 5;0,1/2,2,9/2,8,25/2;0,1/6,4/3,9/2,32/3,125/6;0,1/24,2/3,27/8,32/3,625/24;0,1/120,4/15,81/40,128/15,3125/120];
C = [0,0,1,0,0,0]\';
B= inv(A)*C;

a = B(1);
b = B(2);
c = B(3);
d = B(4);
e = B(5);
f = B(6);
I =  -2*exp(x)*sin(x);
J = (a*(exp(x)*cos(x))+b*(exp(x+dx)*cos(x+dx))+c*(exp(x+2*(dx))*cos(x+2*(dx)))+d*(exp(x+3*(dx))*cos(x+3*(dx)))+e*(exp(x+4*(dx))*cos(x+4*(dx)))+f*(exp(x+5*dx)*cos(x+5*(dx))))/(dx^2);
out = abs(I-J);
end

And same is done for Left hand side skewed method.

In right and left side skewed method the terms with multiple of x^4 are neglected.

function out = backward_differencing_scheme1(x,dx)

%analytical Function = exp(x)*cos(x)
%analytical derivative = -(2*exp(x))*sin(x)

% backward_differencing_scheme
A = [1,1,1,1,1,1;0,-1,-2,-3,-4,-5;0,1/2,2,9/2,8,25/2;0,-1/6,-4/3,-9/2,-32/3,-125/6;0,1/24,2/3,27/8,32/3,625/24;0,-1/120,-4/15,-81/40,-128/15,-3125/120];
C = [0,0,1,0,0,0]\';
B= inv(A)*C;

a = B(1);
b = B(2);
c = B(3);
d = B(4);
e = B(5);
f = B(6);

I =  -2*exp(x)*sin(x);
J = (a*(exp(x)*cos(x))+b*(exp(x-dx)*cos(x-dx))+c*(exp(x-2*(dx))*cos(x-2*(dx)))+d*(exp(x-3*(dx))*cos(x-3*(dx)))+e*(exp(x-4*(dx))*cos(x-4*(dx)))+f*(exp(x-5*dx)*cos(x-5*(dx))))/(dx^2);
out = abs(I-J);
end

Similarly we calculate second order forward and backward differencing methods for f(x)

Formula for forward differencing method is

= (f(x)-2*f(x+1)+f(x+2))/dx^2

function out = second_order_forward_difference(x,dx)

%analytical Function = exp(x)*cos(x)
%analytical derivative = -2*exp(x)*sin(x)

% forward_differencing_scheme_second_order

R = -2*exp(x)*sin(x);
S = (exp(x)*cos(x)-2*exp(x+dx)*cos(x+dx)+exp(x+2*dx)*cos(x+2*dx))/dx^2;

out = abs(R-S);

Formula for Backward differencing method is

= (f(x)-2*f(x-1)+f(x-2))/dx^2

function out = second_order_backward_difference(x,dx)

%analytical Function = exp(x)*cos(x)
%analytical derivative = -2*exp(x)*sin(x)

% backward_differencing_scheme_second_order

R = -2*exp(x)*sin(x);
S = (exp(x-2*dx)*cos(x-2*dx)-2*exp(x-dx)*cos(x-dx)+exp(x)*cos(x))/dx^2;

out = abs(S-R);

Finding errors for all five methods with absolute difference of analytical and approximate values.

Main script of talyor table approximation.

clear all
close all
clc

x=pi/3;
dx = linspace(pi/6,pi/60000,50);

error_skewed_right_side=zeros(50);
error_skewed_left_side=zeros(50);
error_cds=zeros(50);
error_second_order_fd=zeros(50);
error_second_order_bd=zeros(50);

for i=1:length(dx)
error_skewed_right_side(i)=forward_differencing_scheme1(x,dx(i));
error_skewed_left_side(i)=backward_differencing_scheme1(x,dx(i));
error_cds(i)=central_differencing_scheme1(x,dx(i));
error_second_order_fd(i)=second_order_forward_difference(x,dx(i));
error_second_order_bd(i)=second_order_backward_difference(x,dx(i));

end
figure(1)
loglog(dx,error_skewed_right_side,\'color\',\'b\',\'linewidth\',2)
hold on
loglog(dx,error_skewed_left_side,\'color\',\'g\',\'linewidth\',2)
hold on
loglog(dx,error_cds,\'color\',\'r\',\'linewidth\',2)
xlabel(\'dx\')
ylabel(\'error\')
title(\'dx vs error\')
legend(\'skewed right differencing scheme\',\'skewed left differencing scheme\',\'central differencing scheme\');

figure(2)
loglog(dx, error_skewed_right_side,\'b\',\'linewidth\',2);
hold on
loglog(dx,error_skewed_left_side,\'g\',\'linewidth\',2);
hold on
loglog(dx,error_cds,\'r\',\'linewidth\',2);
hold on
loglog(dx,error_second_order_fd,\'y\',\'linewidth\',2);
hold on
loglog(dx,error_second_order_bd,\'black\',\'linewidth\',2);
xlabel(\'dx\')
ylabel(\'error\');
title(\'fourth order error VS Second order error\')
legend(\'right sided differencing scheme\', \'left sided differencing scheme\', \'central differencing scheme\', \'error_second_order_fd\', \'error_second_order_bd\' ,\'location\', \'northwest\');

Plotting Right left side skewed method and central differencing method.

From the graph we see that central differencing scheme \'RED plot\' is prone to minimum error with the value of dx and Right side skewed differencing \'BLUE plot\' has maximum error for the same decreasing value of dx. While left side skewed method \'Green plot\' sits in between.

Yellow plot is second order forward differencing while Black is second order backward differencing.

Figure 2 shows the comparison between second order approximation and the fourth order approximation. Error in second order approximation is higher then fourth order approximation.

But the disadvantage for the central differencing scheme is that it cannot be applied at the edge of the grid, while the other two method can be applied.

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.