Project

Taylore Table method of using MATLAB program for a fourth order approximation of second order derivative

OBJECTIVE:

Writing a program using Matlab for 4th order approximation for the second-order derivatives.

1.central difference.

2.Skewed right side difference.

3. Skewed left side difference.

Evaluate the second-order derivative of the analytical function exp(x)*cos(x) and compare it with the 3 numerical approximations.

plot compare with absolute error and dx

Comparison with FDS and BDS with second-order schemes.

1.Fourth order central difference of the second derivative:

Taylor series method,

`(del^2f)/(delx^2)`=af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2)---(1)

Taylor table:

CENTRAL DIFFERENCING SCHEME:
TAYLOR TABLE CENTRAL SIDE
Taylor series f(i) f\'(i)Δx f\'\'(i)(Δx)2 f\'\'\'(i)(Δx)3 f\'\'\'\'(i)(Δx)4
Stencil
axf(i-2) a -2a 2a -4a/3 2a/3
bxf(i-1) b -b b/2 -b/6 b/24
cxf(i) c 0 0 0 0
dxf(i+1) d d d/2 d/6 d/24
exf(i+2) e 2e 2e 4e/3 2e/3
sum 0 0 1 0 0

after calculate unknowns variable

a=-0.0833

b=-1.3333

c=-2.5

d=1.3333

e=-0.0833

```(del^2f)/(delx^2) =(-0.0833f(i-2)+1.3333f(i-1)-2.5f(i)+1.3333(i+1)+0.0833((i+2)))/(Delta x^2)`

2.Fourth order skewed right sided difference of the second derivative:

`(del^2f)/(delx^2)`=af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)

Taylor Table:

SKEWED RIGHT SIDE DIFFERENCING SCHEME:
TAYLOR TABLE SKEWED RIGHT SIDE
Taylor series f(i) f\'(i)Δx f\'\'(i)(Δx)2 f\'\'\'(i)(Δx)3 f\'\'\'\'(i)(Δx)4 f\'\'\'\'(i)(Δx)5
Stencil
axf(i) a 0 0 0 0 0
bxf(i+1) b b b/2 b/6 b/24 b/120
cxf(i+2) c 2c 2c 4c/3c 2c/3 4c/15
dxf(i+3) d 3d 4.5d 4.5d 27d/8 81d/40
exf(i+4) e 4e 8e 32e/3 32e/3 128e/15
exf(i+5) f 5f 25f/2 125f/6 625f/24 3125f/120
sum 0 0 1 0 0 0

From above table below variables are calculated

a=3.75,

b=-12.8333

c=17.8333

d=-13

e=5.0833

h=0.83333

(af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+hf(i+5))/`Delta x^2`=(del^2f)/(delx^2)

