---
author: Nathan Carter (ncarter@bentley.edu)
---

This answer assumes you have imported SymPy as follows.


```python
from sympy import *                   # load all math functions
init_printing( use_latex='mathjax' )  # use pretty math output
```

In SymPy, we tend to work with formulas (that is, mathematical expressions)
rather than functions (like $f(x)$).  So if we wish to compute the
derivative of $f(x)=10x^2-16x+1$, we will focus on just the $10x^2-16x+1$ portion.


```python
var( 'x' )
formula = 10*x**2 - 16*x + 1
formula
```




$\displaystyle 10 x^{2} - 16 x + 1$



We can compute its derivative by using the `diff` function.


```python
diff( formula )
```




$\displaystyle 20 x - 16$



If it had been a multi-variable function, we would need to specify the
variable with respect to which we wanted to compute a derivative.


```python
var( 'y' )               # introduce a new variable
formula2 = x**2 - y**2   # consider the formula x^2 + y^2
diff( formula2, y )      # differentiate with respect to y
```




$\displaystyle - 2 y$



We can compute second or third derivatives by repeating the variable
with respect to which we're differentiating.  To do partial derivatives,
use multiple variables.


```python
diff( formula, x, x )    # second derivative with respect to x
```




$\displaystyle 20$




```python
diff( formula2, x, y )   # mixed partial derivative
```




$\displaystyle 0$



7c676fffbfe982763f698abf44542a7abd841c57
ipynb
Jupyter Notebook
database/tasks/How to compute the derivative of a function/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
database/tasks/How to compute the derivative of a function/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
database/tasks/How to compute the derivative of a function/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
2021-07-18T19:01:29.000Z
2022-03-29T06:47:11.000Z
Qwen/Qwen-72B
1. YES
2. YES