```python
import numpy as np
import sympy as smp
from sympy.vector import *
import matplotlib.pyplot as plt
from scipy.integrate import quad
from scipy.integrate import quad_vec
```


```python
x, y, z, u1, u2, u3, v1, v2, v3, t = smp.symbols('x y z u_1 u_2 u_3 v_1 v_2 v_3 t')
```

# Vectors and Geometry

Numerical or symbolic


```python
a = np.array([1,2,3])
b = np.array([7,8,9])
u = smp.Matrix([u1,u2,u3])
v = smp.Matrix([v1,v2,v3])
```

## Addition and multiplication


```python
2*a+b
```




    array([ 9, 12, 15])




```python
2*u+v
```




$\displaystyle \left[\begin{matrix}2 u_{1} + v_{1}\\2 u_{2} + v_{2}\\2 u_{3} + v_{3}\end{matrix}\right]$



## Dot products


```python
np.dot(a, b)
```




    50




```python
u.dot(v)
```




$\displaystyle u_{1} v_{1} + u_{2} v_{2} + u_{3} v_{3}$



## Cross products


```python
np.cross(a,b)
```




    array([-6, 12, -6])




```python
u.cross(v)
```




$\displaystyle \left[\begin{matrix}u_{2} v_{3} - u_{3} v_{2}\\- u_{1} v_{3} + u_{3} v_{1}\\u_{1} v_{2} - u_{2} v_{1}\end{matrix}\right]$



## Length of vector


```python
np.linalg.norm(a)
```




    3.7416573867739413




```python
u.norm()
```




$\displaystyle \sqrt{\left|{u_{1}}\right|^{2} + \left|{u_{2}}\right|^{2} + \left|{u_{3}}\right|^{2}}$



## Vector projection

$\text{proj}_v(u) = \frac{u \cdot v}{|v|^2} v$


```python
proj_b_a = np.dot(a,b)/np.linalg.norm(b)**2 * b
proj_b_a
```




    array([1.80412371, 2.06185567, 2.31958763])




```python
proj_v_u = u.dot(v)/v.norm()**2 * v
proj_v_u
```




$\displaystyle \left[\begin{matrix}\frac{v_{1} \left(u_{1} v_{1} + u_{2} v_{2} + u_{3} v_{3}\right)}{\left|{v_{1}}\right|^{2} + \left|{v_{2}}\right|^{2} + \left|{v_{3}}\right|^{2}}\\\frac{v_{2} \left(u_{1} v_{1} + u_{2} v_{2} + u_{3} v_{3}\right)}{\left|{v_{1}}\right|^{2} + \left|{v_{2}}\right|^{2} + \left|{v_{3}}\right|^{2}}\\\frac{v_{3} \left(u_{1} v_{1} + u_{2} v_{2} + u_{3} v_{3}\right)}{\left|{v_{1}}\right|^{2} + \left|{v_{2}}\right|^{2} + \left|{v_{3}}\right|^{2}}\end{matrix}\right]$



## Lines/Planes in Space

Lines: $\vec{r}(t) = \vec{r}_0 + t\vec{v}$


```python
r0 = smp.Matrix([1,1,1])
v = smp.Matrix([1,3,-1])
r = r0 + t*v
r
```




$\displaystyle \left[\begin{matrix}t + 1\\3 t + 1\\1 - t\end{matrix}\right]$



Planes: $\vec{n} \cdot (P_0-\left<x,y,z\right>) = 0$


```python
P0 = smp.Matrix([4,4,8])
r = smp.Matrix([x,y,z])
n = smp.Matrix([1,1,1])
n.dot(P0-r)
```




$\displaystyle - x - y - z + 16$



