Taylor table method of fourth order approximation for the given function
The below program finds the central,skewed right,skewed left fourth order approximation of second derivative of the given function for different values of dx ranging from pi/4 to pi/4000 and the error is found by finding the absolute difference between analytically and numerically derived value,then the absolute error is then plotted it into a graph.The numerical order approximations are derived using Taylor's table. In order to find the numerical equations for entering into matlab comparison linear equations are solved using matlab i.e., AX=B, X=A^-1*B.
Main program:
clear all
close all
clc
%function whose first derivative is to be computed is sin(x)/x^3
%the first derivative is to be computed at x=pi/3
x=pi/3;
%analytically the derivative of the function is (x^3*(cos(x))-sin(x)*3*x^2)/x^6
%first order approximation= (f(x+dx)-f(x))/dx (forward differenc approach)
%second order approximation= (f(x+dx)-f(x-dx))/2*dx (central difference approach)
%fourth order approximation= (f(x-2*dx)-(8*f(x-dx))+(8*f(x+dx))-f(x+2*dx))/(12*dx)
%assuming dx ranges from pi/4 to pi/4000
dx=linspace(pi/4,pi/4000,20);
for i=1:length(dx)
central_difference_fourth_order_approximation_error(i)=error(x,dx(i),1);
skewed_right_sided_fourth_order_approximation_error(i)=error(x,dx(i),2);
skewed_left_sided_fourth_order_approximation_error(i)=error(x,dx(i),3);
forward_difference_first_order_approximation_error(i)=error(x,dx(i),4);
backward_difference_first_order_approximation_error(i)=error(x,dx(i),5);
end
figure(1)
loglog(dx,central_difference_fourth_order_approximation_error,'linewidth',2)
hold on
loglog(dx,skewed_right_sided_fourth_order_approximation_error,'linewidth',2)
hold on
loglog(dx,skewed_left_sided_fourth_order_approximation_error,'linewidth',2)
hold on
loglog(dx,forward_difference_first_order_approximation_error,'linewidth',2)
hold on
loglog(dx,backward_difference_first_order_approximation_error,'linewidth',2)
xlabel('Log of dx')
ylabel('Log of error')
legend('Central difference fourth order approximation','Skewed right sided fourth order approximation','Skewed left sided fourth order approximation','Forward difference first order approximation','Backward difference first order approximation error')
Below is the function for computing the errors for central difference,skewed right,skewed left fourth order approximations and also first order forward and backward approximations:
function error=error(x,dx,i)
if i==1
%central difference fourth order approximation
central_difference_fourth_order_approximation=(-0.0833*f(x-(2*dx))+1.3333*f(x-dx)-2.5*f(x)+1.3333*f(x+dx)-0.0833*f(x+2*dx))/(dx^2);
error=abs(central_difference_fourth_order_approximation-f2(x));
end
if i==2
%skewed_right_sided_fourth_order_approximation
skewed_right_sided_fourth_order_approximation=(2.91667*f(x)-8.66667*f(x+dx)+9.5*f(x+2*dx)-4.66667*f(x+3*dx)+0.91667*f(x+4*dx))/(dx^2);
error=abs(skewed_right_sided_fourth_order_approximation-f2(x));
end
if i==3
%skewed_left_sided_fourth_order_approximation
skewed_left_sided_fourth_order_approximation=(2.91667*f(x)-8.66667*f(x-dx)+9.5*f(x-2*dx)-4.66667*f(x-3*dx)+0.91667*f(x-4*dx))/(dx^2);
error=abs(skewed_left_sided_fourth_order_approximation-f2(x));
end
if i==4
forward_difference_first_order_approximation=(f(x+2*dx)-2*(f(x+dx))+f(x))/(2*(dx^2));
error=abs(forward_difference_first_order_approximation-f2(x));
end
if i==5
backward_difference_first_order_approximation=(f(x-2*dx)-2*(f(x-dx))+f(x))/(2*(dx^2));
error=abs(backward_difference_first_order_approximation-f2(x));
end
end
The below function represents the mathematical function, in order to make the program modular and reduce the size of main body, the mathematical function is made into a function seperate from main code and called whenever needed:
function f=f(x)
f=exp(x)*cos(x);
end
The below function represents the the second derivative of the mathematical function given in the question:
function f2=f2(x)
f2=-2*exp(x)*sin(x);
end
The below plot compares the central,skewed right and skewed left fourth order approximations compared for different values of dx values:
The below plot compares the central,skewed right and skewed left fourth order approximations compared for different values of dx values:
From these plots, we can see that skewed right and skewed left drops faster than central fourth order approximation and skewed schemes are more useful in CFD because fluid flows right to left or left to right, so skewed schemes becomes more useful than central fourth order approximation where points on both sides are required for computation, where it is rare or impossible to find information in most cases. That is why skewed schemes are more useful than central schemes.
While comparing the plots of skewed right,skewed left,central fourth order approximation and forward,backward first order approximations of the second derivative of the funtion we can see that fourth order approximation schemes drop error values exponentially faster than first order scheme. But first order schemes are more faster than fourth order schemes and fourth order schemes becomes tougher to implement for complex problems where the first order schemes can be easily implemented even for complex problem.