Differentiation Methods With Numerical Computing and Python Programming Language.12
Differentiation Methods With Numerical Computing and Python Programming Language.12
ABSTRACT
Numerical differentiation is a mathematical technique used to estimate the
derivative of a function at a specific point using a set of discrete data points. This
technique is highly useful when the analytic expression for the function is not
available or when dealing with noisy or irregularly distributed data. The process of
numerical differentiation involves computing the slope or rate of change of a
function at a particular point. This is achieved by estimating the derivative using
difference formulas or interpolation techniques. Difference formulas, such as
forward difference, backward difference, or central difference, estimate the
derivative by considering the difference in function values between neighbouring
data points. These formulas approximate the derivative based on the change in the
function over a small interval. When applying numerical differentiation, it is
important to consider factors such as the accuracy of the approximation, the choice
of interpolation or difference method, the spacing and density of data points, and
potential sources of error, such as noise or outlier data. Overall, numerical
differentiation provides a valuable tool for estimating derivatives and gaining
insights into the behaviour of a function based on available data. It complements
analytic techniques and enables the analysis of complex systems or functions that
lack known analytic expressions.
The most used method for numerical differentiation is the finite difference
approximation. This method relies on constructing suitable approximations to the
derivative using function values at two or more nearby points. The choice of finite
difference formula depends on the desired accuracy and the available information
about the function.
There are three commonly used finite difference formulas. First is the forward
difference formula, which approximates the derivative at the point x₀ by considering
the function values at x₀ and the nearby point x₀ + h. Second is the backward
difference formula using the points x₀ and x₀ - h. Third is the central difference
formula, which combines both forward and backward differences to provide a more
accurate approximation. This formula uses the points x₀ - h and x₀ + h. The choice
of formula depends on the specific requirements of the problem and the trade-off
between accuracy and computational overhead.
2. Precise of literature
Numerical differentiation is the process of calculating the derivative of a function
from its data. In some cases, a function may have a complex form, making
analytical computation impractical. However, we can approximate the derivative of
such functions using numerical methods, one of which is the finite difference
approximation method.
(2)
(3)
(4)
3. Methods
Computer is a device or machine capable of performing computations. Computation
refers to the process of performing mathematical or logical operations on data to
produce desired results.
Therefore, to facilitate the calculations, the software used in the laboratory is one
of the programming-based software, namely Visual Studio Code (VSC). The
programming language used and implemented in the software itself is the Python
programming language. Python is a high-level programming language that is
versatile, easy to understand, and has a simple syntax. These characteristics form
the basis for using the Python programming language to perform numerical
algorithmic calculations in this laboratory experiment.
The sample experiments used in the laboratory consist of two sample experiments,
which are included in the laboratory module. The sample experiments are as
follows:
a. List of program:
x = symbols('x')
def f(x):
return (sin(3-2*x))**4
def F(x):
return (sin(3*x))/(x*cos(2*x))
def analytical_derivative_fx(x):
return (diff(f(x), x).subs(x, x_input))
def analytical_dervivative_Fx(x):
return (diff(f(x), x).subs(x, x_input))
x_input = 1
h = [0.001, 0.005, 0.05, 0.1, 0.5]
value_of_analytical_derivative_function_fx =
analytical_derivative_fx(x).evalf()
print()
value_of_analytical_derivative_funciton_Fx =
analytical_dervivative_Fx(x).evalf()
print()
print("The value of analytical derivative of function
(sin(3*x))/(x*cos(2*x))) is",
value_of_analytical_derivative_funciton_Fx)
print("---Numercial Derivative of function (sin(3*x))/(x*cos(2*x))
with forward difference method---")
for i in range(len(h)):
value_of_numerical_derivative_function_fx =
numerical_derivative_forward_difference_method(F, x_input, h[i])
numerical_error_for_funciton_fx =
abs(value_of_analytical_derivative_function_fx-
value_of_numerical_derivative_function_fx)
print ("The numerical value of function
(sin(3*x))/(x*cos(2*x)) with", h[i], "is",
value_of_numerical_derivative_function_fx.evalf(), "with is
error", numerical_error_for_funciton_fx.evalf())
print ()
print()
b. Program analysis
First, the program calculates the analytical derivatives of both functions and stores
them in variables. Then, the program computes the numerical derivatives using each
of the three methods for several step sizes. For each method and step size, the
program calculates the numerical derivative, computes the numerical error by
comparing it with the analytical derivative, and prints the results.
The goal of this analysis is to evaluate the accuracy of the numerical approximation
techniques. By comparing the numerical derivatives with the analytical derivatives,
this program provides insights into the reliability and effectiveness of the different
differentiation methods used. The selection of multiple step sizes allows for an
evaluation of the comparative accuracy of the numerical derivatives at different
levels of precision.
The output of the program for the first sample function can be observed in Figure 1
below.
Figure 1 Display of the program output for the first sample function.
From Figure 1, we can analyze that the values of the numerical differentiation
calculations vary depending on the method used and the value of the approximation
h. We can also analyze from the program output of the first sample function that
the smaller the value of h, the closer the numerical approximation is to the analytical
calculation. This is also reflected in the calculation of the error between the
numerical and analytical approximations. In other words, the smaller the value of h
for the first sample function, the more accurate the approximation, and the most
accurate numerical method for calculating the approximation of the first sample
function is the central difference method. This can also be seen from the very small
error values for that method.
The program output displayed for the second sample function can be reviewed in
Figure 2 below.
Figure 2 Display of the program output for the second sample function.
We can analyse from Figure 2 that the overall approximation results, when
considering the differences in numerical differentiation methods and the values of
h, are like the output of the first sample function in Figure 1. The numerical
computation results shown in Figure 2 exhibit the most accurate approximations
when the value of h is the smallest, specifically when h is equal to 0.001, using the
central difference numerical differentiation method.
Solutions:
a. In numerical differentiation for finite difference, we need 2 points (a, b)
with a spatial distance between the points (h). It can be illustrated as
follows:
Figure 3 Graph of spatial distance between the points of some function (h)
From Figure 3, we can see that as the spacing (h) increases, more points
with corresponding y-values are involved. With more values involved, the
curve formed will approximate the shape of the analytical function more
closely. This can be observed from the two graphs below.
(a) (b)
Figure 4 (a) Graph with the value of spaces of h are smaller. (b) Graph
with the value of space of h is bigger.
With a larger h (Figure 4 (a)), there are areas of the curve that are not
covered compared to using a smaller h (Figure 4 (b)). Therefore, with
smaller sub-intervals, the resulting error will be smaller as well.
Conversely, with larger sub-intervals, the resulting error will be larger.
impulses = []
for t in time_to_calculate:
closest_index = min(range(len(time)), key=lambda i:
abs(time[i] - t))
for i in range(len(time_to_calculate)):
print(f"Impuls pada t = {time_to_calculate[i]} s:
{impulses[i]} Ns")
Output program :
Explaination:
At t = 0 and t = 0.56, there is no impulse because of the limitation of the
data. If we consider the limited data, the velocity changes would be zero
because at t = 0 the velocity is at its minimum point, and at t = 0.56 the
velocity is at its maximum point.
c. The forward difference method is used when the value being sought is in
the initial interval, so there are other variables ahead of it but none behind
it. The backward difference method is used when the value being sought is
in the final interval, so there are other variables behind it but none ahead of
it. The central difference method is used when the value being sought is
between the initial and final intervals, with other variables both ahead and
behind the variable being sought.
d. The comparison is explained in section 4 Results and Discussion,
specifically in subsections 4.1 and 4.2.
6. Conclusion
To calculate the derivative or differentiation of a function or data, three different
numerical differentiation methods can be used: forward difference, backward
difference, and central difference. These three methods yield different numerical
results, with the central difference method providing the most accurate results. The
accuracy of the calculations also depends on the value of h, which represents the
change in a parameter up to a certain point. Smaller values of h lead to more
accurate numerical calculations due to the consideration of very small and precise
changes.
References
1. Burden, R. L., & Faires, J. D. (2010). Numerical analysis (9th ed.).
Brooks/Cole, Cengage Learning.
2. Cheney, E. W., & Kincaid, D. (2017). Numerical mathematics and computing
(7th ed.). Cengage Learning.
3. Conte, S. D., & de Boor, C. (2019). Elementary numerical analysis: An
algorithmic approach (3rd ed.). Dover Publications.
4. Davis, P. J., & Rabinowitz, P. (2012). Methods of numerical integration (2nd
ed.). Courier Corporation.
5. Kincaid, D., & Cheney, E. W. (2009). Numerical analysis: Mathematics of
scientific computing (3rd ed.). American Mathematical Society.
6. Stoer, J., & Bulirsch, R. (2002). Introduction to numerical analysis (3rd ed.).
Springer-Verlag.