**Examples** Find vector parallel to the line of intersection of the two planes $3x-6y-2z = 15$ and $2x+y-2z=5$. (It's  going to be perpendicular to both normal vectors)


```python
n1 = np.array([-3,6,2])
n2 = np.array([-2,-1,2])
ans = np.cross(n1,n2)
ans
```




    array([14,  2, 15])



# Vector Calculus

## Vector Derivatives


```python
r = smp.Matrix([3*t, smp.sin(t), t**2])
r
```




$\displaystyle \left[\begin{matrix}3 t\\\sin{\left(t \right)}\\t^{2}\end{matrix}\right]$




```python
smp.diff(r,t)
```




$\displaystyle \left[\begin{matrix}3\\\cos{\left(t \right)}\\2 t\end{matrix}\right]$



Example: Find the angle between the velocity and acceleration as a function of time $\theta(t)$


```python
v = smp.diff(r,t)
a = smp.diff(v,t)
theta = smp.acos(v.dot(a)/(v.norm()*a.norm())).simplify()
```


```python
theta
```




$\displaystyle \operatorname{acos}{\left(\frac{4 t - \frac{\sin{\left(2 t \right)}}{2}}{\sqrt{\left|{\sin{\left(t \right)}}\right|^{2} + 4} \sqrt{4 \left|{t}\right|^{2} + \left|{\cos{\left(t \right)}}\right|^{2} + 9}} \right)}$




```python
theta.subs(t,6).evalf()
```




$\displaystyle 0.251108015692338$




```python
tt = np.linspace(0, 10, 100)
aa = smp.lambdify([t], theta)(tt)
plt.plot(tt,aa)
plt.xlabel('$t$', fontsize=20)
plt.ylabel(r'$\theta(t)$', fontsize=20)
plt.show()
```

## Vector Integrals

Note: Will not add the +C


```python
r = smp.Matrix([smp.exp(t)*smp.cos(t), t**4, 1/(1+t**2)])
r
```




$\displaystyle \left[\begin{matrix}e^{t} \cos{\left(t \right)}\\t^{4}\\\frac{1}{t^{2} + 1}\end{matrix}\right]$




```python
smp.Integral(r).doit()
```




$\displaystyle \left[\begin{matrix}\frac{e^{t} \sin{\left(t \right)}}{2} + \frac{e^{t} \cos{\left(t \right)}}{2}\\\frac{t^{5}}{5}\\\operatorname{atan}{\left(t \right)}\end{matrix}\right]$



In some cases integral cannot be evaluated symbolically: must solve definite integrals numerically:


```python
r = smp.Matrix([smp.exp(t**2)*smp.cos(t)**3, smp.exp(-t**4), 1/(3+t**2)])
r
```




$\displaystyle \left[\begin{matrix}e^{t^{2}} \cos^{3}{\left(t \right)}\\e^{- t^{4}}\\\frac{1}{t^{2} + 3}\end{matrix}\right]$



Integrate from $t=0$ to $t=4$


```python
r_num = smp.lambdify([t], r)
```


```python
r_num = smp.lambdify([t], r)
quad_vec(r_num, 0, 4)[0]
```




    array([[-4.83559254e+05],
           [ 9.06402477e-01],
           [ 6.70972506e-01]])



## Arclength

$$L = \int_a^b \sqrt{dx^2 + dy^2 + dz^2} = \int_a^b \sqrt{(dx/dt)^2 + (dy/dt)^2 + (dz/dt)^2 } dt $$

Rare Cases can be done symbolically. Find arclength of $\left< 0, t, t^2 \right>$ from $t=0$ to $t=1$


```python
r = smp.Matrix([0, t, t**2])
smp.integrate(smp.diff(r,t).norm(), (t,0,1))
```




$\displaystyle \frac{\operatorname{asinh}{\left(2 \right)}}{4} + \frac{\sqrt{5}}{2}$



In most cases needs to be done numerically. Find arclength of $\left< e^t, \sin(t), t^4 \right>$ from $t=0$ to $t=1$


```python
r = smp.Matrix([smp.exp(t), smp.sin(t), t**4])
r_num = smp.lambdify([t], smp.diff(r,t).norm())
quad(r_num, 0, 1)[0]
```




    2.316977146444862



## Other Relavent Quantities

If $ds = \sqrt{dx^2 + dy^2 + dz^2}$ is the arclength element $ds/dt = \sqrt{(dx/dt)^2 + (dy/dt)^2 + (dz/dt)^2 } = |d\vec{r}/dt| = |\vec{v}|$ then the other quantities of importance are

* Unit Tangent vector: $\vec{T} = \frac{d\vec{r}}{dt}\frac{1}{|d\vec{r}/dt|} = \vec{v}/|\vec{v}|$ 
* Curvature: $\kappa = |\frac{d\vec{T}}{dt}|\frac{1}{|\vec{v}|}$
* Unit normal vector to Curve: $\vec{N} = \frac{d\vec{T}/dt}{|d\vec{T}/dt|}$

**Example** Find all these for $\vec{r}(t) = \left<a\cos(t)e^t, b\sin(t), ct\right>$


```python
t, a, b, c = smp.symbols('t a b c', pos=True, real=True)
```

Define the path


```python
r = smp.Matrix([a*smp.cos(t)*smp.exp(t), b*smp.sin(t), c*t])
r
```




$\displaystyle \left[\begin{matrix}a e^{t} \cos{\left(t \right)}\\b \sin{\left(t \right)}\\c t\end{matrix}\right]$



Define the velocity vector $\vec{v}$ and speed $|\vec{v}|$


```python
v = smp.diff(r, t)
v_norm = v.norm()
```

Get $\vec{T}$, $\kappa$, and $\vec{N}$


```python
T = v/v_norm
kappa = smp.diff(T, t).norm()/v_norm
N = smp.diff(T,t)/smp.diff(T,t).norm()
```

Can evaluate these quantities or make plots


```python
kappa.subs([(t,2),(a,3),(b,4),(c,5)]).evalf()
```




$\displaystyle 0.00775458595406284$




```python
N.subs([(t,2),(a,3),(b,4),(c,5)]).evalf()
```




$\displaystyle \left[\begin{matrix}-0.152946073876551\\-0.204518432770271\\-0.966840063890063\end{matrix}\right]$



Can also make continuous plots


```python
kappa_num = smp.lambdify([t,a,b,c], kappa)
```


```python
tt = np.linspace(0, 2, 100)
kk = kappa_num(tt, 1, 2, 3)
plt.plot(tt, kk)
plt.xlabel('$t$', fontsize=20)
plt.ylabel('$\kappa(t)$', fontsize=20)
plt.show()
```

# Partial/Directional Derivatives


```python
x, y, z = smp.symbols('x y z')
```

## Basics

Partial Derivatives $\partial f / \partial x$ and $\partial f / \partial y$ of $f(x,y)=y^2\sin(x+y)$


```python
f = y**2 * smp.sin(x+y)
smp.diff(f,x)
```




$\displaystyle y^{2} \cos{\left(x + y \right)}$




```python
smp.diff(f,y)
```




$\displaystyle y^{2} \cos{\left(x + y \right)} + 2 y \sin{\left(x + y \right)}$



Or more complicated things like $\frac{\partial^3 f}{\partial xy^2}$


```python
smp.diff(f,y,y,x)
```




$\displaystyle - y^{2} \cos{\left(x + y \right)} - 4 y \sin{\left(x + y \right)} + 2 \cos{\left(x + y \right)}$



## The Chain Rule


```python
t = smp.symbols('t')
x, y, z, w, v =smp.symbols('x, y, z w v', cls=smp.Function)
```

* Suppose $x$, $y$, and $z$ are functions of $t$ 
* $w$ is a function of $x$, $y$, and $z$. i.e $w=w(x,y,z)$

Find $dw/dt$


```python
x = x(t)
y = y(t)
z = z(t)
w = w(x, y, z)
```

Can keep things in a more functional form:


```python
smp.diff(w,t)
```




$\displaystyle \frac{d}{d x{\left(t \right)}} w{\left(x{\left(t \right)},y{\left(t \right)},z{\left(t \right)} \right)} \frac{d}{d t} x{\left(t \right)} + \frac{d}{d y{\left(t \right)}} w{\left(x{\left(t \right)},y{\left(t \right)},z{\left(t \right)} \right)} \frac{d}{d t} y{\left(t \right)} + \frac{d}{d z{\left(t \right)}} w{\left(x{\left(t \right)},y{\left(t \right)},z{\left(t \right)} \right)} \frac{d}{d t} z{\left(t \right)}$



Or put in specific functions:


```python
w1 = x**2+smp.exp(y)*smp.sin(z)
smp.diff(w1,t).subs([(x, smp.sin(t)),
                    (y, smp.cos(t)),
                    (z, t**2)]).doit()
```




$\displaystyle 2 t e^{\cos{\left(t \right)}} \cos{\left(t^{2} \right)} - e^{\cos{\left(t \right)}} \sin{\left(t \right)} \sin{\left(t^{2} \right)} + 2 \sin{\left(t \right)} \cos{\left(t \right)}$



## Gradients

Gradients: $\nabla f$. Now we are dealing with particular coordinate systems, so we must be careful:


```python
C = CoordSys3D('')
```


```python
f = C.x*smp.sin(C.y)
gradient(f)
```




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




```python
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
gradient(f).to_matrix(C).subs([(C.y, 1)])
```




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



## Directional Derivatives

Directional derivatives $D_u f = \nabla f \cdot u$


```python
u = 4*C.i - 3*C.j + 2*C.k
# Make into unit vector
u = u.normalize()
u
```




$\displaystyle (\frac{4 \sqrt{29}}{29})\mathbf{\hat{i}_{}} + (- \frac{3 \sqrt{29}}{29})\mathbf{\hat{j}_{}} + (\frac{2 \sqrt{29}}{29})\mathbf{\hat{k}_{}}$




```python
gradient(f).dot(u)
```




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



## Extreme Values and Saddle Points

Extreme values of $f(x,y)$ can occur at

* 1. Boundary points of the domain of $f$
* 2. Critical Points ($f_x=f_y=0$)

If

* (i) $f_{xx}<0$ and $f_{xx}f_{yy} - f_{xy}^2>0$ at (a,b) then **local maximum**
* (ii) $f_{xx}>0$ and $f_{xx}f_{yy} - f_{xy}^2>0$ at (a,b) then **local minimum**
* (iii) $f_{xx}f_{yy} - f_{xy}^2 < 0$ at (a,b) then **saddle point**
* (iv) $f_{xx}f_{yy} - f_{xy}^2 = 0$ at (a,b) then **inconclusive**


```python
x, y = smp.symbols('x y', real=True)
f = x**3 + 3*x*y + y**3
f
```




$\displaystyle x^{3} + 3 x y + y^{3}$




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




    [{x: -1, y: -1}, {x: 0, y: 0}]




```python
fxx = smp.diff(f,x,x)
fyy = smp.diff(f,y,y)
fxy = smp.diff(f,x,y)
```

Testing the first point


```python
fxx.subs([(x,-1),(y,-1)])
```




$\displaystyle -6$




```python
test = fxx*fyy-fxy**2
test.subs([(x,-1),(y,-1)])
```




$\displaystyle 27$



Thus the first point is a local maximum!

## Lagrange Multipliers

Minimize $f(x,y,z)$ subject to the constraint $g(x,y,z)=0$

* Requires solving the two equations $\nabla f = \lambda \nabla g$ and $g(x,y,z)=0$

**Example**: A space probe has the shape of an ellipsoid $4x^2 + y^2 + 4z^2 = 16$ and after sitting in the sun for an hour. the temperature on its surface is given by $T(x,y,z)=8x^2+4yz-16z+600$. Find the hottest point on the surface

Note that $g=4x^2 + y^2 + 4z^2 - 16 = 0$ and $f=T = 8x^2+4yz-16z+600$ here


```python
C = CoordSys3D('')
```


```python
eq1 = gradient(f)-lam*gradient(g)
eq1 = eq1.to_matrix(C)
```


```python
lam = smp.symbols('\lambda')
g = 4*C.x**2 + C.y**2 + 4*C.z**2 - 16
f = 8*C.x**2 + 4*C.y*C.z-16*C.z+600
eq1 = gradient(f)-lam*gradient(g)
eq1 = eq1.to_matrix(C)
eq2 = g
sols = smp.solve([eq1, eq2])
sols
```




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




```python
for sol in sols:
    print(f.subs(sol).evalf())
```

    642.666666666667
    641.569219381653
    558.430780618347
    600.000000000000
    642.666666666667


First and last solutions are the points where the probe is the hottest.

# 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} xdzdydx$$


```python
x, y, z = smp.symbols('x, y, z')
f = x
smp.integrate(f, (z, 3, 4-x**2-y**2), (y, 0, 1-x**2), (x, 0, 1))
```




$\displaystyle \frac{1}{8}$



But most of the time they need to be done numerically, such as in this example:

$$\int_0^1 \int_0^{1-x^2} \int_3^{4-x^2-y^2} xe^{-y}\cos(z)dzdydx$$


```python
# Wont Run!!
#x, y, z = smp.symbols('x, y, z')
#f = x*smp.exp(-y)*smp.cos(z)
#smp.integrate(f, (z, 3, 4-x**2-y**2), (y, 0, 1-x**2), (x, 0, 1))
```

Use scipy to evaluate this numerically


```python
from scipy.integrate import tplquad
f = lambda z, y, x: x*np.exp(-y)*np.cos(z)
tplquad(f, 0, 1,
        lambda x: 0, lambda x: 1-x**2,
        lambda x, y: 3, lambda x, y: 4-x**2-y**2)[0]
```




    -0.09109526451447894



# Integrals and Vector Fields

## Line Integrals (Scalar)

Given $\vec{r}(t) = \left< g(t), h(t), k(t) \right>$ the line integral of $f(x,y,z)$ along the curve is

$$\int_C f(x,y,z) ds = \int_a^b f(g(t), h(t), k(t))|d\vec{r}/dt|dt$$


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


```python
integrand = f*smp.diff(r,t).norm()
integrand
```




$\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)}$



