fluxion v0.2.0 Fluxion.ODE.Rk4 View Source

A fourth-order Runge-Kutta solver for initial value problems of Ordinary Differential Equations of the form @y’(t) = f(t, y(t))@.

RK4 generates a sequence of approximate @y(t)@ values defined:

@q_1 = f(t_k, y_k)@

@q_2 = f(t_k + \frac{h}{2}, y_k + \frac{h}{2}q_1)@

@q_3 = f(t_k + \frac{h}{2}, y_k + \frac{h}{2}q_2)@

@q_4 = f(t_k + h, y_k + hq_3)@

@y_{k+1} = y_k + \frac{h}{6}[q_1 + 2q_2 + 2q_3 + q_4]@

Link to this section Summary

Functions

Compute N iterations of the RK4 method and return the computed value for @y(t + stride * iterations)@

Link to this section Functions

Link to this function step(ode, initial, stride, iterations \\ 1) View Source
step(
  ode :: Fluxion.ODE.t(),
  initial :: number(),
  stride :: number(),
  iterations :: number()
) :: number()

Compute N iterations of the RK4 method and return the computed value for @y(t + stride * iterations)@.

Example

For the purpose of example, start from a known function for @y@ like @y(t) = sin(t)@. This gives @y’(t) = cos(t)@. Given this, it’s possible to approximate the real solution with the RK4 method.

iex> defmodule TSq do
...>   @behaviour Fluxion.ODE
...>   @impl true
...>   def f(x, _y_k), do: :math.cos(x)
...> end
iex> steps = 10
iex> stride = 0.01
iex> # Compute the real value of y(t) after 10 steps
iex> real_y_1 = :math.sin(stride*steps)
iex> # Approximate the value of y(t) after 10 steps
iex> approx_y_1 = Fluxion.ODE.Rk4.step(TSq, 0 , stride, steps)
iex> # Compare the results
iex> Fluxion.Math.is_equal(real_y_1, approx_y_1, 1.0e-10)
:true