# Vector CoordSys3D
> ### gradient


```python
import sympy as sym
import sympy.vector
sym.vector.CoordSys3D('')
```




$\displaystyle CoordSys3D\left(, \left( \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right], \  \mathbf{\hat{0}}\right)\right)$




```python
C = sym.vector.CoordSys3D('')
f = C.x * sym.sin(C.y)
f
```




$\displaystyle \mathbf{{x}_{}} \sin{\left(\mathbf{{y}_{}} \right)}$




```python
sym.vector.gradient(f)
```




$\displaystyle (\sin{\left(\mathbf{{y}_{}} \right)})\mathbf{\hat{i}_{}} + (\mathbf{{x}_{}} \cos{\left(\mathbf{{y}_{}} \right)})\mathbf{\hat{j}_{}}$




```python
sym.vector.gradient(f).to_matrix(C)
```




$\displaystyle \left[\begin{matrix}\sin{\left(\mathbf{{y}_{}} \right)}\\\mathbf{{x}_{}} \cos{\left(\mathbf{{y}_{}} \right)}\\0\end{matrix}\right]$




```python
sym.vector.gradient(f).subs(C.y, sym.pi)
```




$\displaystyle (- \mathbf{{x}_{}})\mathbf{\hat{j}_{}}$




```python
sym.vector.gradient(f).to_matrix(C).subs(C.y,sym.pi)
```




$\displaystyle \left[\begin{matrix}0\\- \mathbf{{x}_{}}\\0\end{matrix}\right]$



## Directional derivative vector u


```python
u = 3 * C.i + 4 * C.j + 5 * C.k
u
```




$\displaystyle (3)\mathbf{\hat{i}_{}} + (4)\mathbf{\hat{j}_{}} + (5)\mathbf{\hat{k}_{}}$




```python
u.normalize()
```




$\displaystyle (\frac{3 \sqrt{2}}{10})\mathbf{\hat{i}_{}} + (\frac{2 \sqrt{2}}{5})\mathbf{\hat{j}_{}} + (\frac{\sqrt{2}}{2})\mathbf{\hat{k}_{}}$




```python
f = C.i + 3*C.j + C.k
g = C.x * sym.cos(C.y)
u = 3* C.i + 4* C.j + C.k
sym.vector.dot(f,u)
```




$\displaystyle 16$




```python
sym.vector.dot(g,u)
```




$\displaystyle 3 \left(\mathbf{\hat{i}_{}} \cdot \left(\mathbf{{x}_{}} \cos{\left(\mathbf{{y}_{}} \right)}\right)\right) + 4 \left(\mathbf{\hat{j}_{}} \cdot \left(\mathbf{{x}_{}} \cos{\left(\mathbf{{y}_{}} \right)}\right)\right) + \mathbf{\hat{k}_{}} \cdot \left(\mathbf{{x}_{}} \cos{\left(\mathbf{{y}_{}} \right)}\right)$




```python
sym.vector.dot(f,g)
```




$\displaystyle \mathbf{\hat{i}_{}} \cdot \left(\mathbf{{x}_{}} \cos{\left(\mathbf{{y}_{}} \right)}\right) + 3 \left(\mathbf{\hat{j}_{}} \cdot \left(\mathbf{{x}_{}} \cos{\left(\mathbf{{y}_{}} \right)}\right)\right) + \mathbf{\hat{k}_{}} \cdot \left(\mathbf{{x}_{}} \cos{\left(\mathbf{{y}_{}} \right)}\right)$




```python
sym.vector.gradient(f).to_matrix(C)
```




$\displaystyle \left[\begin{matrix}0\\0\\0\end{matrix}\right]$




```python
sym.vector.gradient(g).to_matrix(C)
```




$\displaystyle \left[\begin{matrix}\cos{\left(\mathbf{{y}_{}} \right)}\\- \mathbf{{x}_{}} \sin{\left(\mathbf{{y}_{}} \right)}\\0\end{matrix}\right]$




```python
sym.vector.gradient(g).dot(u)
```




$\displaystyle - 4 \mathbf{{x}_{}} \sin{\left(\mathbf{{y}_{}} \right)} + 3 \cos{\left(\mathbf{{y}_{}} \right)}$