Suppose 
* $\vec{r}(t) = \left<\cos(t), \sin(t), t \right>$ 
* $f(x,y,z)=2xy+\sqrt{z}$ 

and we are going from $t=0$ to $t=2\pi$. In this case it can be solved symbolically:


```python
integrand1 = integrand.subs([(f, 2*x*y+smp.sqrt(z)),
                            (x, smp.cos(t)),
                            (y, smp.sin(t)),
                            (z, t)]).doit().simplify()
smp.integrate(integrand1, (t, 0, 2*smp.pi))
```




$\displaystyle \frac{\sqrt{2}}{2} + \sqrt{2} \left(- \frac{1}{2} + \frac{4 \sqrt{2} \pi^{\frac{3}{2}}}{3}\right)$



In general, can't be done symbolically. Consider now

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

and we are going from $t=0$ to $t=2\pi$.


```python
integrand2 = integrand.subs([(f, 2*x*y+smp.sqrt(z)),
                            (x, 3*smp.cos(t)),
                            (y, smp.sin(t)),
                            (z, smp.exp(t/4))]).doit().simplify()
# Won't Run!
#smp.integrate(integrand2, (t, 0, 2*smp.pi))
```


```python
integrand2
```




$\displaystyle \frac{\left(e^{\frac{t}{8}} + 3 \sin{\left(2 t \right)}\right) \sqrt{e^{\frac{t}{2}} + 128 \sin^{2}{\left(t \right)} + 16}}{4}$



