Challenge

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

Discretization is the process through which we can transform continuous variables, models or functions into a discrete form. We do this by creating a set of contiguous intervals (or bins) that go across the range of our desired variable/model/function.

Mathematical problems with contentious data have infinite degrees of freedom, and because of the limit of the computational power of any system, we need to discretize the problem.

To solve for the approximations we will compute the Taylor table for each case and determine the coefficient of all approximate schemes.

Left Skewed difference Scheme

Also known as the ‘backward difference scheme’, can be approximated, but first, we need to determine the number of discrete points upon which we will operate and solve the Taylor series.

Number of nodes = order of accuracy + order of derivative

(This applies for skewed schemes only.)

Therefore , number of nodes = 4 + 2 = 6 i.e. the stancil needs to have 6 points

--------*--------------*----------*---------*------------*----------*------

i-5 i-4 i-3 i-2 i-1 i

Here the 6 points on the stencil are assumed to be (i-5), (i-4), (i-3), (i-2), (i-1), (i);

Central Difference Scheme

This is a symmetrical scheme and requires points on either side of the point of concern to compute a stencil to get the best possible results. It can be approximated, but first, we need to determine the number of discrete points upon which we will operate and solve the Taylor series.

Number of nodes = order of accuracy + order of derivative - 1

Therefore , number of nodes = 4 + 2 - 1 = 5 i.e. the stancil needs to have 5 points

--------*--------------*----------*---------*------------*-------

i-2 i-1 i i+1 i+2

Here the 5 points on the stencil are assumed to be (i-2), (i-1), (i), (i+1), (i+2);

Right Skewed Difference Scheme

Also known as the ‘forward difference scheme’, can be approximated, but first, we need to determine the number of discrete points upon which we will operate and solve the Taylor series.

Number of nodes = order of accuracy + order of derivative

(This applies for skewed schemes only.)

Therefore , number of nodes = 4 + 2 = 6 i.e. the stancil needs to have 6 points

--------*--------------*----------*---------*------------*----------*------

i i+1 i+2 i+3 i+4 i+5

Here the 6 points on the stencil are assumed to be (i+5), (i+4), (i+3), (i+2), (i+1), (i);

-------------------------------------------------------------------------------------------------------------------------------------------

Writing of the codes

%% Left Skewed Scheme or Backward Difference
function [output_left] = Taylor_Table_Backward_Diff(x,dx)
%% Analytical Derivative
% Firstly introducing the analytical derivative
Analytical_der = -2*exp(x)*sin(x);
% Finding the coefficients for backward difference with the help of taylor table
A = [1, 1, 1, 1, 1, 1; 0, -1, -2, -3, -4, -5; 0/2, 1/2, 4/2, 9/2, 16/2, 25/2; 0, -1/6, -8/6, -27/6, -64/6, -125/6; 0, 1/24, 16/24, 81/24, 256/24, 625/24; 0, -1/120, -32/120, -243/120, -1024/120, -3125/120];
B = [0; 0; 1; 0; 0; 0];
C = linsolve(A,B);
for i = 1:length(dx)
    Backward_diff(i) = (C(1)*((exp(x))*(cos(x))) + C(2)*((exp(x-dx(i)))*(cos(x-dx(i)))) + C(3)*((exp(x-(2*dx(i))))*(cos(x-(2*dx(i))))) + C(4)*((exp(x-(3*dx(i))))*(cos(x-(3*dx(i))))) + C(5)*((exp(x-(4*dx(i))))*(cos(x-(4*dx(i))))) + C(6)*((exp(x-(5*dx(i))))*(cos(x-(5*dx(i))))))/(dx(i)^2);
end
%% Error Computation
output_left = abs(Analytical_der - Backward_diff);
end
%% Central Difference Symmetric scheme
function [output_center] = Taylor_Table_Center_Diff(x,dx)
%% Analytical Derivative
% Firstly introducing the analytical derivative
Analytical_der = -2*exp(x)*sin(x);
%% Numerical Derivative
% Finding the coefficients for backward difference with the help of taylor table
A = [1, 1, 1, 1, 1, ; -2, -1, 0, 1, 2; 2, 1/2, 0, 1/2, 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];
C = linsolve(A,B);
for i = 1:length(dx)
    Central_diff(i) = (C(1)*((exp(x-(2*dx(i))))*(cos(x-(2*dx(i))))) + C(2)*((exp(x-dx(i)))*(cos(x-dx(i)))) + C(3)*((exp(x))*(cos(x))) + C(4)*((exp(x+dx(i)))*(cos(x+dx(i)))) + C(5)*((exp(x+(2*dx(i))))*(cos(x+(2*dx(i))))))/(dx(i)^2);