```python
sym.vector.gradient(g).dot(u).subs([(C.x,1),(C.y,sym.pi)])
```




$\displaystyle -3$



# [Extreme Values and Saddle Points](https://www.youtube.com/watch?v=Teb28OFMVFc)
Extream values of f(x,y) can occur at
> Boundary points of the domain of f  
> Critical Points($f_x = f_y = 0$)  
> if
>> $ f_{xx} \lt 0 $ and $ f_{xx}f_{yy} - f{xy}^2 \gt 0 $ at (a,b) then local maximum  
>> $ f_{xx} \gt 0 $ and $ f_{xx}f_{yy} - f{xy}^2 \gt 0 $ at (a,b) then local minimum  
>> $ f_{xx}f_{yy} - f_{xy}^2 \lt 0$ at (a,b) then saddle point  
>> $ f_{xx}f_{yy} - f_{xy}^2 = 0$ at (a,b) then inconclusive


```python
x = sym.symbols('x')
f = sym.symbols('f',cls=sym.Function)
f = C.x**3 + 3*C.x*C.y + C.y**3
sym.diff(f,C.x,C.x)
sym.vector.gradient(f)
```




$\displaystyle (3 \mathbf{{x}_{}}^{2} + 3 \mathbf{{y}_{}})\mathbf{\hat{i}_{}} + (3 \mathbf{{x}_{}} + 3 \mathbf{{y}_{}}^{2})\mathbf{\hat{j}_{}}$




```python
sym.diff(f,C.y,C.y)
```




$\displaystyle 6 x_1$




```python
sym.solve([sym.diff(f,C.x),sym.diff(f,C.y)])
```




    [{.x: -1, .y: -1},
     {.x: 0, .y: 0},
     {.x: -(1/2 - sqrt(3)*I/2)**2, .y: 1/2 - sqrt(3)*I/2},
     {.x: -(1/2 + sqrt(3)*I/2)**2, .y: 1/2 + sqrt(3)*I/2}]




```python
fxx = sym.diff(f,C.x,C.x)
fyy = sym.diff(f,C.y,C.y)
fxy = sym.diff(f,C.x,C.y)
fxx.subs([(C.x,-1),(C.y,-1)])
```




$\displaystyle -6$




```python
(fxx*fyy - fxy**2).subs([(C.x,-1),(C.y,-1)])
# therefore at(-1,-1) lcoal minimum
```




$\displaystyle 27$



# Lagrange Mulipliers
## [let find a point(a,b,c) on g(x,y,z) from Min or Max value](https://www.youtube.com/watch?v=lmD9p6J_-TA)
> Minimize $f(x,y,z)$ subject to the constraint $g(x,y,z)=0,  g(a,b,c) = 0$  
>> Requires solving the two euqations $\nabla f = \lambda \nabla \text{ and } g(x,y,z) = 0 $  
>> why $\lambda$:  
>>> $\because$ each diffenrence Magnitude of gradient 

> Example: 
>> A space probe has the shape of an elipsoid $4x^2 + y^2 + 4z^2 = 16$ and after setting in the sun for an hour. the temperture on its surface is given by $`T(x,y,z) = 8x^2 + 4yz - 16z + 600$. Find the hottest point on the surfae  
>
>>Note: 
>>> that $ g = 4x^2 + y^2 + 4z^2 - 16 = 0 $ and $ f = T = 8x^2 + 4yz - 16z + 600 $ here


```python
C = sym.vector.CoordSys3D('')
```


```python
lam = sym.symbols('\lambda')
g = 4*C.x**2 + C.y**2 + 4*C.z**2 - 16
T = 8*C.x**2 + 4*C.y*C.z - 16*C.z + 600
print(f'Df: {sym.vector.gradient(T)} \nDg:{sym.vector.gradient(g)}')
```

    Df: 16*.x*.i + 4*.z*.j + (4*.y - 16)*.k 
    Dg:8*.x*.i + 2*.y*.j + 8*.z*.k



```python
eq1 = sym.vector.gradient(T) - lam*sym.vector.gradient(g)
eq1 = eq1.to_matrix(C)
s1 = sym.solve(eq1)
s1
```




    [{.x: 0,
      .y: -4/((\lambda - 1)*(\lambda + 1)),
      .z: -2*\lambda/((\lambda - 1)*(\lambda + 1))},
     {.y: -4/3, .z: -4/3, \lambda: 2}]




```python
s2 = sym.solve(g)
print(g)
s2
```

    4*.x**2 + .y**2 + 4*.z**2 - 16





    [{.x: -sqrt(-.y**2 - 4*.z**2 + 16)/2}, {.x: sqrt(-.y**2 - 4*.z**2 + 16)/2}]




```python
sol = sym.solve([(sym.vector.gradient(T) - lam*sym.vector.gradient(g)).to_matrix(C), g])
sol[0]
g.subs(sol[0])
T.subs(sol[0])
```




$\displaystyle \frac{1928}{3}$




```python
for s in sol:
    print(T.subs(s))