Do it numerically using scipy's `quad` function


```python
integrand2_num = smp.lambdify([t], integrand2)
quad(integrand2_num, 0, 2*np.pi)[0]
```




    21.05530045675979



## Line Integrals (Vector)

Given $\vec{r}(t) = \left< g(t), h(t), k(t) \right>$ 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
t = smp.symbols('t', real=True)
x, y, z, F1, F2, F3 = smp.symbols('x y z F_1, F_2, F_3', cls=smp.Function, real=True)
x = x(t)
y = y(t)
z = z(t)
F1 = F1(x,y,z)
F2 = F2(x,y,z)
F3 = F3(x,y,z)
r = smp.Matrix([x, y, z])
F = smp.Matrix([F1, F2, F3])
```


```python
integrand = F.dot(smp.diff(r,t))
integrand
```




$\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)}$



Suppose $\vec{F} = \left< \sqrt{z}, -2x, \sqrt{y}\right>$ and $\vec{r}(t) = \left<t, t^2, t^4 \right>$ from $t=0$ to $t=1$


```python
integrand1 = integrand.subs([(F1, smp.sqrt(z)),
                             (F2, -2*x),
                             (F3, smp.sqrt(y)),
                             (x, t),
                             (y, t**2),
                             (z, t**4)]).doit().simplify()
