## Taylor series and Matlab code

1. Central Approximation :

(del^2f)/(delx^2)= 4th order approximtion using i-2,i-1,i,i+1 and i+2.

using Taylor series method,

(del^2f)/(delx^2)=afunderset(i-2) +bfunderset(i-1) +cfunderset(i)+dfunderset(i+1)+efunderset(i+2)

Rewritten n tabular form, we have

 f(i) Δxf’( i ) Δx2 fII(i) Δx3fIII(i) Δx4fIV(i) Δx5fV(i) Δx6fVI(i) a f(i-2) a -2a (4a)/2 -(8a)/6 (16a)/24 -(32a)/120 (64a)/720 b f(i-1) b -b b/2 -b/6 b/24 -b/120 b/720 c fi c 0 0 0 0 0 0 d f(i+1) d d d/2 d/6 d/24 d/120 d/720 e f(i+2) e 2e (4e)/2 (8e)/6 (16e)/24 (32e)/120 (64e)/720 0 0 1 0 0 ? ?

On further simplifiation,

We can make this in " AX =B" , Rewritting them in equation form,

a+b+c+d+e = 0                                       .....................(2)

-2a-b+0+d+2e = 0                                   .....................(3)

2a+b/2+0+d/2+2e = 0                           .....................(4)

-4a/3-b/6+0+d/6+4e/6 = 0                   .....................(5)

2a/3+b/24+0+d/24+2e/3 = 0               .....................(6)

These equations are solved using MATLAB code and the values of a b c d e are obtained.

Substituting the vaues of a b c d e on equation 1 we get the exact equation.

Skewed Left Approximaton :

(del^2f)/(delx^2) = 4th order approximation using i-4,i-3,i-2,i-1 and i.

Using Taylor series method,

(del^2f)/(delx^2)= afunderset(i-4)+bfunderset(i-3)+cfunderset(i-2)+dfunderset(i-1)+efunderset(i)

rewritting the taylor series ,

 f(i) ΔxfI(i) Δx2fII(i) Δx3fIII(i) Δx4fIV(i) Δx5fV(i) Δx6fVI(i) a f(i-4) a -4a (16a)/2 -(64a)/6 (256a)/24 -(1024a)/120 (4096a)/720 b f(i-3) b -3b (9b)/2 -(27b)/6 (81b)/24 -(243b)/120 (729b)/720 c f(i-2) c -2c (4c)/2 -(8c)/6 (16c)/24 -(32c)/120 (64c)/720 d f(i-1) d -d d/2 -d/6 d/24 -d/120 d/720 e fi e 0 0 0 0 0 0 0 0 1 0 0 ? ?

On further simplification,

This  can be rewritten in "AX = B" , re writting this in equation form,

a+b+c+d+e = 0                                           ...............(2)

-4a-3b-2c-d+0 = 0                                       ...............(3)

8a+9b/2+2c+d/2+0 = 0                             ...............(4)

-32a/3-9b/2-4c/3-d/6+0 = 0                     ...............(5)

32a/3+27b/8+2c/3+d/24+0 = 0               ...............(6)

These equations are solved using the MATLAB and the values are obtained for a b c d and e

Substituting the values of a b c d and e in equation 1 we get the exact equation.

SKEWED RIGHT APPROXIMATION :

(del^2f)/(delx^2) =4th order approximation using i,i+1,i+2,i+3 and i+4

using taylor series method,

(del^2f)/(delx^2)= afunderset(i)+bfunderset(i+1)+cfunderset(i+2)+dfunderset(i+3)+efunderset(i+4)

Rewritting the taylor equation,

 f(i) ΔxfI(i) Δx2fII(i) Δx3fIII(i) Δx4fIV(i) Δx5fV(i) Δx6fVI(i) a fi a 0 0 0 0 0 0 b f(i+1) b b b/2 b/6 b/24 b/120 b/720 c f(i+2) c 2c (4c)/2 (8c)/6 (16c)/24 (32c)/120 (64c)/720 d f(i+3) d 3d (9d)/2 (27d)/6 (81d)/24 (243d)/120 (729d)/720 e f(i+4) e 4e (16e)/2 (-64e)/6 (256e)/24 (1024e)/120 (4096e)/720 0 0 1 0 0 ? ?

On further simplification,

And this cn be rewritten in the form of "AX = B", again rewritting them we get

a+b+c+d+e = 0                                    ....................(2)

0+b+2c+3d+4e = 0                             .....................(3)

0+b/2+2c+9d/2+8e = 1                     .....................(4)

0+b/6+4c/3+9d/2+32e/3 = 0          ......................(5)

0+b/24+2c/3+27d/8+32e/3 = 0      ......................(6)

These equations are solved in MATLAB and we can obtain the values of a b c d and e

Substituting the values of a b c d and e in equation 1 we get the exact equation.

MATLAB CODE :

CENTRAL APPROXIMATION CODE :

function out = centraldifference (x, dx)

% analytical function is (exp(x))*(cos(x))
% for second order derivative f"(x)
% f"(x) = -2*exp(x)*sin(x)

analytical_derivative = -2*exp(x)*sin(x);
%central_difference
central_differencing = ((-1*exp(x-2*dx)*cos(x-2*dx))+(16*exp(x-dx)*cos(x-dx))-(30*exp(x)*cos(x))+(16*exp(x+dx)*cos(x+dx))-(1*exp(x+2*dx)*cos(x+2*dx)))/(12*(dx^2));
%error
out = abs(central_differencing - analytical_derivative);

end


SKEWED LEFT APPROXIMATION :

function out = skewedleftsided(x, dx)

% analytical function is (exp(x))*(cos(x))
% for second order derivative f"(x)
% f"(x) = -2*exp(x)*sin(x)

analytical_derivative = -2*exp(x)*sin(x);
%skewed left
skewed_left_sided = (35*exp(x)*cos(x)-104*exp(x-1*dx)*cos(x-1*dx)+114*exp(x-2*dx)*cos(x-2*dx)-56*exp(x-3*dx)*cos(x-3*dx)+11*exp(x-4*dx)*cos(x-4*dx))/(12*(dx^2));
%error
out = abs(skewed_left_sided - analytical_derivative);

end

SKEWED RIGHT APPROXIMATION:

function out = skewedrightsided (x, dx)

% analytical function is (exp(x))*(cos(x))
% for second order derivative f"(x)
% f"(x) = -2*exp(x)*sin(x)

analytical_derivative = -2*exp(x)*sin(x);
%skewed right
skewed_right_sided = (35*exp(x)*cos(x)-104*exp(x+dx)*cos(x+dx)+114*exp(x+2*dx)*cos(x+2*dx)-56*exp(x+3*dx)*cos(x+3*dx)+11*exp(x+4*dx)*cos(x+4*dx))/(12*(dx^2));
%error
out = abs(skewed_right_sided - analytical_derivative);

end


MAIN PROGRAM :

clear all
close all
clc

x = pi/3;
dx = linspace(pi/4,pi/400,30);

for i = 1:length(dx)

central_difference(i)= centraldifference (x, dx(i));
skewed_right(i)= skewedrightsided (x, dx(i));
skewed_left(i) = skewedleftsided(x, dx(i));

end

%plotting
loglog(dx,central_difference,'b')
hold on
loglog(dx,skewed_right,'g')
hold on
loglog(dx,skewed_left,'r')
title('Comparing 4th order approximations for second order derivative:')
legend('central difference','skewed right','skewed left')
%labeling
xlabel('dx')
ylabel('error')

RESULT :