# Derivatives and Integrals

## Basic Derivative Formula $$\frac{d}{dx} x^n = n \times x^{n-1}$$

# Basic Integral Formula $$\int x^n dx = \frac{1}{n+1} x^{n+1} + C$$


```python
# Load sympy module
from sympy import *
```


```python
# Define variables
x, y = symbols('x y')
```

## Define the function $$f(x) = x^3 - 2x^2 + x - 1$$


```python
# Function f(x) = x^3 - 2x^2 + x - 1
def f(x):
    return x**3 - 2*x**2 + x - 1
```

## Calculate $$D_x f = 3x^2 - 4x + 1$$


```python
# Derivative of f(x)
d_x = diff(f(x), x)
d_x
```




$\displaystyle 3 x^{2} - 4 x + 1$




```python
# Substitute x = 2
number_subs = diff(f(x), x).subs(x, 2)
print(number_subs)
```

    5


## Calculate $$\int f(x) dx$$


```python
# Integral of f(x)
integral = Integral(f(x), x)
integral
```




$\displaystyle \int \left(x^{3} - 2 x^{2} + x - 1\right)\, dx$



## The Result is $$\frac{x^4}{4} - \frac{2x^3}{3} + \frac{x^2}{2} - x$$


```python
# Do the integral of f(x)
integral.doit()
```




$\displaystyle \frac{x^{4}}{4} - \frac{2 x^{3}}{3} + \frac{x^{2}}{2} - x$



## Subtitute $$x = 2$$


```python
# Substitute x = 2
integ = integrate(f(x), x).subs(x, 2)
print(integ)
```

    -4/3


## Define the function $$g(x) = x \times y$$


```python
# Function g(x, y) = x * y
def g(x, y):
    return x * y
```

## Calculate $$\displaystyle \int_{0}^{2} \int_{0}^{2} f(x) dy dx = 4$$


```python
# Integral of g(x, y)
print(integrate(g(x, y), (x, 0, 2), (y, 0, 2)))
```

    4


cc90c415e16fdeb04d1abf70abd1f960b4e65016
ipynb
Jupyter Notebook
derivatives-integrals.ipynb
ricoen/learn-math
fc84bc4d0dc353f8ccb3c52e36155069e9ca5af4
MIT
2021-11-30T10:05:29.000Z
2021-12-30T13:39:08.000Z
derivatives-integrals.ipynb
ricoen/learn-math
fc84bc4d0dc353f8ccb3c52e36155069e9ca5af4
MIT
derivatives-integrals.ipynb
ricoen/learn-math
fc84bc4d0dc353f8ccb3c52e36155069e9ca5af4
MIT
Qwen/Qwen-72B
1. YES
2. YES