smp.integrate(integrand1, (t, 0, 1))
```




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



Often times this can't be done by hand, and one needs to evaluate numerically using scipy:

Suppose $\vec{F} = \left< \sqrt{|z|}, -2x, \sqrt{|y|}\right>$ and $\vec{r}(t) = \left<3\cos^2(t), t^2, 2\sin(t) \right>$ from $t=0$ to $t=2\pi$


```python
integrand2 = integrand.subs([(F1, smp.sqrt(smp.Abs(z))),
                             (F2, -2*x),
                             (F3, smp.sqrt(smp.Abs(y))),
                             (x, 3*smp.cos(t)**2),
                             (y, t**2),
                             (z, 2*smp.sin(t))]).doit().simplify()
# Won't Run
#smp.integrate(integrand2, (t, 0, 2*smp.pi))
```


```python
integrand2
```




$\displaystyle 2 \left(- 6 t \cos{\left(t \right)} - 3 \sqrt{2} \sin{\left(t \right)} \sqrt{\left|{\sin{\left(t \right)}}\right|} + \left|{t}\right|\right) \cos{\left(t \right)}$



Do it numerically using scipy's `quad` function


```python
integrand2_num = smp.lambdify([t], integrand2)
quad(integrand2_num, 0, 2*np.pi)[0]
```




    -118.4352528130723



## Surface Integrals (Scalar)

Area of a surface parameterized by $\vec{r}(u,v)$ is given by 

$$A = \iint_S \left| \frac{d\vec{r}}{du} \times \frac{d\vec{r}}{dv} \right| du dv$$

The surface integral of a scalar function $G(\vec{r})$ is given by

$$\iint_S G(\vec{r}(u,v)) \left| \frac{d\vec{r}}{du} \times \frac{d\vec{r}}{dv} \right| du dv$$

2D parabola: $\vec{r}(x,y) = \left<x, y, x^2+y^2 \right>$ and thus $\vec{r}(\rho, \theta) = \left<\rho\cos\theta, \rho\sin\theta, \rho^2\right>$. Suppose the surface density is given by $G(x,y,z)=x^2+y^2$. Find surface integral for $0<\rho<1$ and $0<\theta<2\pi$


```python
rho, the = smp.symbols(r'\rho \theta',pos=True, real=True)
x, y, z, G = smp.symbols('x y z G', cls=smp.Function, real=True)
x = x(rho, the)
y = y(rho, the)
z = z(rho, the)
G = G(x,y,z)
r = smp.Matrix([x, y, z])
```


```python
integrand = G*smp.diff(r,rho).cross(smp.diff(r,the)).norm()
integrand
```




$\displaystyle \sqrt{\left|{\frac{\partial}{\partial \rho} x{\left(\rho,\theta \right)} \frac{\partial}{\partial \theta} y{\left(\rho,\theta \right)} - \frac{\partial}{\partial \theta} x{\left(\rho,\theta \right)} \frac{\partial}{\partial \rho} y{\left(\rho,\theta \right)}}\right|^{2} + \left|{\frac{\partial}{\partial \rho} x{\left(\rho,\theta \right)} \frac{\partial}{\partial \theta} z{\left(\rho,\theta \right)} - \frac{\partial}{\partial \theta} x{\left(\rho,\theta \right)} \frac{\partial}{\partial \rho} z{\left(\rho,\theta \right)}}\right|^{2} + \left|{\frac{\partial}{\partial \rho} y{\left(\rho,\theta \right)} \frac{\partial}{\partial \theta} z{\left(\rho,\theta \right)} - \frac{\partial}{\partial \theta} y{\left(\rho,\theta \right)} \frac{\partial}{\partial \rho} z{\left(\rho,\theta \right)}}\right|^{2}} G{\left(x{\left(\rho,\theta \right)},y{\left(\rho,\theta \right)},z{\left(\rho,\theta \right)} \right)}$




```python
integrand1 = integrand.subs([(G, x**2+y**2),
                             (x, rho*smp.cos(the)),
                             (y, rho*smp.sin(the)),
                             (z, rho**2)]).doit().simplify()
