# [first order differential eqations](https://people.math.wisc.edu/~angenent/519.2016s/notes/elementary-diffeqs.html)
> ## First Order Separable Equation
>> ## $
\frac{dy}{dx} = f(x,y) \\
y'(x) = F(x)G(y(x)), \quad or \quad \frac{dy}{dx} = F(x)G(y)
$
>>> ### not separable $ \frac{dy}{dx} = x + y \quad or \quad y' = x + y$

# The simplest case
> ## $ \frac{dx}{dt} = f(t), \quad x(t_0) = x_0$
>> ## $ x(t) = x_0 + \int_{t_0}^{t} f(s)ds $

# The general case
> ## $ \frac{dx}{dt} = f(t,x), \quad x(t_0) = x_0 $
>> ## $ x_0 - \delta \leq x \leq x_0 + \delta , \quad t_{-}  \leq t \leq t_{+} $

# [Proof by Picard iteration of Exisence Theorem](https://people.math.wisc.edu/~angenent/519.2016s/notes/picard.html#mjx-eqn-eqsimplest-ODE)
> ## $
x_0(t) = x_0 \\
\text{which gives us a new function } x_1(t)
$
>> ## $
\frac{dx_1}{dt} = f(t,x_0), \quad x_0(t_0) = x_0 \\
\frac{dx_2}{dt} = f(t,x_1(t)), \quad x_1(t0) = x_0 \\
\dots \\
\frac{dx_n}{dt} = f(t, x_{n-1}(t), \quad x_{n-1}(t_0) = x_0 \\
\therefore x(t) \overset{def}{=} \lim \limits_{k\to\infty} x_k(t) \\
$
> ## $$
x_k(t) = x_0 + \int_{t_0}^{t} f(s,x_{k-1}(s))ds \\
x_{k+1}(t) = x_0 + \int_{t_0}^{t} f(s,x_{k}(s))ds \\
$$  
>
> ## $
\begin{cases}
k \to \infty, \Rightarrow x_k(t) \to x(t) \\
k \to \infty, \Rightarrow y_k(x) \to y(x)
\end{cases}
$
> ## $$
x(t) = x_0 + \int_{t_0}^{t} f(s,x(s))ds \\
y(x) = y_0 + \int_{x_0}^{x} f(t,x(t))dt \\
$$


```python
import sympy as sym
t, x = sym.symbols('t x')
f,y = sym.symbols('f y',cls=sym.Function)
f.diff(x)

# y' = x + y
y = sym.dsolve(f(x).diff(x) - x - f(x),f(x))
C1,_ = y.free_symbols
y = y.subs(C1,2).simplify()

# y'= x + y
k1 = 2 + sym.integrate(t+2,(t,0,x))
k2 = 2 + sym.integrate(t+k1.subs(x,t),(t,0,x))
k3 = 2 + sym.integrate(t+k2.subs(x,t),(t,0,x))
k4 = 2 + sym.integrate(t+k3.subs(x,t),(t,0,x))
k5 = 2 + sym.integrate(t+k4.subs(x,t),(t,0,x))
k5
```




$\displaystyle \frac{x^{6}}{720} + \frac{x^{5}}{40} + \frac{x^{4}}{8} + \frac{x^{3}}{2} + \frac{3 x^{2}}{2} + 2 x + 2$



# [Picards method](https://www.youtube.com/watch?v=ZpvUyOvoNHg)
> # $ y'(x) = f(x,y) $
>> # $$ \begin{align} 
    & y(x_0) & = \quad & y_0 \quad \text{, given initial value}  \\
    & \frac{d\, y(x)}{dx} & =\quad  & f(x,y) \\
    & dy(x) & =\quad  & f(x,y)\,dx \\
    & \int_{y_0}^{y}dy(x) & =\quad  & \int_{x_0}^{x}f(t,y(x)) dx \\
    & y - y_0 & =\quad  & \int_{x_0}^{x}f(x,y)dx \\
    & \therefore y & =\quad  & y_0 + \int_{x_0}^{x}f(x,y)dx \\
\end{align}$$
>
> # first approximation method
>> # $$ y(x) = y_{0} + \int_{x_0}^{x} f(x,y(x)) dx $$  
>> # $$ y(x) = y_{0} + \int_{x_0}^{x} f(t,y(t)) dt $$


# [ODE]( https://www.youtube.com/watch?v=C0Hl8DXDJ2M&list=PLW1_9UnhaSkEdN19b2KTQm1ShFGdHHbIJ&index=2)
# $  
\begin{cases}
y = ce^x -x -1  
        \begin{cases}
           y' & = \frac{d}{dx}(ce^x - x -1) \\
           & = ce^x -1 \\
           & = ce^x -1 -x + x \\
           & = y + x\\
        \end{cases} \\
\therefore y' = y + x \\
\end{cases} 
$

## $ y'' =  a^2 y $
> ## $ y = c_{1}sinh(ax)+c_{2}cosh(ax)$
>> ## $ \because \text{Recall: } \begin{cases}
        \frac{d}{dx}sinh(x) = cosh(x) \\
        \frac{d}{dx}cosh(x)  = sinh(x)
        \end{cases} $
> ## $ y = c_1 sinh(ax) + c_2 cosh(ax)$
> ## $ y' = a c_1 cosh(ax) + ac_2sinh(ax)$
> ## $ y'' = a^2 c_1 sinh(ax) + a^2c_2cosh(ax)$


```python
import sympy as sym
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as pat

x,y,a,b,c,c1,c2 = sym.symbols(r'x y alpha beta c c_1 c_2')
f = sym.symbols('f',cls=sym.Function)
fx =  c1*sym.sinh(a*x) + c2*sym.cosh(a*x)
sym.diff(fx,x,x) == a**2*fx
```




    True




```python
g = c*sym.exp(x) - x 
dgdx = sym.diff(dgdx,x)
dgdx == g + x
```




    True




```python
def h(t,x):
    return x * sym.integrate(sym.sin(t)/t,(t,0,x))

t = sym.symbols('t')
f = x*sym.integrate(sym.sin(t)/t,(t,0,x))
sym.diff(h(t,x),x) == sym.sin(x) + f/x
```




    True




```python

```


```python
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot()
x = np.linspace(-7,7,1000)
ax.plot(x,np.sinh(x))
ax.plot(x,np.cosh(x))
ax.axis('scaled')
ax.set_xlim(xmin=-7,xmax=7)
ax.set_ylim(ymin=-7,ymax=7)
```


```python
import sympy as sym
x,y = sym.symbols('x y')
f,g = sym.symbols('f g', cls=sym.Function)
f = f(x)

sym.dsolve(f.diff(x)-1-f**2,f.simplify())
sym.dsolve(f.diff(x) + (x+2)*f**2,f,hint='separable')
sym.dsolve(g(x).diff(x)-2*sym.sec(2*g(x)),g(x),hint='separable')
```




    [Eq(g(x), -asin(C1 + 4*x)/2 + pi/2), Eq(g(x), asin(C1 + 4*x)/2)]




```python
#sym.dsolve(2*x*f(x)*f(x).diff(x)-f(x)**2 + x**2,f(x),hint='separable_reduced')
sym.dsolve(2*x*f*f.diff(x)-f**2 + x**2,f,simplify=False)
```




$\displaystyle x + \frac{f^{2}{\left(x \right)}}{x} = C_{1}$




```python
sym.dsolve(f.diff(x) - (4*x**2+f**2)/x*f,f,simplify=False)
```




$\displaystyle \frac{1}{f^{2}{\left(x \right)}} = \left(C_{1} - \operatorname{Ei}{\left(4 x^{2} \right)}\right) e^{- 4 x^{2}}$




```python
import sympy as sym
x,h = sym.symbols('x h')
f = lambda x:x/(x-1)
f(x+h)
Df = (f(x+h) - f(x))/h
Df.simplify()
a = sym.limit(Df,h,0)
a.factor() == sym.diff(f(x),x).factor()
sym.limit(sym.sin(x)/x,x,0)
```




$\displaystyle 1$




```python
sym.Integral(sym.exp(-x**2),x).doit()
sym.Integral(sym.exp(-x**2),x)
```




$\displaystyle \int e^{- x^{2}}\, dx$




```python
sym.integrate(sym.exp(-x**2),(x,-sym.oo,sym.oo))
```




$\displaystyle \sqrt{\pi}$



# Matrices
$$
A = \begin{pmatrix}
1 & 2 \\
3 & 4 \\
\end{pmatrix}
$$


```python
A = [[1,2],[3,4]]
B = [[5,6],[7,8]]
A = sym.Matrix(A)
B = sym.Matrix(B)
```


```python
A + B
```




$\displaystyle \left[\begin{matrix}6 & 8\\10 & 12\end{matrix}\right]$




```python
A * B
```




$\displaystyle \left[\begin{matrix}19 & 22\\43 & 50\end{matrix}\right]$




```python
A.det()
A.T
```




$\displaystyle \left[\begin{matrix}1 & 3\\2 & 4\end{matrix}\right]$



# Chain rule


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




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




```python
sym.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)}$




```python
w1 = x**2 + sym.exp(y) * sym.sin(z)
sym.diff(w1,t).subs([(x,sym.sin(t)),(y,sym.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)}$



# Vector CoordSys3D
> ### gradient


```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).to_matrix(C).subs(C.y,sym.pi)
```




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



# Directional Derivatives
$$
D_uf = \Delta f\cdot u
$$


```python
u = 4*C.i - 3*C.j + 2*C.k
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
sym.vector.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](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,y = sym.symbols('x y')
f = x**3 + 3*x*y + y**3
f
```




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




```python
sym.solve([sym.diff(f,x),sym.diff(f,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,x,x)
fyy = sym.diff(f,y,y)
fxy = sym.diff(f,x,y)
```




$\displaystyle 3$



# testing the first point (-1,-1)


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




$\displaystyle -6$




```python
(fxx * fyy - fxy**2).subs([(x,-1),(y,-1)])
# fxx < 0 and fxx * fyy - fxy**2 > 0 => local maximum
```




$\displaystyle 27$




```python

```

723d5f82f5d082ef6b3d3c70a9923de2bbfaa920
ipynb
Jupyter Notebook
python/Template/ode-picard-method.ipynb
karng87/nasm_game
a97fdb09459efffc561d2122058c348c93f1dc87
MIT
python/Template/ode-picard-method.ipynb
karng87/nasm_game
a97fdb09459efffc561d2122058c348c93f1dc87
MIT
python/Template/ode-picard-method.ipynb
karng87/nasm_game
a97fdb09459efffc561d2122058c348c93f1dc87
MIT
Qwen/Qwen-72B
1. YES
2. YES