Challenge

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

Given function:

f = exp(x)*cos(x)

2nd order derivative of the function

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

1) Developing Taylor Table for the Forward Differencing Scheme Approximation method

For FDS, f = (af(x) + bf(x+dx) + cf(x+2dx) + df(x+3dx) + ef(x+4dx))/(dx^2)

Table:

2) Developing Taylor Table for the Backward Differencing Scheme Approximation method

For BDS, f = (af(x) + bf(x-dx) + cf(x-2dx) + df(x-3dx) + ef(x-4dx))/(dx^2)

Table:

1) Developing Taylor Table for the Central Differencing Scheme Approximation method

For CDS, f = (af(x-2dx) + bf(x-dx) + cf(x) + df(x+dx) + ef(x+2dx))/(dx^2)

Table:

Coefficient for all three approximation methods will be calcualted in MATLAB only.

Code:

Main Code:

% Project Name: Deriving 4th order approximation of a 2nd order derivative
% using Taylor Table method (Week 3.5 Challenge)

% Methodology
% Taylor Table method will be used to determine the coeeficient of each
% term & also the truncation error will be derived finding difference
% between analytical method & their 2nd order derivative of respective
% approximation method. 

% Table for Taylor method has been created for each approximation method
% which will be used to calculate the coefficient by converting them into
% matrix form in the MATLAB. 

clear all; close all; clc;

% Initial point
x = pi/5;
% Given function
fun = exp(x)*cos(x);
% Steps size
dx = linspace(pi/40, pi/200, 40);

% analytical function = exp(x)*cos(x)
% first derivative = -exp(x)*(sin(x)-cos(x))
% second derivative = -2*exp(x)*sin(x)
analytical_derivative = -2 * exp(x) * sin(x);

% Findind error between analytical & numerical derivative
for i = 1:length(dx)
    % Truncation errr for FDS
    error_FDS(i) = FDS_derivative(x,dx(i),analytical_derivative);
    % Truncation errr for BDS
    error_BDS(i) = BDS_derivative(x,dx(i),analytical_derivative);
    % Truncation errr for CDS
    error_CDS(i) = CDS_derivative(x,dx(i),analytical_derivative);
end

% Plotting
% loglog command will be used to plot becaise truncation errro is in the
% range of 10^6.
loglog(dx, error_FDS, 'color', 'r')
hold on
loglog(dx, error_CDS, 'color', 'g')
loglog(dx,error_BDS, 'color', 'b')
hold off
xlabel('dx')
ylabel('Error')
title('Error Comparision between Numerical Approximation Methods')
legend('FDS', 'CDS', 'BDS', 'location', 'southeast')

For FDS:

function error_FDS = FDS_derivative(dx, x, analytical_derivative)

% This function is created to determine the coefficient for FDS & than the
% calculating the error between the numnerical approximation method & the
% analytical method.

% Calculating coefficients for FDS
D = [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];
e = [0; 0; 1; 0; 0];
coeff = inv(D)*e;

% 4th order approximation of 2nd order derivative of the given function
% using Taylor Table method
% FDS_derivative = (af(x) + bf(x+dx) + cf(x+2dx) + df(x+3dx) + ef(x+4dx))/(dx^2)

% Dividing above equation in smaller terms
y(1) = exp(x)*cos(x);
y(2) = exp(x+dx)*cos(x+dx);
y(3) = exp(x + 2*dx)*cos(x + 2*dx);
y(4) = exp(x + 3*dx)*cos(x + 3*dx);
y(5) = exp(x + 4*dx)*cos(x + 4*dx);

FDS_derivative = ((coeff(1)*y(1)) + coeff(2)*y(2) +coeff(3)*y(3) + coeff(4)*y(4) + coeff(5)*y(5))/(dx^2);

% Truncation Error for FDS
error_FDS = abs(analytical_derivative - FDS_derivative);

For BDS:

function error_BDS = BDS_derivative(x, dx, analytical_derivative)

% This function is created to determine the coefficient for BDS & than the
% calculating the error between the numnerical approximation method & the
% analytical method. 

% Calculating coefficients for BDS
G = [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];
h = [0; 0; 1; 0; 0];
coeff = inv(G)*h;

% 4th order approximation of 2nd order derivative of the given function 
% using Taylor Table method
% BDS_derivative = (af(x) + bf(x-dx) + cf(x-2dx) + df(x-3dx) + ef(x-4dx))/(dx^2)

% Dividing above equation in smaller terms
y(1) = exp(x)*cos(x);
y(2) = exp(x-dx)*cos(x-dx);
y(3) = exp(x - 2*dx)*cos(x - 2*dx);
y(4) = exp(x - 3*dx)*cos(x - 3*dx);
y(5) = exp(x - 4*dx)*cos(x - 4*dx);
 
BDS_derivative = ((coeff(1)*y(1)) + coeff(2)*y(2) +coeff(3)*y(3) + coeff(4)*y(4) + coeff(5)*y(5))/(dx^2);

% BDS truncation error
error_BDS = abs(analytical_derivative - BDS_derivative);

For CDS:

function error_CDS = CDS_derivative(x, dx, analytical_derivative)

% This function is created to determine the coefficient for CDS & than the
% calculating the error between the numnerical approximation method & the
% analytical method. 

% Calculating coefficients for CDS
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];
b = [0; 0; 1; 0; 0];
coeff = inv(A)*b;

% 4th order approximation of 2nd order derivative of the given function 
% using Taylor Table method
% FDS_derivative = (af(x-2dx) + bf(x-dx) + cf(x) + df(x+dx) + ef(x+2dx))/(dx^2)

% Dividing above equation in smaller terms
y(1) = exp(x + 2*dx)*cos(x + 2*dx);
y(2) = exp(x+dx)*cos(x+dx);
y(3) = exp(x)*cos(x);
y(4) = exp(x - dx)*cos(x - dx);
y(5) = exp(x - 2*dx)*cos(x - 2*dx);

CDS_derivative = ((coeff(1)*y(1)) + coeff(2)*y(2) +coeff(3)*y(3) + coeff(4)*y(4) + coeff(5)*y(5))/(dx^2);

% CDS truncation error
error_CDS = abs(analytical_derivative - CDS_derivative);

Graph to compare errors in all three approximation methods:

The skewed differencing schemes, forward & backward, are very useful when the data is available of previous points, in case of backward, or the data is available for future points, as in case of forward. The approximation method will able to determine the value of current point as per the 4th order accuracy in either direction. While central differencing schemes wll be able to calculate in 2 points accuracy in either direction.

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.