How to Find the Partial Derivative of a Function in Python 


               


How to Find the Partial Derivative of a Function in Python



Python


In this article, we show how to find the partial derivative of a function in Python.

With modules, it is easy to find the partial derivative of a mathematical function in Python. <

The model we use is the sympy module.

In order to use this module, you must first install it.

So, open up the command prompt window on your computer and specify the full path to the Scripts folder in the Python package you installed. Then, type in, pip install sympy. Once you see that the module has been successfully installed, then you are ready to proceed to the code on this page.

A partial derivative is the derivative of a function that has more than one variable with respect to only one variable.

So, below we will find the partial derivative of the function, x2y3+ 12y4 with respect to the y variable.

The Python code below calculates the partial derivative of this function (with respect to y).



So, the first thing, we must do is import Symbol and Derivative from the sympy module.

As explained above, this module must be installed by you.

The next thing we must do is specify the variable (or symbol) that we want to differentiate with respect to. In our function, we want to find the derivative with respect to the variable y. Therefore, we differentiate with respect to the variable, y. This must always be specified when differentiating in Python using the sympy module.

Next, we specify the function that we want to differentiate. Again, we are differentiating the function, x2y3+ 12y4

Translating this function into Python is, x**2 * y**3 + 12*y**4.

Anytime a number is raised to a power, in Python the symbol, **, denotes this. Thus, x**4 represents x4

Whenever we have a number multiplied by a variable, such as 7x, this must be specified with the symbol, *. Thus, 7x would be represented as 7*x.

We then create a variable named deriv (can be any name) and set it equal to Derivative(function, x). The first parameter is the function you want to differentiate and the second parameter is the variable, or symbol, that you want to differentiate with respect to.

The last step is simply to call the doit() function on the deriv variable.

Running this code gives us the following output, shown below.



Let's now take the same exact function and find the partial deriative with respect to x.

The following Python code below computes this.



Running this code gives us the following output below.





And this is all that is required to find the partial derivative of a function in Python.


Related Resources

How to Show All Tables of a MySQL Database in Python

How to Count the Number of Rows in a MySQL Table in Python



HTML Comment Box is loading comments...