All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
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 …
Udhaya Bhaskhar Kumar
updated on 28 May 2023
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.
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Detailed reaction mechanism reduction project
A reaction mechanism is something that dictates which products are formed when two or more reactants react under different conditions. It also dictates the rate of change of concentration of species and heat of combustion. When a reaction takes place, a huge number of intermediary elementary reactions takes place which…
19 Sep 2023 10:28 AM IST
BlockMesh Drill down and simulation of Backfacing step flow using OpenFOAM
In this project, flow over back facing step is simulated for three different meshes. To simulate the flow OpenFOAM's icoFoam solver is utilised, which solves for incompressible, laminar, transient, single phase newtonian fluids under isothermal condition. The time step for each mesh is decided such that the courant number…
31 May 2023 09:30 PM IST
Steady vs unsteady simulations of flow over a 2D cylinder
In this project, flow over a 2D cylinder is studied for both steady and transient state using simulations done through Ansys. In order to study vortex shedding a customized fluid is set having density as 1kg/m3 and viscosity as 0.02 kg/ms so as to get a reynolds number of 100. To observe fluctuations due to vortex shedding…
28 May 2023 08:15 PM IST
Simulation of a 1D Super-sonic nozzle flow simulation using Macormack Method
The objective of this project is to solve 1D supersonic nozzle flow equations in conservative and non-conservative forms using the Macormack Method, to perform grid dependence test, to find the number of cycles required to be run to reach convergence, to compare the normalized mass flow obtained from both the forms and…
28 May 2023 08:15 PM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.