Week 3.5 - Deriving 4th order approximation of a 2nd order derivative using Taylor Table method
AIM:
Using the Taylor table method, the central differencing scheme, the skewed right sided difference, and the skewed left sided difference are used to derive the fourth order approximations of a second order derivative. The analytical derivative is then compared to the values obtained for the given function.’
derivation explanations provided in the word document
clear all
close all
clc
%value of x
x= pi/3;
%range of dx
dx= linspace(pi/4,pi/4000,20);
%for loop
for i=1:length(dx)
central_differencing(i) = fourth_order_central_differencing(x,dx(i));
right_sided_skew(i) = fourth_order_right_sided_skew(x,dx(i));
left_sided_skew(i) = fourth_order_left_sided_skew(x,dx(i));
end
%plotting
figure(1)
loglog(dx,central_differencing,'b')
hold on
loglog(dx,right_sided_skew,'r')
hold on
loglog(dx,left_sided_skew,'y')
xlabel('Range of dx','color','r')
ylabel('Truncation error','color','r')
title('dx range vs error','color','b')
legend('the central differencing','right sided skewed difference','left sided skewed difference','location','northwest')
function Error_central_differencing = fourth_order_central_differencing(x,dx)
% analytical function = exp(x)*cos(x)
% analytical derivative is
%f''(x) = -2*exp(x)*sin(x)
analytical_derivative = -2*exp(x)*sin(x);
%numerical derivative is
%central differencing method derivatives as follows
central_differencing = (((-0.0833)*((exp(x-(2*dx)))*(cos(x-(2*dx)))))+((1.3333)*((exp(x-dx))*(cos(x-dx))))-((2.5000)*((exp(x))*(cos(x))))+((1.3333)*((exp(x+dx))*(cos(x+dx))))-((0.0833)*((exp(x+(2*dx)))*(cos(x+(2*dx))))))/(dx^2);
%error value defining
Error_central_differencing = abs(central_differencing- analytical_derivative);
end
function Error_right_sided_skew = fourth_order_right_sided_skew(x,dx)
% analytical function = exp(x)*cos(x)
% analytical derivative is
%f''(x) = -2*exp(x)*sin(x)
analytical_derivative = -2*exp(x)*sin(x);
%numerical derivative as follows
%central differencing method derivatives
right_sided_skew = (((3.7500)*((exp(x))*(cos(x))))-((12.8333)*((exp(x+dx))*(cos(x+dx))))+((17.8333)*((exp(x+(2*dx)))*(cos(x+(2*dx)))))-((13.0000)*((exp(x+(3*dx)))*(cos(x+(3*dx)))))+((5.0833)*((exp(x+(4*dx)))*(cos(x+(4*dx)))))-((0.8333)*((exp(x+(5*dx)))*(cos(x+(5*dx))))))/(dx^2);
%error value defining
Error_right_sided_skew = abs(right_sided_skew- analytical_derivative);
end
function Error_left_sided_skew = fourth_order_left_sided_skew(x,dx)
% analytical function = exp(x)*cos(x)
% analytical derivative is
%f''(x) = -2*exp(x)*sin(x)
analytical_derivative = -2*exp(x)*sin(x);
%numerical derivative as folows
%central differencing method derivatives
left_sided_skew = (((-0.8333)*((exp(x-(5*dx)))*(cos(x-(5*dx)))))+((5.0833)*((exp(x-(4*dx)))*(cos(x-(4*dx)))))-((13.0000)*((exp(x-(3*dx)))*(cos(x-(3*dx)))))+((17.8333)*((exp(x-(2*dx)))*(cos(x-(2*dx)))))-((12.8333)*((exp(x-dx))*(cos(x-dx))))+((3.7500)*((exp(x))*(cos(x)))))/(dx^2);
%error value defining
Error_left_sided_skew = abs(left_sided_skew- analytical_derivative);
end
clear all
close all
clc
%value of x
x= pi/3;
%range of dx
dx= linspace(pi/4,pi/4000,20);
%for loop
for i=1:length(dx)
central_differencing(i) = fourth_order_central_differencing(x,dx(i));
right_sided_skew(i) = fourth_order_right_sided_skew(x,dx(i));
left_sided_skew(i) = fourth_order_left_sided_skew(x,dx(i));
end
%plotting
figure(1)
loglog(dx,central_differencing,'b')
hold on
loglog(dx,right_sided_skew,'r')
hold on
loglog(dx,left_sided_skew,'y')
xlabel('Range of dx','color','r')
ylabel('Truncation error','color','r')
title('dx range vs error','color','b')
legend('the central differencing','right sided skewed difference','left sided skewed difference','location','northwest')