You are on page 1of 13

Differentiation Methods with Numerical Computing using

Python Programming Language

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.

Keywords: Numerical Differentiation, Function, Difference Formulas,


Differentiation Methods
1. Introduction
Numerical differentiation is a fundamental technique in scientific computing and
numerical analysis that aims to approximate the derivative of a function at a specific
point. The derivative provides information about the rate of change of a function
and plays a crucial role in various scientific and engineering applications.

In calculus, the derivative of a function f(x) at a point x₀ is defined as the limit of


the difference quotient as the interval around x₀ shrinks to zero:

f'(x₀) = lim(h → 0) [(f(x₀ + h) - f(x₀)) / h] (1)

However, in many practical cases, it is not always possible or efficient to compute


derivatives analytically using this limit definition. This is where numerical
differentiation comes into play, providing an alternative approach to estimate
derivatives.

The basic idea behind numerical differentiation is to approximate the derivative by


considering function evaluations at nearby points. By using finite difference
schemes, numerical differentiation approximates the derivative based on the
function values at a set of discrete points.

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.

Numerical differentiation is not without its limitations. This approach introduces


errors due to limited computational precision, rounding errors, and truncation errors
that arise from the approximations themselves. These errors can accumulate and
affect the accuracy of the derivative estimates, particularly when the step size h is
chosen improperly.

To address these issues, advanced techniques such as Richardson extrapolation,


adaptive step size selection, and high-order finite difference formulas can be
employed. These methods aim to improve the accuracy and stability of numerical
differentiation. However, in this paper, the focus will be on the theory, explanation,
and application of the numerical differentiation method itself.

In summary, numerical differentiation provides a practical and efficient approach


to estimate function derivatives when analytical methods are impractical or
unavailable. By using finite difference formulas and appropriate numerical
techniques, this method enables scientists and engineers to gain insights into the
behaviour and properties of functions in various applications.

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.

The finite difference approximation method for determining the derivative of a


function is based on the Taylor series expansion of a function f(x) around a point x.
From the Taylor series expansion, Equations 2-4 can be obtained.

(2)

(3)

(4)

Equation 2 represents the equation for first-order forward difference. Equation 3


represents the equation for first-order backward difference. Equation 4 represents
the equation for first-order central difference. In these equations, the second term
on the right-hand side represents truncation error. For second-order or higher-order
accuracy, the Taylor series expansion can be applied again.

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.

Computers are specifically designed to carry out computations, which involve


processing and manipulating data according to predetermined algorithms or
instructions. Computation can involve various operations such as arithmetic
calculations, logical comparisons, data transformations, and decision-making
processes.

Computation can be performed in various ways, depending on the nature of the


problem and the algorithms used. Computers can handle numerical computations,
which involve mathematical calculations, as well as symbolic computations, which
involve manipulation of symbols or characters. In other words, computers can
process large amounts of data quickly and accurately, making them highly efficient
for performing complex computations and solving various problems.

In this laboratory experiment, a computer is used as a tool for numerical


calculations. To perform the calculations, software is needed to translate
mathematical logic into computer algorithms, so that mathematical problems can
be efficiently computed and the displayed results are accurate or close to the actual
results. Moreover, numerical calculations involve repetitive computations to
approximate or obtain the actual 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:

1. First experiment sample


Calculate the first derivative of the function 𝑓(𝑥) = sin⁴(3 − 2𝑥) at 𝑥 = 1 with
the values ℎ ∈ {0.001, 0.005, 0.05, 0.1, 0.5} for each method. Also, calculate
the resulting error.
2. Second experiment sample
Calculate the first derivative of the function 𝑓(𝑥) = sin(3𝑥) / (𝑥 * cos(2𝑥)) at
𝑥 = 1 with values of ℎ ∈ {0.001, 0.005, 0.05, 0.1, 0.5} for each method.
Also, calculate the resulting errors.

The experimental procedure to conduct this lab is to perform numerical


differentiation calculations using Python programming language algorithms on the
two specified sample experiments. The numerical calculation algorithms are
designed according to various types of numerical differentiation methods discussed
in the Basic Theory. The numerical calculation algorithm is implemented in a single
program that can display the calculation results for both samples.
4. Result and discussion
4.1. List of programs and its analysis
To solve the given problem in the lab experiment, the problem can be addressed by
creating a single list of programs that encompasses all the given experimental
problems. The list of programs created to solve the experimental problems is as
follows:

a. List of program:

from sympy import *

x = symbols('x')

def numerical_derivative_forward_difference_method(f, x, h):


