Differentiation
Explore how to use SymPy for symbolic differentiation including first-order, higher-order, and partial derivatives of functions. Understand the mathematical concepts and learn to implement derivatives in Python for scientific computing tasks.
We'll cover the following...
Differentiation is the process of finding a function that outputs the rate of change of one variable with respect to another.
SymPy provides the functionality of symbolically calculating the derivatives of a function.
First-order derivatives
Derivatives are computed with the diff function, which recursively uses the various differentiation rules.
We will be exploring different types of derivatives using SymPy. The SymPy diff() function takes a minimum of two arguments: the function to be differentiated and the variable with respect to which the differentiation is performed.
diff(y, x)
Let’s look at an implementation of this:
Value of the derivative
The value of the first derivate determines whether the function is increasing or decreasing. A positive first derivative shows the function is increasing and a negative first derivative shows that the function is decreasing.
To find the value of the derivative, we use the subs() method. The first argument of subs() is the variable to be substituted and the second argument is its value. The following syntax is used:
diff(f(x), x).subs(x, value)
Higher-order derivatives
Higher-order differentials are the differentials of the first differential. Each of these higher-order differentials has importance. A common example is in kinematics.
- Displacement of a body is defined by .
- Its velocity is defined by its first derivative, .
- Its acceleration is defined by its second derivative, .
As mentioned above, the SymPy diff() function takes a minimum of two arguments. The remaining arguments can be used for higher-order derivatives.
diff(f(x), x, x)
Each subsequent argument after the second argument will differentiate the function with respect to that variable.
If the third variable is an integer, it will denote the order of differentiation. Let’s look an example of this below:
Partial derivatives
First-order
In mathematics, a partial derivative of a function with several variables is its derivative with respect to one of those variables, with the others held constant. Below are the examples for first-order partial derivatives:
Partial derivatives are used in vector calculus and differential geometry. In SymPy, these are calculated for multivariable functions:
Higher-order
For higher-order partial derivatives, see the example below:
Higher-order partial derivatives are computed using a similar method as the higher-order derivatives except that in partial derivatives, we have to specify the degree of two variables. We can either specify the variable and the order of its derivative to be computed or keep on adding subsequent arguments.
In the next lesson, we will learn about integration in SymPy.