integrand1
```




$\displaystyle \rho^{2} \sqrt{4 \rho^{2} + 1} \left|{\rho}\right|$




```python
smp.integrate(integrand1, (the, 0, 2*smp.pi), (rho, 0, 1))
```




$\displaystyle \frac{\pi}{60} + \frac{5 \sqrt{5} \pi}{12}$



As before, if this integral can't be done symbolically, need to use scipy to convert to numerical function.

## Surface Integrals (Vector)

The surface integral of a vector function $\vec{G}(\vec{r})$ is given by

$$\iint_S \vec{G}(\vec{r}(u,v)) \cdot \left(\frac{d\vec{r}}{du} \times \frac{d\vec{r}}{dv}\right) du dv$$

This is also know as the flux of the vector field $\vec{G}$ through the surface

2D parabola: $\vec{r}(x,y) = \left<x, y, x^2+y^2 \right>$ and thus $\vec{r}(\rho, \theta) = \left<\rho\cos\theta, \rho\sin\theta, \rho^2\right>$. Suppose a vector field is given by $\vec{G}(x,y,z)=\left<y^2, z, 0 \right>$. Find the flux of $\vec{G}$ through half the surface for $0<\rho<1$ and $0<\theta<\pi$


```python
rho, the = smp.symbols(r'\rho \theta',pos=True, real=True)
x, y, z, G1, G2, G3 = smp.symbols('x y z G_1, G_2, G_3', cls=smp.Function, real=True)
x = x(rho, the)
y = y(rho, the)
z = z(rho, the)
G1 = G1(x,y,z)
G2 = G2(x,y,z)
G3 = G3(x,y,z)
r = smp.Matrix([x, y, z])
G = smp.Matrix([G1, G2, G3])
```


```python
integrand = G.dot(smp.diff(r,rho).cross(smp.diff(r,the)))
integrand
```




$\displaystyle \left(\frac{\partial}{\partial \rho} x{\left(\rho,\theta \right)} \frac{\partial}{\partial \theta} y{\left(\rho,\theta \right)} - \frac{\partial}{\partial \theta} x{\left(\rho,\theta \right)} \frac{\partial}{\partial \rho} y{\left(\rho,\theta \right)}\right) \operatorname{G_{3}}{\left(x{\left(\rho,\theta \right)},y{\left(\rho,\theta \right)},z{\left(\rho,\theta \right)} \right)} + \left(- \frac{\partial}{\partial \rho} x{\left(\rho,\theta \right)} \frac{\partial}{\partial \theta} z{\left(\rho,\theta \right)} + \frac{\partial}{\partial \theta} x{\left(\rho,\theta \right)} \frac{\partial}{\partial \rho} z{\left(\rho,\theta \right)}\right) \operatorname{G_{2}}{\left(x{\left(\rho,\theta \right)},y{\left(\rho,\theta \right)},z{\left(\rho,\theta \right)} \right)} + \left(\frac{\partial}{\partial \rho} y{\left(\rho,\theta \right)} \frac{\partial}{\partial \theta} z{\left(\rho,\theta \right)} - \frac{\partial}{\partial \theta} y{\left(\rho,\theta \right)} \frac{\partial}{\partial \rho} z{\left(\rho,\theta \right)}\right) \operatorname{G_{1}}{\left(x{\left(\rho,\theta \right)},y{\left(\rho,\theta \right)},z{\left(\rho,\theta \right)} \right)}$




```python
integrand1 = integrand.subs([(G1, y**2),
                             (G2, z),
                             (G3, 0),
                             (x, rho*smp.cos(the)),
                             (y, rho*smp.sin(the)),
                             (z, rho**2)]).doit().simplify()
