```julia
using MyCalculus
using Plots
using Symbolics
```

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




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




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


    fun (generic function with 1 method)



```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.0,1.0), framestyle = :origin)

```

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



```julia
@variables x₀, y₀, step
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
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
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}



## Now we reduce step size:


```julia
x1, y1 = EulerMethod(fun, -1.0, -0.5, 0.5, 25)
x2, y2 = EulerMethod(fun, -1.0, -0.5, 0.25, 100)
x3, y3 = EulerMethod(fun, -1.0, -0.5, 0.125, 1000)
x4, y4 = EulerMethod(fun, -1.0, -0.5, 0.0125, 1000)
```


    ([-1.0, -0.9875, -0.975, -0.9625, -0.95, -0.9375, -0.925, -0.9125, -0.9, -0.8875  …  11.375, 11.3875, 11.4, 11.4125, 11.425, 11.4375, 11.45, 11.4625, 11.475, 11.4875], [-0.5, -0.484375, -0.4690985107421875, -0.45416034308243075, -0.43955082286707137, -0.42526076129353235, -0.4112814273548329, -0.39760452219874504, -0.3842221552478339, -0.37112682194054275  …  -3.350340007934192, -3.352217780824636, -3.3540944801986754, -3.355970107922435, -3.3578446658565735, -3.3597181558563056, -3.3615905797714243, -3.3634619394463243, -3.365332236720024, -3.3672014734261864])



```julia
plot(x1,y1, ylim=(-4.0,0.5), xlim=(-1.0,10.0), framestyle = :origin) # you can see some instability around x=8.0
plot!(x2,y2, ylim=(-4.0,0.5), xlim=(-1.0,10.0), framestyle = :origin) 
plot!(x3,y3, ylim=(-4.0,0.5), xlim=(-1.0,10.0), framestyle = :origin)
plot!(x4,y4, ylim=(-4.0,0.5), xlim=(-1.0,10.0), framestyle = :origin)
```


```julia
x = 0, y=0
```


```julia

```

7f281ae307a9fd097d9533929db6da91b16a6504
ipynb
Jupyter Notebook
src/sketch.ipynb
gjunqueira-sys/MyCalculus.jl
9a1dee9be36b805e9523ca6d047d827f58c29a62
MIT
src/sketch.ipynb
gjunqueira-sys/MyCalculus.jl
9a1dee9be36b805e9523ca6d047d827f58c29a62
MIT
src/sketch.ipynb
gjunqueira-sys/MyCalculus.jl
9a1dee9be36b805e9523ca6d047d827f58c29a62
MIT
Qwen/Qwen-72B
1. YES
2. YES