# How

## Calculate the derivative of an expression.

We can calculate the derivative of an expression using `sympy.diff` which takes,
an expression, a variable and a degree.

````{tip}
```
sympy.diff(expression, variable, degree=1)
```
````

The default value of `degree` is 1.

For example to compute $\frac{d (4 x ^ 3 + 2 x + 1}{dx}$:


```python
import sympy as sym

x = sym.Symbol("x")
expression = 4 * x ** 3 + 2 * x + 1
sym.diff(expression, x)
```




$\displaystyle 12 x^{2} + 2$



To compute the second derivative: $\frac{d ^ 2 (4 x ^ 3 + 2 x + 1}{dx ^ 2}$


```python
sym.diff(expression, x, 2)
```




$\displaystyle 24 x$



## Calculate the indefinite integral of an expression.

We can calculate the indefinite integral of an expression using
`sympy.integrate`. Which takes an expression and a variable.

````{tip}
```
sympy.integrate(expression, variable)
```
````

For example to compute $\int 4x^3 + 2x + 1 dx$:


```python
sym.integrate(expression, x)
```




$\displaystyle x^{4} + x^{2} + x$



## Calculate the definite integral of an expression.

We can calculate the definite integral of an expression using
`sympy.integrate`. The first argument is an expression but instead of passing a
variable as the second argument we pass a tuple with the variable and the upper
and lower bounds of integration.

````{tip}
```
sympy.integrate(expression, (variable, lower_bound, upper_bound))
```
````

For example to compute $\int_0^4 4x^3 + 2x + 1 dx$:


```python
sym.integrate(expression, (x, 0, 4))
```




$\displaystyle 276$



## Use $\infty$

In `sympy` we can access $\infty$ using `sym.oo`:

````{tip}
```
sympy.oo
```
````

For example:


```python
sym.oo
```




$\displaystyle \infty$



## Calculate limits

We can calculate limits using `sympy.limit`. The first argument is the
expression, then the variable and finally the expression the variable tends to.

````{tip}
```
sympy.limit(expression, variable, value)
```
````

For example to compute $\lim_{h \to 0} \frac{4 x ^ 3 + 2 x + 1 - 4(x - h)^3 - 2(x - h) - 1}{h}$:


```python
h = sym.Symbol("h")
expression = (4 * x ** 3 + 2 * x + 1 - 4 * (x - h) ** 3 - 2 * (x - h) - 1) / h
sym.limit(expression, h, 0)
```




$\displaystyle 12 x^{2} + 2$



c99965c9bf93605def7e2898fd3c9765524a39f1
ipynb
Jupyter Notebook
book/tools-for-mathematics/03-calculus/how/.main.md.bcp.ipynb
11michalis11/pfm
c91b1eda70d7cde3fbe065db4667f84853947850
MIT
2020-09-24T21:02:41.000Z
2020-10-14T08:37:21.000Z
book/tools-for-mathematics/03-calculus/how/.main.md.bcp.ipynb
11michalis11/pfm
c91b1eda70d7cde3fbe065db4667f84853947850
MIT
2020-09-21T15:54:23.000Z
2021-12-19T23:26:15.000Z
book/tools-for-mathematics/03-calculus/how/.main.md.bcp.ipynb
11michalis11/pfm
c91b1eda70d7cde3fbe065db4667f84853947850
MIT
2020-10-02T09:21:27.000Z
2021-07-08T14:46:27.000Z
Qwen/Qwen-72B
1. YES
2. YES