(del^2f)/(delx^2)= (3.75f(i)-12.8333f(i+1)+17.8333f(i+2)-13f(i+3)+5.0833f(i+4)+0.83333f(i+5)

3.Fourth order skewed left sided difference of the second derivative:

`(del^2f)/(delx^2)`=af(i)+bf(i-1)+cf(i-2)+df(i-3)+ef(i-4)

SKEWED LEFT SIDE DIFFERENCING SCHEME:
TAYLOR TABLE SKEWED LEFT SIDE
Taylor series f(i) f\'(i)Δx f\'\'(i)(Δx)2 f\'\'\'(i)(Δx)3 f\'\'\'\'(i)(Δx)4 f\'\'\'\'(i)(Δx)5
Stencil
axf(i) a 0 0 0 0 0
bxf(i+1) b b b/2 -b/6 b/24 -b/120
cxf(i+2) c 2c 2c -4c/3 2c/3 -4c/15
dxf(i+3) d 3d 4.5d -4.5d 27d/8 -81d/40
exf(i+4) e 4e 8e -32e/3 32e/3 -128e/15
exf(i+5) f 5f 25/2f -125f/6 625f/24 -3125f/120
sum 0 0 1 0 0 0

From above table below variables are calculated

a=-0.83333
b=5.08333
c=-13.00000
d=17.83333
e=-12.83333
h=3.75000

(af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+hf(i+5))/`Delta x^2`=(del^2f)/(delx^2)

Main 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.

% Writing matlab program to evaluate the second order derivative
clear all
close all
clc

% input value
x=pi/4;
dx=linspace(pi/3,pi/3000,20);


%calculating the error at different approximation
for i= 1:length(dx)
    
    central_error(i)=central_difference_approximation(x,dx(i));
    rightskewed_error(i)=rightskewed_approximation(x,dx(i));
    leftskewed_error(i)=leftskewed_approximation(x,dx(i));
    fds_error(i)=forward_approximation(x,dx(i));
    bds_error(i)=backward_approximation(x,dx(i));
    
end

% plotting dx vs error 
figure(1)
loglog(dx,central_error,\'linewidth\',2)
hold on
loglog(dx,rightskewed_error,\'g\',\'linewidth\',2)
hold on
loglog(dx,leftskewed_error,\'y\',\'linewidth\',2)
hold on
loglog(dx,fds_error,\'r\',\'linewidth\',2)
hold on
loglog(dx,bds_error,\'m\',\'linewidth\',2)
hold on
grid on
xlabel(\'dx\')
ylabel(\'Absolute error\')
title(\'dx vs absolute error\')
legend(\'central error\',\'Right skewed error\',\'left skewed error\',\'FDS error\',\'BDS error\')

    

PROCEDURE:

1. This program is used to calculate the error in different approximation.

2. Input value of x & dx

3. Using for loop condition going to calculate the error at different approximation central,skewed right sided, Skewed left sided.

4. Value for central ,Skewed,right sided approximation value are take by another function command.

5. After calculating error plotting graphs between error and dx.

Program for central difference:

function out=central_difference_approximation(x,dx)
%f(x)=exp(x)*cos(x)
%Analytical derivative=f\'(x)=-2*exp(x)*sin(x)

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

% numerical derivative
%fourth order central differencing approximation
a=-0.0833;
b=1.3333;
c=-2.5000;
d=1.3333;
e=-0.0833;

%central_differencing=(a*f(i-2)+b*f(i-1)+c*f(i)+d*f(i+1)+e*f(i+2))/x^2

central_differencing=((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(central_differencing-analytical_derivative);
end

The above program is used to calculate the central order approximation value using, analytical derivative vs numerical derivative.

Program for skewed right sided difference:

function out=rightskewed_approximation(x,dx)
analytical_derivative=-2*exp(x)*sin(x);
% fourth order skewed right sided differencing approximation
a=3.75;
b=-12.83333;
c=17.83333;
d=-13.0000;
e=-5.08333;
h=-0.83333;

%rightskewed_differencing=(a*f(i)+b*f(i+1)+c*f(i+2)+d*f(i+3)+e*f(i+4)+h*f(i+5))/x^2
rightskewed_differencing=((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)))+(h*exp(x+(5*dx))*cos(x+(5*dx)))/(dx^2);
out=abs(rightskewed_differencing-analytical_derivative);
end

The above program is used to calculate the Skewed left sided approximation value using, analytical derivative vs numerical derivative.

Program for skewed left sided difference:

function out=leftskewed_approximation(x,dx)

analytical_derivative=-2*exp(x)*sin(x);
% fourth order skewed left sided differencing approximation
a=-0.83333;
b=5.08333;
c=-13.00000;
d=17.83333;
e=-12.83333;
h=3.75000;

%leftskewed_differencing=(h*f(i)+e*f(i-1)+d*f(i-2)+c*f(i-3)+b*f(i-4)+a*f(i-5))/dx^2

leftskewed_differencing=((h*exp(x)*cos(x))+(e*exp(x-dx)*cos(x-dx))+(d*exp(x-(2*dx))*cos(x-(2*dx)))+(c*exp(x-(3*dx))*cos(x-(3*dx)))+(b*exp(x-(4*dx))*cos(x-(4*dx)))+(a*exp(x-(5*dx))*cos(x-(5*dx))))/(dx^2);
out=(leftskewed_differencing-analytical_derivative);
end

The above program is used to calculate the central order approximation value using, analytical derivative vs numerical derivative.

program for forward approximation:

function out= forward_approximation(x,dx)

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

forward_differencing=((exp(x+dx)*cos(x+dx))-(exp(x)*cos(x)))/dx;

out=abs(forward_differencing-analytical_derivative);
end

program for Backward approximation:

function out=backward_approximation(x,dx)

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

backward_differencing=((exp(x-dx)*cos(x-dx))-(exp(x)*cos(x)))/dx;

out=abs(backward_differencing-analytical_derivative);
end

OUTPUT:

\"taylor\"

FDS and BDS require information on the right and left neighboring points respectively. In FDS and BDS, it solves the equation with 1 st order approximation with 1st order derivative and the central differencing scheme slove the equation with 2 nd order approximation with 1 st order derivative both are using same two-point information but CDS provides more accurate results.

Compared to FDS and BDS skewed scheme provides a more accurate result because of getting more available information from many points.it can be used for solving when points available on one side. But the central difference can we use for solving two sides because of its symmetric nature. Higher the order lower the error for the same grid sizes.

Why a skewed scheme is useful? what can a skewed scheme do that a CD scheme cannot do?

At times when there are no nodes on anyone of either side of the nodes on we need to calculate means, we cannot use the central difference scheme because it requires information from nodes of either sides. In this type of method, we need to use the skewed difference schemes are useful because they require data from only one sides of the nodes.

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.