Project

Use of taylor table method and Matlab code to drive the 4th order approximations of the second-order derivative by the different methods

In this challenge, we are going to derive the 4 th order approximations of the second-order derivative by the following methods.

The methods we are going to use are :

1 Central Difference

2 Skewed right-sided difference

3 skewed left-sided difference

First , we Use here is

1 central Difference:

Numerical Stenical we will consider here is

(i-2)- -(i-1)- - (i)- - (i+1)- -(1+2)

2nd order derivative can be written as

D^2u/(dx)^2 = a.f(i-2)+b.f(i-1)+c.f(i)+d.f(i+1)+e.f(i+2)

Taylor’s Table we can compute is as

As we are dealing with five unknowns we have five equations to find the unknowns which can be done by using simple MatLab code .

\"\"

\"\"

\"\"

Function codes for each scheme are as follows to get the idea of the behave of error for known values of x and dx

%function code for central Difference

function e1=CD_second_order_derivative(x,dx)

analytical_derivative=-exp(x)*2*sin(x);

a=exp(x-2*dx)*cos(x-2*dx);
b=exp(x-1*dx)*cos(x-1*dx);
c=exp(x)*cos(x);
d=exp(x+1*dx)*cos(x+1*dx);
e=exp(x+2*dx)*cos(x+2*dx);

% Central differencing for Fourth-order approximation of 2nd order
% derivative

central_differencing=(-0.08333*a+1.3333*b+2.5*c+1.3333*d-0.08333*e)/dx^2;

e1=abs(analytical_derivative-central_differencing);

end 

%function code for skewed right- sided difference

function e2=SRSD_second_order_derivative(x,dx)

analytical_derivative=-exp(x)*2*sin(x)

c=exp(x)*cos(x);
d=exp(x+1*dx)*cos(x+1*dx);
e=exp(x+2*dx)*cos(x+2*dx);
f=exp(x+3*dx)*cos(x+3*dx);
g=exp(x+4*dx)*cos(x+4*dx);


% Skewed Right Side Difference  for Fourth-order approximation of 2nd order
% derivative

SRSD_differencing=(2.9167*c-8.6667*d+9.5*e-4.666*f+0.9167*g)/dx^2;

e2=abs(analytical_derivative-SRSD_differencing);

end 

%function code for skewed left- sided difference

function e3=SLSD_second_order_derivative(x,dx)

analytical_derivative=-exp(x)*2*sin(x);

 
j=exp(x-4*dx)*cos(x-4*dx);
h=exp(x-3*dx)*cos(x-3*dx);
a=exp(x-2*dx)*cos(x-2*dx);
b=exp(x-1*dx)*cos(x-1*dx);
c=exp(x)*cos(x);


% Central Differencing  for Fourth-order approximation of 2nd order
% derivative

SLSD_differencing=(2.9167*c-8.6667*b+9.5*a-4.6667*h+0.9167*j)/dx^2;

e3=abs(analytical_derivative-SLSD_differencing);

end 

MAIN PROGRAM CODE

clear all
close all
clc

% function f(x)=exp(x)*cos(x)
% Double derivative of functions f\"(x)= -exp(x)*2*sin(x)

% lets assume 
x=pi/2;
dx=linspace(pi/4,pi/400,10);

% Analytical D.derivative of function is 
analytical_derivative=-exp(x)*2*sin(x);

% for 4th order approximation

for i=1:length(dx)
    j(i)=exp(x-4.*dx(i))*cos(x-4.*dx(i));
    h(i)=exp(x-3.*dx(i))*cos(x-3.*dx(i));
    a(i)=exp(x-2.*dx(i))*cos(x-2.*dx(i));
    b(i)=exp(x-1.*dx(i))*cos(x-1.*dx(i));
    c(i)=exp(x)*cos(x);
    d(i)=exp(x+1.*dx(i))*cos(x+1.*dx(i));
    e(i)=exp(x+2.*dx(i))*cos(x+2.*dx(i));
    f(i)=exp(x+3.*dx(i))*cos(x+3.*dx(i));
    g(i)=exp(x+4.*dx(i))*cos(x+4.*dx(i));

    
    ERROR1(i)=CD_second_order_derivative(x,dx(i))
    ERROR2(i)=SRSD_second_order_derivative(x,dx(i))
    ERROR3(i)=SLSD_second_order_derivative(x,dx(i))

    % FORWARD DIFFERENCING
    forward_differencing=(c-2*d+e)/dx(i).^2;
    ERROR_FDS(i)=abs(forward_differencing(i)-analytical_derivative)
    
    % BACKWARD DIFFERENCING 
    backward_differencing=(c-2*b+a)/dx(i).^2;
    ERROR_BDS(i)=abs(backward_differencing(i)-analytical_derivative);
    
end

figure(1)
loglog(dx,ERROR1,\'b\',\'linewidth\',1.5)
hold on
loglog(dx,ERROR2,\'r\',\'linewidth\',1.5)
hold on
loglog(dx,ERROR3,\'y\',\'linewidth\',1.5)
hold on
xlabel(\'dx\');
ylabel(\'Absolute Errors\');
title(\'Comparision between FDS/BDS & CD/SRSD/SLSD schemes of 4th order approximation of 2nd order derivative\')
legend(\'CD\',\'SRSD\',\'SLSD\');
    


figure(2)
loglog(dx,ERROR1,\'b\',\'linewidth\',1.5)
hold on
loglog(dx,ERROR2,\'r\',\'linewidth\',1.5)
hold on
loglog(dx,ERROR3,\'y\',\'linewidth\',1.5)
hold on
loglog(dx,ERROR_FDS,\'K--\',\'linewidth\',0.5)
hold on
loglog(dx,ERROR_BDS,\'k-o\',\'linewidth\',0.5)
hold on
xlabel(\'dx\');
ylabel(\'Absolute Errors\');
title(\'Comparision between FDS/BDS & CD/SRSD/SLSD schemes of 4th order approximation of 2nd order derivative\')
legend(\'CD\',\'SRSD\',\'SLSD\',\'FDS\',\'BDS\');
    
    
    
    

the plot below

\"\"

\"\"

CONCULSIONS

  • initially, for the higher value of DX, SRSD gives a higher value of error comparatively to other approximations schemes ,but CD gives consistently lower valve for the error,
  • after some value of DX, error computing Through SRSD and SLSD coming closer but the value of error for CD goes on increasing but sill it is able to maintain an error lower than other schemes,
  • After many manipulations in the value of X, we will have completely different result .in the case, CD scheme error drastically gives rise whereas SLSD and SRSD able to give very much closer value to exact value .
  • Talking about FDS and BDS, both are very consistent in their error and closer to each other although they are providing higher value of error than the rest of schemes. They are having very lower accuracy but high precision we can expect.
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.