Challenge

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

Aim:To write and exicute a MATLAB code for the fourth order approximtions of the second order derivativeusingTylore table method.

Introduction

Ealuating 4th order approximation using central difference , skewed right side difference ,skewed left sided difference and comparing the results with the analytical function expx*cos (x) and

  • Comparing the results of abosolute errors between the schemes by plots
  • Comparing the errors of FDS vs BDS approxiamtion
  • Comparing skewed scheme vs central scheme

Formulation for the tylore table method for CDS

`f ''(x) =(af(x-2h)+bf(x-h)+cf(x)+df(x+h)+e(x+2h))/h^2`

Formulation for the tylore table method of left schewed scheme

`f''(x)=(af(x)+bf(x-h)+cf(x-2h)+df(x-3h)cf(x-4h))/h^2`

Formulation tylore table method right skewed sheme

`f''(x)=(af(x)+bf(x-h)+cf(x+2h)df(x+3h)ef(x+4h))/h^2`

Forward difference scheme

`f''(x)=(af(x)+bf(x+h)+cf(x+2h))/h^2`

Backward difference scheme

`f''(x)=(af(x)+bf(x-h)+cf(x-2h))/h^2`

MATLAB CODE

clear all
close all
clc

x=pi/3;
dx=linspace(0.01,1,10);

for i= 1:length(dx)
    error_second_order_forward(i)=second_order_forward_approx(x,dx(i));
    error_second_order(i)=second_order_approx(x,dx(i));
    error_central_difference(i)=fourth_central(x,dx(i));
    error_skewed_Right(i)=fourth_skewed_right(x,dx(i));
    error_skewed_Left(i)=fourth_skewed_left(x,dx(i));
end

loglog(dx,error_second_order_forward,'r',dx,error_second_order,'b',dx,error_second_order,'k',dx,error_central_difference,'m',dx,error_skewed_Right,'g',dx,error_skewed_Left,'y');
title('Fourth order approximation');
grid on
xlabel('step size "dx"');
ylabel('Error dx');
legend('Central','Right','Left','FDS','BDS')

%SECOND ORDER FORWARD

function error_second_order_forward = second_order_forward_approx(x,dx)
analytical_derivative = -2*sin(x)*exp(x);
second_order =((exp(x+2*dx)*cos(x+2*dx))+(-2*exp(x+dx)*cos(x+dx))+(exp(x)*cos(x)))/dx^2;
error_second_order_forward =abs(analytical_derivative - second_order);

%SECOND ORDER 
function error_second_order = second_order_approx(x,dx)
analytical_derivative = -2*sin(x)*exp(x);

second_order = ((exp(x-2*dx)*cos(x-2*dx))+(-2*exp(x-dx)*cos(x-dx))+(exp(x)*cos(x)))/dx^2;
error_second_order =abs(analytical_derivative - second_order);

%fourth order skewed right

function error_skewed_Right =fourth_skewed_right(x,dx)
analytical_derivative = -2*sin(x)*exp(x);
skewed_right =((2.9167*cos(x)*exp(x))+(-8.667*cos(x+dx)*exp(x+dx))+(9.5*cos(x+2*dx)*exp(x+2*dx))+(-4.667*cos(x+3*dx)*exp(x+3*dx))+(0.9167*cos(x+4*dx)*exp(x+4)))/dx^2;
error_skewed_Right = abs(analytical_derivative - skewed_right);

%fourth order skewed left

function error_skewed_Left=fourth_skewed_left(x,dx)

analytical_derivative = -2*sin(x)*exp(x);
skewed_left =((0.91667*cos(x)*exp(x))+(-4.667*cos(x-dx)*exp(x-dx))+(9.5*cos(x-2*dx)*exp(x-2*dx))+(-8.667*cos(x-3*dx)*exp(x-3*dx))+(2.9167*cos(x-4*dx)*exp(x-4*dx)))/dx^2
error_skewed_Left = abs(analytical_derivative - skewed_left)

%Fourth order central difference scheme
function error_central_difference = fourth_central(x,dx)
analytical_derivative = -2*sin(x)*exp(x);
central_difference =((-0.0833*cos(x-2*dx)*exp(x-2*dx))+(1.3333*cos(x-dx)*exp(x-dx))+(-2.5*cos(x)*exp(x))+(1.3333*cos(x+dx)*exp(x+dx))+(-0.0833*cos(x+2*dx)*exp(x+2*dx)))/dx^2;
error_central_difference = abs(analytical_derivative - central_difference);
end

Conclusion

As we seen on the above plot , cental difference gives very less error compare to other schemes

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.