Search⌘ K
AI Features

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.

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.

y=x4+x3+x2y=x^{4}+x^{3}+x^{2}

dydx=4x3+3x2+2x\frac{dy}{dx} =4x^3 +3x^2+2x

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:

Python 3.5
from sympy import *
def f(x):
return (x**4 + x**3 + x**2)
def g(x):
return (sin(x) + cos(x))
def h(x):
return (sin(x)*exp(x) + x)
x = Symbol('x')
print("Derivative of f(x):", diff(f(x), x))
print("Derivative of g(x):", diff(g(x), x))
print("Derivative of h(x):", diff(h(x), x))

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 x(t)x(t).
  • Its velocity is defined by its first derivative, dxdt\frac{dx}{dt}.
  • Its acceleration is defined by its second derivative, d2xdt2\frac{d^2x}{dt^2}.

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:

Python 3.5
from sympy import *
def f(x):
return (x**4 + x**3 + x**2)
def g(x):
return sin(5*x)
x = Symbol('x')
print("Second derivative of f(x):", diff(f(x), x, x))
print("Second derivative of f(x):", diff(f(x), x, 2)) # specifying the value of order of differential
print("-----")
print("Third Derivative of g(x):", diff(g(x), x, x, x))
print("Third Derivative of g(x):", diff(g(x), x, 3)) # specifying the value of order of differential

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:

z=y2x+x2yz=y^2x+x^2y

zy=2yx+x2\frac{\partial z}{\partial y}=2yx+x^2

zx=y2+2xy\frac{\partial z}{\partial x}=y^2+2xy

Partial derivatives are used in vector calculus and differential geometry. In SymPy, these are calculated for multivariable functions:

Python 3.5
from sympy import *
def f(x, y):
return (y**2)*x + (x**2)*y
x = Symbol('x')
y = Symbol('y')
z = f(x, y)
print("dz/dy = ", diff(z, y))
print("dz/dx = ", diff(z, x))

Higher-order

For higher-order partial derivatives, see the example below:

z=y2x+x2yz=y^2x+x^2y

zy=2yx+x2\frac{\partial z}{\partial y}=2yx+x^2

2zyx=2y+2x\frac{\partial^2 z}{\partial y \partial x}=2y+2x

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.

Python 3.5
from sympy import *
def f(x, y):
return (y**4)*(x**3) + (x**4)*(y**3)
x = Symbol('x')
y = Symbol('y')
z = f(x, y)
print("d^3z/dy^2dx = ", diff(z, y, 2, x, 1)) # or could use diff(z, y, y, x)

In the next lesson, we will learn about integration in SymPy.