end
%% Error Computation
output_center = abs(Analytical_der - Central_diff);
end
%% Right skewed Scheme or forwarde difference
function [output_righ] = Taylor_Table_Forward_Diff(x,dx)
%% Analytical Derivative
% Firstly introducing the analytical derivative
Analytical_der = -2*exp(x)*sin(x);
%% Numerical Derivative
% Finding the coefficients for backward difference with the help of taylor table
A = [1, 1, 1, 1, 1, 1; 0, 1, 2, 3, 4, 5; 0/2, 1/2, 4/2, 9/2, 16/2, 25/2; 0, 1/6, 8/6, 27/6, 64/6, 125/6; 0, 1/24, 16/24, 81/24, 256/24, 625/24; 0, 1/120, 32/120, 243/120, 1024/120, 3125/120];
B = [0; 0; 1; 0; 0; 0];
C = linsolve(A,B);
for i = 1:length(dx)
    Forward_diff(i) = (C(1)*((exp(x))*(cos(x))) + C(2)*((exp(x+dx(i)))*(cos(x+dx(i)))) + C(3)*((exp(x+(2*dx(i))))*(cos(x+(2*dx(i))))) + C(4)*((exp(x+(3*dx(i))))*(cos(x+(3*dx(i))))) + C(5)*((exp(x+(4*dx(i))))*(cos(x+(4*dx(i))))) + C(6)*((exp(x+(5*dx(i))))*(cos(x+(5*dx(i))))))/(dx(i)^2);
end
%% Error Computation
output_righ = abs(Analytical_der - Forward_diff);
end
%% Calling the function
clear all
close all
clc
% In the taylor table method we are provided with a function.
% Function = exp(x)*cos(x)
x = pi/3;
dx = linspace(pi/400,pi/7,1000);

%% Calling the left skewed, Central, and Right Skewed difference function in a common place to be compared
% Calling central diff function
Central_Diff = Taylor_Table_Center_Diff(x,dx);
% Calling Skewed Right diff function
Skewed_Right_Diff = Taylor_Table_Forward_Diff(x,dx);
% Calling Skewed Left diff function
Skewed_Left_Diff = Taylor_Table_Backward_Diff(x,dx);

%% Plotting the errors with respect  to change in 'dx'
figure(1)
plot(dx,Central_Diff,'Color','r',"LineWidth",2);
hold on
plot(dx,Skewed_Right_Diff,'Color','b',"LineWidth",2);
hold on
plot(dx,Skewed_Left_Diff,'Color','g',"LineWidth",2);
xlabel('Change in dx');
ylabel('Errors');
title('4th order scheme of 2nd derivative');
legend('Central Difference','Right Skewed Difference','Left Skewed Difference');

figure(2)
loglog(dx,Central_Diff,'Color','r',"LineWidth",2);
hold on
loglog(dx,Skewed_Right_Diff,'Color','b',"LineWidth",2);
hold on
loglog(dx,Skewed_Left_Diff,'Color','g',"LineWidth",2);
xlabel('Change in dx');
ylabel('Errors');
title('4th order scheme of 2nd derivative log chart');
legend('Central Difference','Right Skewed Difference','Left Skewed Difference');

Drive Link: https://drive.google.com/drive/folders/174huE2EoHDLBd0ejEFXMGCt9wBYUDExx?usp=sharing

Report Doc Link: https://docs.google.com/document/d/1cGV51ELtI3bi4nItpFl6JKWOROax6VkVT9GL8p6vv1g/edit?usp=sharing

Here it is quite apparent that in the first graph the skewed schemes the errors are smaller than the central difference scheme. One of the reasons for that is in the central scheme the points are supposed to be computed on both sides of the point of concern, but this method is problematic then we are at corners or at the boundary of the system and there is no further point that we can use to compute in our scheme what is why the right and left-skewed scheme are better options to compute results in this type of Syneron.

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.