DETAILED REPORT ON DISCRETIZATION BASICS FOR A RANGE OF dx
DETAILED REPORT ON DISCRETIZATION BASICS FOR A RANGE OF dx
1. AIM
To write a program that compares the first, second and fourth order approximations of the given function f(x) = sin(x)/x^3 for a range of dx against the analytical derivative.
- EQUATIONS/ FORMULAE USED
Given function, f(x) = sin(x)/x^3
Analytical derivative, f'(x) = (xcos(x) - 3sin(x))/x^4, x=pi/3 is the point of consideration where the function is being solved and dx ranges from pi/4000 to pi/40 with 200 interval points.
Basically, Taylor series is used here for numerical approximation. Two types of differencing scheme are used such as forward and central differencing scheme.
Forward differencing scheme
It’s a scheme in which the derivative is calculated using the information from the point on right side of the point of consideration. Also for 1stOrder Forward differencing approximation the value is taken only from only one point.
1stOrder approx - Forward differencing scheme = (f(x+dx)-f(x))/dx
Central differencing scheme
It’s a scheme in which the derivative is calculated using the information from the very next pointon both sides (2 points) of the point of consideration. It is always used forapproximation of even number order. So for 2nd and 4thOrder Central differencing approximation the value is taken fromthe point on both sides (2 points) and two points on both sides (4 points) respectively.
2nd Orderapprox - Central differencing scheme = (f(x+dx)-f(x-dx))/dx
4th Orderapprox - Central differencing scheme = (f(x-2*dx)-(8*f(x-dx))+(8*f(x+dx))-f(x+2*dx))/(12*dx)
Error is equal to the difference in numerical and analytical derivative.
- OBJECTIVES & PROCEDURE
- The main objective of the challenge is to understand the basics of discretization via numerical approximations and compare the behavior of first, second and fourth order approximations against the analytical derivative of a function.
- In the process, Firstly we calculate the first order analytical derivative of the given function at a point, x=pi/3.
- Forward and central differencing schemes are applied in separate function programs to calculate the 1st, 2nd& 4th order approximation respectively.
- Then main program is used to call each function programs in order to calculate the derivative over a range of dx.
- The values obtained from first, second and fourth order approximations are compared with the analytical derivative to find the error and graph is plotted.
- PROGRAM
Programming language used – Octave 5.1.1
Program
-------------------------------------------------------------------------------------
MAIN PROGRAM
-------------------------------------------------------------------------------------
x = pi/3;
dx = linspace(pi/4000,pi/40,200);
for i = 1:length(dx)
first_order_error(i) = First_Order_Discretization_Basics(x,dx(i));
second_order_error(i) = Second_Order_Discretization_Basics(x,dx(i));
fourth_order_error(i) = Fourth_Order_Discretization_Basics(x,dx(i));
end
figure(1)
loglog(dx,first_order_error,'*')
hold on
loglog(dx,second_order_error,'*')
hold on
loglog(dx,fourth_order_error,'*')
xlabel('dx','Fontsize',18)
ylabel('error','Fontsize',18)
legend('first order error','second order error','fourth order error')
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
FUNCTION PROGRAM
-------------------------------------------------------------------------------------
FIRST ORDER DISCRETIZATION
function out = First_Order_Discretization_Basics(x,dx)
% Given function f(x)= sin(x)/x^3;
% Analytical derivative for the given function f'(x) = (xcos(x)- 3sin(x))/x^4;
% Computing Analytical derivative at x = pi/3;
Analytical_derivative = ((x*cos(x))- (3*sin(x)))/x^4;
% Numerical Derivative
% 1st Order approx - Forward differencing scheme = (f(x+dx)-f(x))/dx;
Forward_differencing_scheme = ((sin(x+dx)/(x+dx)^3)-(sin(x)/x^3))/dx;
% First Order Error
out = abs (Analytical_derivative - Forward_differencing_scheme);
end
-------------------------------------------------------------------------------------
SECOND ORDER DISCRETIZATION
function out = Second_Order_Discretization_Basics(x,dx)
% Given function f(x)= sin(x)/x^3;
% Analytical derivative for the given function f'(x) = (xcos(x)- 3sin(x))/x^4;
% Computing Analytical derivative at x = pi/3;
Analytical_derivative = ((x*cos(x))- (3*sin(x)))/x^4;
% Numerical Derivative
% 2nd Order approx - Central differencing scheme = (f(x+dx)-f(x-dx))/dx;
Central_differencing_scheme = ((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx);
% Second Order Error
out = abs (Analytical_derivative - Central_differencing_scheme);
end
-------------------------------------------------------------------------------------
FOURTH ORDER DISCRETIZATION
function out = Fourth_Order_Discretization_Basics(x,dx)
% Given function f(x)= sin(x)/x^3;
% Analytical derivative for the given function f'(x) = (xcos(x)- 3sin(x))/x^4;
% Computing Analytical derivative at x = pi/3;
Analytical_derivative = ((x*cos(x))- (3*sin(x)))/x^4;
% Numerical Derivative
% 4th Order approx - Central differencing scheme = (f(x-2*dx)-(8*f(x-dx))+(8*f(x+dx))-f(x+2*dx))/(12*dx);
Fourth_Order_Central_differencing_scheme = ((sin(x-2*dx)/(x-2*dx)^3)-(8*(sin(x-dx))/(x-dx)^3)+(8*(sin(x+dx))/(x+dx)^3)-(sin(x+2*dx)/(x+2*dx)^3))/(12*dx);
% Fourth Order Error
out = abs (Analytical_derivative - Fourth_Order_Central_differencing_scheme);
end
5. RESULTS
Graph

6. CONCLUSION
- From the above graph, it can be clearly seen that the answer obtained from fourth order approximations is much closer to analytical method for the given function over the range of dx. So the error is minimal compared to first and second order approximation.
- But at the same time, this trend obtained from the results cannot be generalized as some variation in error was observed.
- The variations in error may occur if the input values such as dx is changed. This is due to the stability of each order of approximation. The stability reduces as the order of approximation increases.
- So the selection of order of approximation is crucial depending on the problem.