Challenge

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

Objective

To derive the 4th order approximations of the second-order derivative using following three methods using help of taylor table.

1. Central difference

2. Skewed right-sided difference

3. Skewed left-sided difference

Comparing the result obtained from three different methods

1.Solving using central difference scheme

For symmetric/central schemes

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

N = P + Q -1

N = 4+2 -1 = 5

Number of nodes required = 5

We consider c as the centre node and two nodes in its left & right side so that the equations for five nodes will be

From the equation when obtain the below taylor table

On converting above system of equations into matrix and solving by matrix inversion method

The solved values for X are

2. Left side skewed approximation

For skewed schemes

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

N = P + Q

N = 4+2 = 6

Number of nodes required = 6

3. Right side skewed approximation

For skewed schemes

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

N = P + Q

N = 4+2 = 6

Number of nodes required = 6

Main Code

clear all
close all
clc
x= pi/3;
dx= linspace(pi/4,pi/4000,100);
for i=1:length(dx)
%Central differencing scheme
central_differencing(i) = fourth_order_central_differencing(x,dx(i));
%Left Sided Skew Differencing
left_sided_skew(i) = fourth_order_left_sided_skew(x,dx(i));
%Right Sided Skew Differencing
right_sided_skew(i) = fourth_order_right_sided_skew(x,dx(i));
end
figure(1)
loglog(dx,central_differencing,'r')
hold on
loglog(dx,right_sided_skew,'g')
loglog(dx,left_sided_skew,'b')
xlabel('range of dx')
ylabel('truncation error')
title('range of dx vs error')
legend('central differencing','skewed right sided difference','skewed left sided difference')
hold off
Function Code - Central Difference Method
function Error_central_differencing = fourth_order_central_differencing(x,dx)
%Analytic Function = exp(x)*cos(x)
%Analytic Derivative
analytical_derivative = -2*exp(x)*sin(x);
%Central Differencing
central_differencing = (((-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);
%error
Error_central_differencing = abs(central_differencing- analytical_derivative);
end
Function Code - Skewed Left Difference Method
function Error_left_sided_skew = fourth_order_left_sided_skew(x,dx)
% Analytic Function = exp(x)*cos(x)
% Analytic Derivative
analytical_derivative = -2*exp(x)*sin(x);
%Left Sided Skew Differencing
b=((-0.8333)*((exp(x-(5*dx)))*(cos(x-(5*dx)))))+((5.0833)*((exp(x-(4*dx)))*(cos(x-(4*dx)))))-((13.0000)*((exp(x-(3*dx)))*(cos(x-(3*dx)))));
c=+((17.8333)*((exp(x-(2*dx)))*(cos(x-(2*dx)))));
d=-((12.8333)*((exp(x-dx))*(cos(x-dx))))+((3.7500)*((exp(x))*(cos(x))))/(dx^2);
left_sided_skew = (((-0.8333)*((exp(x-(5*dx)))*(cos(x-(5*dx)))))+((5.0833)*((exp(x-(4*dx)))*(cos(x-(4*dx)))))-((13.0000)*((exp(x-(3*dx)))*(cos(x-(3*dx)))))+((17.8333)*((exp(x-(2*dx)))*(cos(x-(2*dx)))))-((12.8333)*((exp(x-dx))*(cos(x-dx))))+((3.7500)*((exp(x))*(cos(x)))))/(dx^2)
error = b + c + d;
%Error
Error_left_sided_skew = abs(left_sided_skew- analytical_derivative);
end
Function Code - Skewed Right Difference Method
function Error_right_sided_skew = fourth_order_right_sided_skew(x,dx)
%Analytic function = exp(x)*cos(x)
%Analytic derivative
analytical_derivative = -2*exp(x)*sin(x);
%Right Sided Skew Differencing
right_sided_skew = (((3.7500)*((exp(x))*(cos(x))))-((12.8333)*((exp(x+dx))*(cos(x+dx))))+((17.8333)*((exp(x+(2*dx)))*(cos(x+(2*dx)))))-((13.0000)*((exp(x+(3*dx)))*(cos(x+(3*dx)))))+((5.0833)*((exp(x+(4*dx)))*(cos(x+(4*dx)))))-((0.8333)*((exp(x+(5*dx)))*(cos(x+(5*dx))))))/(dx^2);
%Error
Error_right_sided_skew = abs(right_sided_skew- analytical_derivative);
end
In this program we have compared three different approximation methods which are
1.Central difference method
2.Skewed left side difference method
3.Skewed right side difference method
We take the function for this program as exp(x)*cos(x).
We compare the second order differential values of the function selected using the three different methods and compare it with three above mentioned methods.
We develop taylor tables for all three methods and solve the values using a matrix solving method
AX=B
For finding X, X = B^-1 A
inverse of a matrix can be find out using inv(matrix) command.
After giving X values to the differential equation of all 3 schemes we solve the function to obtain its second derivative.
Graph is plotted using loglog command to get a better visualisation of output obtained.
Result
Conclusion
From the results and the plot got from the output of matlab program it is clear that the Central difference scheme is consistent and has lower errors when compared with other skewed methods.
The truncation error is much lower and steady in case of the central difference scheme when compared to both other skewed methods.
The reason for the above result is because, in central difference scheme data from both sides of the node is obtained and interpolated to obtain a result for the central node
The cases where a skewed difference method can be used are when calculating the boundary values or where nodes at both sides are not available, in such cases we can use skewed difference method where we need to compromise a bit on higher truncation errors than central differencing method.
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.