Challenge

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

AIM: TO COMPARE THE ERRORS OCCURING IN 4TH ORDER APPROXIMATION FOR 2ND ORDER DERIVATIVE WHILE USING SKEWED RIGHT SIDED DIFFERENCE METHOD, CENTRAL DIFFERENCE METHOD AND SKEWED LEFT HANDED DIFFERENCE METHOD.

A simple matlab code has been written in order to compare the results. From the graphs it can be seen that the least error occurs when we use central difference technique while it is the most in the case of right handed difference method. But at the same time to use central diffence technique we can calculate the derivative of only those points which have the neighbouring points both side (2D). We cannot calculate the derivative of the points which are on extreme left or extreme right. We have to use right/left handed difference scheme to calculate the derivatives of such points.

close all
clear all
clc

%f(x) = exp(x)*cos(x)
%2nd order derivative of f(x) = -2*exp(x)*sin(x)
%taking values of required parameters
x = pi/3;
dx = [pi/100:pi/400:pi/10];
analytical_derivative = -2*exp(x)*sin(x);

%SKEWED RIGHT SIDED DIFFERENCE METHOD
A_r = [1 1 1 1 1;0 1 2 3 4;0 1/2 2 9/2 8;0 1/6 8/6 27/6 64/6;0 1/24 16/24 81/24 256/24];
B_r = [0;0;1;0;0];
C_r = A_r\B_r;
a = C_r(1);
b = C_r(2);
c = C_r(3);
d = C_r(4);
e = C_r(5);
righthanded_skew = (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))))./(dx.^2);

%%CENTRAL DIFFERENCE METHOD
A_c = [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_c = [0;0;1;0;0];
C_c = A_c\B_c;
a_c = C_c(1);
b_c = C_c(2);
c_c = C_c(3);
d_c = C_c(4);
e_c = C_c(5);
central_scheme = (a_c.*(exp(x-(2.*dx)).*cos(x-(2.*dx))) + b_c.*(exp(x-dx).*cos(x-dx)) + c_c.*(exp(x).*cos(x)) + d_c.*(exp(x+dx).*cos(x+dx)) + e_c.*(exp(x+(2.*dx)).*cos(x+(2.*dx))))./(dx.^2);

%%SKEWED LEFT HANDED DIFFERENCE METHOD
A_l = [1 1 1 1 1;-4 -3 -2 -1 0;8 9/2 2 1/2 0;-64/6 -27/6 -8/6 -1/6 0;256/24 81/24 16/24 1/24 0];
B_l = [0;0;1;0;0];
C_l = A_l\B_l;
a_l = C_l(1);
b_l = C_l(2);
c_l = C_l(3);
d_l = C_l(4);
e_l = C_l(5);
lefthanded_skew = (a_l.*(exp(x-(4.*dx)).*cos(x-(4.*dx))) + b_l.*(exp(x-(3.*dx)).*cos(x-(3.*dx))) + c_l.*(exp(x-(2.*dx)).*cos(x-(2.*dx))) + d_l.*(exp(x-dx).*cos(x-dx)) + e_l.*(exp(x).*cos(x)))./(dx.^2);

%CALCULATION OF ERROR
righthanded_error = abs(analytical_derivative-righthanded_skew);
central_scheme_error = abs(analytical_derivative-central_scheme);
lefthanded_error = abs(analytical_derivative-lefthanded_skew);

loglog(dx,righthanded_error,'linewidth',2,'marker','*')
grid on
hold on
loglog(dx,central_scheme_error,'linewidth',2,'marker','o')
hold on
loglog(dx,lefthanded_error,'linewidth',2,'Marker',"x")
legend('right_e','central_e','left_e')
title('Error v/s dx plot on logarithmic scale')
xlabel('dx')
ylabel('Error')

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.