```

    1928/3
    24*sqrt(3) + 600
    600 - 24*sqrt(3)
    600
    1928/3


# MUltiple Integrals
in rare cases these can be done symbolically
> $$ \int_0^1 \int_0^{1-x^2} \int_3^{4-x^2-y^2} xe^{-y}cos(z)dzdydx $$


```python
import scipy as sci
import scipy.integrate
import numpy as np

sci.integrate.tplquad(lambda x,y,z:x*np.exp(-y)*np.cos(z), 0,1,lambda x:0, lambda x:1-x**2, lambda x,y:3, lambda x,y:4-x**2-y**2)


```




    (0.8652852789233809, 3.877301196381849e-14)



# Integrals and vector Fields
> Work 
>> $ W = \vec{F} \cdot \vec{c}$   
>> $ d\,W = \vec{F} \cdot d\vec{r}$   
>> $ d\,W = \vec{U} \cdot d\,\vec{r}$
>>> d : diferential  of $\vec{r}$  
>>> $\vec{r}$ : position vector  
>>> u : Potential Energy

## Line Inregrals(Scalar)
> Given $\vec{r(t)} = (g(t),h(t),k(t))$ the line integral $f(x,y,z)$ along the curve is
$$ \int_C f(x,y,z)dL = \int_a^b f(g(t),h(t),k(t)) |\frac{\vec{dl}}{dt}| dt $$


```python
t = sym.symbols('t')
x,y,z,f = sym.symbols('x y z f',cls=sym.Function,real=True)
x = x(t)
y = y(t)
z = z(t)
r = sym.Matrix([x,y,z])
f = f(x,y,z)
```


```python
sym.diff(r,t).norm()
sym.diff(r,t)
```




$\displaystyle \left[\begin{matrix}\frac{d}{d t} x{\left(t \right)}\\\frac{d}{d t} y{\left(t \right)}\\\frac{d}{d t} z{\left(t \right)}\end{matrix}\right]$




```python
f*sym.diff(r,t).norm()
```




$\displaystyle \sqrt{\left|{\frac{d}{d t} x{\left(t \right)}}\right|^{2} + \left|{\frac{d}{d t} y{\left(t \right)}}\right|^{2} + \left|{\frac{d}{d t} z{\left(t \right)}}\right|^{2}} f{\left(x{\left(t \right)},y{\left(t \right)},z{\left(t \right)} \right)}$



### 1. suppose
$ 
\vec{r(t)} = < cos(t), sin(t), t > \\
f(x,y,z) = 2xy + \sqrt{z}
$


```python
integrand = (
    f*sym.diff(r,t).norm()).subs(
        [(f,2*x*y+sym.sqrt(z)),
         (x,sym.cos(t)),
         (y,sym.sin(t)),(z,t)
        ])

integrand1=integrand.doit().simplify()