integrand1
```




$\displaystyle - 2 \rho^{4} \left(\frac{\sin{\left(2 \theta \right)}}{2} + 1\right) \sin{\left(\theta \right)}$




```python
smp.integrate(integrand1, (the, 0, smp.pi), (rho, 0, 1))
```




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



Again, if this could not be done symbolically, one would need to convert the integrand into a numpy function and integrate using scipys `quad` function, like in previous examples.

## Explicit Sympy Functionality


```python
from sympy.vector import ParametricRegion
```

Find the mass of a cylinder with radius $a$ and height $h$ centered at origin with density $\rho(x,y)=x^2+y^2$


```python
a, h, r, the, z = smp.symbols(r'a h r \theta z', positive=True)
cylinder = ParametricRegion((r*smp.cos(the), r*smp.sin(the), z),
                    (the, 0, 2*smp.pi), (z, 0, h), (r, 0, a))
vector_integrate(C.x**2 + C.y**2, cylinder)
```




$\displaystyle \frac{\pi a^{4} h}{2}$



Not necessarily a fan of this functionailty because there is no clear way of converting it into an numerical function if it cannot be computed symbolically. For more see

https://docs.sympy.org/latest/modules/vector/vector_integration.html

cadd9b044027831bec859b3345862a8591ed10e2
ipynb
Jupyter Notebook
Maths/math2.ipynb
Sumanshekhar17/Pseudo-Spectral-Method
5d1c2449bafec89006de95f785fe569d488792d9
MIT
2021-09-05T18:21:22.000Z
2021-09-05T18:26:21.000Z
Maths/math2.ipynb
Sumanshekhar17/Pseudo-Spectral-Method
5d1c2449bafec89006de95f785fe569d488792d9
MIT
2021-09-05T18:26:10.000Z
2021-09-14T08:54:49.000Z
Maths/math2.ipynb
Sumanshekhar17/Pseudo-Spectral-Method
5d1c2449bafec89006de95f785fe569d488792d9
MIT
2021-09-06T16:36:35.000Z
2021-09-06T16:36:35.000Z
Qwen/Qwen-72B
1. YES
2. YES