Week 3.5 - Deriving 4th order approximation of a 2nd order derivative using Taylor Table method
CODE:
clear all
close all
clc
x= pi/3;
dx= linspace(pi/4,pi/4000,20);
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
figure(1)
loglog(dx,central_differencing,'r')
hold on
loglog(dx,right_sided_skew,'g')
hold on
loglog(dx,left_sided_skew,'b')
xlabel('range of dx')
ylabel('truncation error')
title('range of dx vs error')
legend('central differencing','skewed right sided difference','skewed left sided difference','location','northwest')
CENTRAL DIFFERENCING SCHEME:
NO OF NODES:
The number of nodes = p+q-1
Where, p is order of derivative.
q is order of approximations.
The no of nodes = 2+4-1 =>5.
code:
function Error_central_differencing = fourth_order_central_differencing(x,dx)
analytical_derivative = -2*exp(x)*sin(x);
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_central_differencing = abs(central_differencing- analytical_derivative);
end

SKEWED RIGHT HAND DIFFERENCE SCHEME:
NO OF NODES:
The number of nodes = p+q
Where, p is order of derivative.
q is order of approximations.
The no of nodes = 2+4 =>6.
code:
function Error_right_sided_skew = fourth_order_right_sided_skew(x,dx)
analytical_derivative = -2*exp(x)*sin(x);
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_right_sided_skew = abs(right_sided_skew- analytical_derivative);
end

SKEWED LEFT HAND DIFFERENCE SCHEME:
NO OF NODES:
The number of nodes = p+q
Where, p is order of derivative.
q is order of approximations.
The no of nodes = 2+4 =6.
code:
function Error_left_sided_skew = fourth_order_left_sided_skew(x,dx)
analytical_derivative = -2*exp(x)*sin(x);
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_left_sided_skew = abs(left_sided_skew- analytical_derivative);
end



Conclusion:
Central differencing is better than other schemes because it uses information from both the sides from the point of consideration to give approximative and accurate values.