return (f(x+h)-f(x))/h

def numerical_derivative_backward_difference_method(f, x, h):


return (f(x)-f(x-h))/h

def numerical_derivative_central_difference_method(f, x, h):


return (f(x+h)-f(x-h))/(2*h)

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("The analytical value of funciton (sin(3-2*x))**4 is",


analytical_derivative_fx(x).evalf())
print("---Numercial Derivative of function (sin(3-2*x))**4 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-2*x))**4 with",
h[i], "is", value_of_numerical_derivative_function_fx.evalf(),
"with is error", numerical_error_for_funciton_fx.evalf())
print ()

print("---Numercial Derivative of function (sin(3-2*x))**4 with


backward difference method---")
for i in range(len(h)):
value_of_numerical_derivative_function_fx =
numerical_derivative_backward_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-2*x))**4 with",
h[i], "is", value_of_numerical_derivative_function_fx.evalf(),
"with is error", numerical_error_for_funciton_fx.evalf())

print()

print("---Numercial Derivative of function (sin(3-2*x))**4 with


central difference method---")
for i in range(len(h)):
value_of_numerical_derivative_function_fx =
numerical_derivative_central_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-2*x))**4 with",
h[i], "is", value_of_numerical_derivative_function_fx.evalf(),
"with is error", numerical_error_for_funciton_fx.evalf())

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("---Numercial Derivative of function (sin(3*x))/(x*cos(2*x))


with backward difference method---")
for i in range(len(h)):
value_of_numerical_derivative_function_fx =
numerical_derivative_backward_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("---Numercial Derivative of function (sin(3*x))/(x*cos(2*x))


with central difference method---")
for i in range(len(h)):
value_of_numerical_derivative_function_fx =
numerical_derivative_central_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())

b. Program analysis

The provided program performs numerical differentiation using three different


methods: forward difference, backward difference, and central difference. This
program focuses on two mathematical functions: (sin(3-2*x))**4 and
(sin(3*x))/(x*cos(2*x)).

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.

Overall, this program offers a systematic approach to numerical differentiation and


enables a quantitative assessment of the errors associated with each method. Such
analysis can be valuable in validating the accuracy of numerical approximations
and aiding in the selection of appropriate methods for various applications in the
mathematical and scientific domains.

4.2. Program output analyse


The output of the program is a result that provides the analytical calculation of the
two sample functions and the corresponding numerical calculations. The numerical
calculations of the two sample functions yield different outputs because the
program utilizes three different numerical differentiation methods, as mentioned in
the basic theory. Each numerical differentiation method has a different
approximation step size, resulting in varying output values. This allows for analysis
not only based on the different numerical differentiation methods used but also
based on the varying values of the step size (h). The program's output also displays
the error of each numerical differentiation calculation for the respective methods.

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.

This phenomenon occurs because the approximation of a method is based on the


derivation of its formula from Taylor series, resulting in different numerical
approximation values. By combining the forward and backward difference
approaches, the central difference method produces numerical approximations with
greater accuracy. As for the value of h, it indicates the change in the dependent
parameter from its initial value. A smaller value of h implies a smaller change in
the parameter being considered, leading to more accurate and precise calculations
when incorporating smaller parameter changes. Overall, the analysis highlights that
the choice of numerical differentiation method and the value of h can significantly
impact the accuracy of the numerical approximation.
5. Final task
a. Investigate what happens when the step size h (interval) is different.
b. A car with a mass of 1200 kg is moving with varying velocity. The data of
the distance traveled by the car over time is as follows:

Determine the impulse experienced by the car at 𝑡 = 0, 0.25, and 0.56.


c. Under what conditions can each method be used?
d. Compare the results obtained from all the methods used to find the
derivative value.

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.

b. Program solution for b point problem


def calculate_impulse(mass, velocity_change):
impulse = mass * velocity_change
return impulse

mass = 1200 #kg


time = [0, 0.1, 0.25, 0.38, 0.56] # second
distance = [0, 2, 2.8, 3.5, 4] # meter

time_to_calculate = [0, 0.25, 0.56] #second

impulses = []

for t in time_to_calculate:
closest_index = min(range(len(time)), key=lambda i:
abs(time[i] - t))

if closest_index > 0 and closest_index < len(time) - 1:


velocity_change = (distance[closest_index+1] -
distance[closest_index-1]) / (time[closest_index+1] -
time[closest_index-1])
else:
velocity_change = 0.0

impulse = calculate_impulse(mass, velocity_change)


impulses.append(impulse)

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.

You might also like