```python
# Notebook imports and packages
import numpy as np

from sympy import symbols, diff
# symbols is for "turning variables into math symbols"
# diff differentiates functions (when using symbols).
# Go through the rest of the code to understand better.
```

# Partial Derivatives and Symbolic Computation

$$f(x, y)=\frac{1}{3^{-x^2-y^2}+1}$$
<hr color="lightblue">
$$\frac{\partial f(x, y)}{\partial x}=\frac{2x\ln \left(3\right)\cdot \:3^{-x^2-y^2}}{\left(3^{-x^2-y^2}+1\right)^2}$$
<hr color="lightblue">
$$\frac{\partial f(x, y)}{\partial y}=\frac{2y\ln \left(3\right)\cdot \:3^{-y^2-x^2}}{\left(3^{-x^2-y^2}+1\right)^2}$$


```python
def f(x, y):
    return 1/(3**(-(x**2)-(y**2)) + 1)

def fpx(x, y): 
    den = 2 * x * np.log(3) * 3**(-x**2-y**2)
    num = (3**(-x**2-y**2)+1)**2
    return den/num

def fpy(x, y):
    den = 2 * y * np.log(3) * 3**(-x**2-y**2)
    num = (3**(-x**2-y**2)+1)**2
    return den/num

# Derivatives here are calculated with Symbolab. It's the same result as with sympy.
```


```python
a, b = symbols('x, y') # Variable 'a' will now represent 'x' and 'b' will represent 'y'.

f(a, b) # Our cost function.
```




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




```python
diff(f(a,b),a) # Differentiates f(x,y) with respect to 'x'.
```




$\displaystyle \frac{2 \cdot 3^{- x^{2} - y^{2}} x \log{\left(3 \right)}}{\left(3^{- x^{2} - y^{2}} + 1\right)^{2}}$




```python
diff(f(a,b),b) # Differentiates f(x,y) with respect to 'y'.
```




$\displaystyle \frac{2 \cdot 3^{- x^{2} - y^{2}} y \log{\left(3 \right)}}{\left(3^{- x^{2} - y^{2}} + 1\right)^{2}}$




```python
print(diff(f(a,b),a))
```

    2*3**(-x**2 - y**2)*x*log(3)/(3**(-x**2 - y**2) + 1)**2



```python
# Cost can be calculated with both sympy and directly.
# In this function we have to differentiate with respect to both 'x' and 'y'.
cost, cost2, dfx, dfy = (
    f(1.8,1.0),
    f(a,b).evalf(subs={a:1.8,b:1.0}),
    diff(f(a,b),a).evalf(subs={a:1.8,b:1.0}), # Differentiates f(x,y) with respect to 'x'.
    diff(f(a,b),b).evalf(subs={a:1.8,b:1.0}) # Differentiates f(x,y) with respect to 'y'.
)
```


```python
cost, cost2, dfx, dfy
```




    (0.9906047940325824, 0.990604794032582, 0.0368089716197505, 0.0204494286776392)




```python
print("Value of f(x,y) at (x=1.8, y=1.0):\t", cost)
print("Value of df(x,y)/dx at (x=1.8, y=1.0):\t", dfx)
print("Value of df(x,y)/dy at (x=1.8, y=1.0):\t", dfy)
```

    Value of f(x,y) at (x=1.8, y=1.0):	 0.9906047940325824
    Value of df(x,y)/dx at (x=1.8, y=1.0):	 0.0368089716197505
    Value of df(x,y)/dy at (x=1.8, y=1.0):	 0.0204494286776392


713aaf57f175751aea4b05c8fd71b405497e826c
ipynb
Jupyter Notebook
Section_04/Example_04_(05-08)/06-SymPy_derivatives.ipynb
ArielMAJ/Data-Science-and-Machine-Learning_Bootcamp
afae685c96d9fc8af0b2ee1be4d817df505c6c8d
MIT
Section_04/Example_04_(05-08)/06-SymPy_derivatives.ipynb
ArielMAJ/Data-Science-and-Machine-Learning_Bootcamp
afae685c96d9fc8af0b2ee1be4d817df505c6c8d
MIT
Section_04/Example_04_(05-08)/06-SymPy_derivatives.ipynb
ArielMAJ/Data-Science-and-Machine-Learning_Bootcamp
afae685c96d9fc8af0b2ee1be4d817df505c6c8d
MIT
Qwen/Qwen-72B
1. YES
2. YES