# "Symbolic Euler's Method"
> "Applying Euler's Method to ODE , but with a twist: we're going to call method with Symbolic variables"
- toc: true
- badges: true
- comments: true
- categories: [jupyter, math, calculus, symbolics, julialang]


```julia
#collapse-show

# load dependacies

using MyCalculus
using Plots
using Symbolics
```

## Appply Euler's method for the following First-Order Differential Equation:




$fun(t, y) = y^2 -t$




```julia
# function definition : RHS of ODE

fun(t, y) = y^2 -t
```


    fun (generic function with 1 method)


## Definition of Euler's Method function:

$EulerMethod(f, x₀, y₀, step= 0.5, n=100.0)$

Function to approximate solution to an ordinary differential equation using the Euler's Method.
dy/dx = f(x, y) where y(x₀) = y₀ and x₀ = x₀.

 __Arguments__

- $f:$ function to approximate solution to, of the form f(x, y). 
        This is the right hand side of the differential equation.
- $x₀:$ initial $x$ value condition
- $y₀:$ initial $y$ value condition
- $step:$ step size for the Euler's Method
- $n:$ number of steps to take

__Returns__
- $x:$ array of x values
- $y:$ array of y values


```julia
x, y = EulerMethod(fun, -1.0, -0.5, 0.5, 5) 
```


    ([-1.0, -0.5, 0.0, 0.5, 1.0], [-0.5, 0.125, 0.3828125, 0.456085205078125, 0.31009206222370267])



```julia
plot(x,y, ylim=(-0.5,0.5), xlim=(-1,1), framestyle = :, ylabel = "y", xlabel = "x")

```

### We can also use Symbolics by passing Symbolics variables to the function:

This is a good way to "peek" into the inner workings of the Euler's Method and see what it's doing for every iteration.

It also showcases the power of Symbolics. Due to  highly composable julia code, we can use Symbolics everywhere a number is expected.



```julia
@variables x₀, y₀, step #defines the symbollics variables
t, y =  EulerMethod(fun, x₀, y₀, step, 5) 
```


    (Num[x₀, step + x₀, x₀ + 2step, x₀ + 3step, x₀ + 4step], Num[y₀, y₀ + step*(y₀^2 - x₀), y₀ + step*(y₀^2 - x₀) + step*((y₀ + step*(y₀^2 - x₀))^2 - step - x₀), y₀ + step*(y₀^2 - x₀) + step*((y₀ + step*(y₀^2 - x₀))^2 - step - x₀) + step*((y₀ + step*(y₀^2 - x₀) + step*((y₀ + step*(y₀^2 - x₀))^2 - step - x₀))^2 - 2step - x₀), y₀ + step*(y₀^2 - x₀) + step*((y₀ + step*(y₀^2 - x₀))^2 - step - x₀) + step*((y₀ + step*(y₀^2 - x₀) + step*((y₀ + step*(y₀^2 - x₀))^2 - step - x₀))^2 - 2step - x₀) + step*((y₀ + step*(y₀^2 - x₀) + step*((y₀ + step*(y₀^2 - x₀))^2 - step - x₀) + step*((y₀ + step*(y₀^2 - x₀) + step*((y₀ + step*(y₀^2 - x₀))^2 - step - x₀))^2 - 2step - x₀))^2 - 3step - x₀)])



```julia
#collapse-output
t;
```

\begin{equation} \left[ \begin{array}{c} x{_0} \\ step + x{_0} \\ x{_0} + 2 step \\ x{_0} + 3 step \\ x{_0} + 4 step \\ \end{array} \right] \end{equation}


```julia
#collapse-output
y;
```

\begin{equation} \left[ \begin{array}{c} y{_0} \\ y{_0} + step \left( - x{_0} + y{_0}^{2} \right) \\ y{_0} + step \left( - x{_0} + y{_0}^{2} \right) + step \left( - step - x{_0} + \left( y{_0} + step \left( - x{_0} + y{_0}^{2} \right) \right)^{2} \right) \\ y{_0} + step \left( - x{_0} + y{_0}^{2} \right) + step \left( - step - x{_0} + \left( y{_0} + step \left( - x{_0} + y{_0}^{2} \right) \right)^{2} \right) + step \left( - 2 step - x{_0} + \left( y{_0} + step \left( - x{_0} + y{_0}^{2} \right) + step \left( - step - x{_0} + \left( y{_0} + step \left( - x{_0} + y{_0}^{2} \right) \right)^{2} \right) \right)^{2} \right) \\ y{_0} + step \left( - x{_0} + y{_0}^{2} \right) + step \left( - step - x{_0} + \left( y{_0} + step \left( - x{_0} + y{_0}^{2} \right) \right)^{2} \right) + step \left( - 2 step - x{_0} + \left( y{_0} + step \left( - x{_0} + y{_0}^{2} \right) + step \left( - step - x{_0} + \left( y{_0} + step \left( - x{_0} + y{_0}^{2} \right) \right)^{2} \right) \right)^{2} \right) + step \left( - 3 step - x{_0} + \left( y{_0} + step \left( - x{_0} + y{_0}^{2} \right) + step \left( - step - x{_0} + \left( y{_0} + step \left( - x{_0} + y{_0}^{2} \right) \right)^{2} \right) + step \left( - 2 step - x{_0} + \left( y{_0} + step \left( - x{_0} + y{_0}^{2} \right) + step \left( - step - x{_0} + \left( y{_0} + step \left( - x{_0} + y{_0}^{2} \right) \right)^{2} \right) \right)^{2} \right) \right)^{2} \right) \\ \end{array} \right] \end{equation}


```julia

```


```julia

```


```julia

```

ca76e14ca7d2e0fcb32e1dc68f1f4f5437a2666f
ipynb
Jupyter Notebook
src/2021-12-28-EulersMethod.ipynb
gjunqueira-sys/MyCalculus.jl
9a1dee9be36b805e9523ca6d047d827f58c29a62
MIT
src/2021-12-28-EulersMethod.ipynb
gjunqueira-sys/MyCalculus.jl
9a1dee9be36b805e9523ca6d047d827f58c29a62
MIT
src/2021-12-28-EulersMethod.ipynb
gjunqueira-sys/MyCalculus.jl
9a1dee9be36b805e9523ca6d047d827f58c29a62
MIT
Qwen/Qwen-72B
1. YES
2. YES