# load dependacies

using MyCalculus
using Plots
using Symbolics

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

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

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
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])
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.

@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₀)])
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}
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}$$