sym.integrate(integrand1,(t,0,2*sym.pi))
```




$\displaystyle \frac{8 \pi^{\frac{3}{2}}}{3}$



## 2. suppose
$
\vec{r(t)} = < 3cos(t), 2sin(t), e^{t/4} > \\
f(x,y,z) = 2xy + \sqrt{z}
$


```python
integrand2=(f*sym.diff(r,t).norm()).subs(
    [
        (f,2*x*y+sym.sqrt(z)),(x,3*sym.cos(t)),(y,sym.sin(t)),(z,sym.exp(t/4))
    ]
).doit().simplify()
sym.integrate(integrand2,(t,0,2*sym.pi))
```




$\displaystyle \frac{\int\limits_{0}^{2 \pi} 3 \sqrt{e^{\frac{t}{2}} + 144 \sin^{2}{\left(t \right)} + 16 \cos^{2}{\left(t \right)}} \sin{\left(2 t \right)}\, dt}{4} + \frac{\int\limits_{0}^{2 \pi} \sqrt{e^{\frac{t}{2}} + 144 \sin^{2}{\left(t \right)} + 16 \cos^{2}{\left(t \right)}} e^{\frac{t}{8}}\, dt}{4}$




```python
integrate_num = sym.lambdify([t],integrand2)
sci.integrate.quad(integrate_num,0,2*np.pi)
```




    (21.05530045675979, 3.960606771059637e-08)



# Line Integrals (Vector)
> Given $ \vec{r(t)} = < g(t),h(t),k(t) > $ 
the line integral of $ \vec{F}(x,y,z,) $  along the curve is
$$
\int_c \vec{F}(x,y,z)\cdot d\,\vec{r} = \int_a^b \vec{F}(g(t),h(t),k(t))\cdot \frac{d\,\vec{r}}{dt}dt
$$


```python
F1,F2,F3 = sym.symbols('F_1 F_2 F_3',cls=sym.Function, real=True)
F1 = F1(x,y,z)
F2 = F2(x,y,z)
F3 = F3(x,y,z)
r = sym.Matrix([x,y,z])
F = sym.Matrix([F1,F2,F3])
F
```




$\displaystyle \left[\begin{matrix}\operatorname{F_{1}}{\left(x{\left(t \right)},y{\left(t \right)},z{\left(t \right)} \right)}\\\operatorname{F_{2}}{\left(x{\left(t \right)},y{\left(t \right)},z{\left(t \right)} \right)}\\\operatorname{F_{3}}{\left(x{\left(t \right)},y{\left(t \right)},z{\left(t \right)} \right)}\end{matrix}\right]$




```python
F.dot(sym.diff(r,t))
```




$\displaystyle \operatorname{F_{1}}{\left(x{\left(t \right)},y{\left(t \right)},z{\left(t \right)} \right)} \frac{d}{d t} x{\left(t \right)} + \operatorname{F_{2}}{\left(x{\left(t \right)},y{\left(t \right)},z{\left(t \right)} \right)} \frac{d}{d t} y{\left(t \right)} + \operatorname{F_{3}}{\left(x{\left(t \right)},y{\left(t \right)},z{\left(t \right)} \right)} \frac{d}{d t} z{\left(t \right)}$



# 1. suppose
$
\vec{F} = \langle \sqrt{z} - 2x , \sqrt{y} \rangle \text{ and } \vec{r(t)} = \langle t, r^2,r^4 \rangle \text{ from } t = 0 \text{ to } t = 1
$


```python
integrand1 = (F.dot(sym.diff(r,t))).subs(
    [(F1,sym.sqrt(z)),(F2,-2*x),(F3,sym.sqrt(y)),(x,t),(y,t**2),(z,t**4)]
).doit().simplify()
```


```python
sym.integrate(integrand1,(t,0,1))
```




$\displaystyle - \frac{1}{5}$




```python
integrand1
```




$\displaystyle 4 t^{3} \sqrt{t^{2}} - 4 t^{2} + \sqrt{t^{4}}$




```python

```

d5431a8b638a524c4fd1606c67a6c3078ed59d44
ipynb
Jupyter Notebook
python/Template/gradient.ipynb
karng87/nasm_game
a97fdb09459efffc561d2122058c348c93f1dc87
MIT
python/Template/gradient.ipynb
karng87/nasm_game
a97fdb09459efffc561d2122058c348c93f1dc87
MIT
python/Template/gradient.ipynb
karng87/nasm_game
a97fdb09459efffc561d2122058c348c93f1dc87
MIT
Qwen/Qwen-72B
1. YES
2. YES