Euler Estimation

by Justin Skycak on

Arrays can be used to implement more than just matrices. We can also implement other mathematical procedures like Euler estimation.

This post is part of the book Introduction to Algorithms and Machine Learning: from Sorting to Strategic Agents. Suggested citation: Skycak, J. (2021). Euler Estimation. In Introduction to Algorithms and Machine Learning: from Sorting to Strategic Agents. https://justinmath.com/euler-estimation/


Arrays can be used to implement more than just matrices. We can also implement other mathematical procedures like Euler estimation. We will jump straight to exercises – it is assumed that you’re already familiar with Euler estimation from calculus.

Exercise: Single-Variable Euler Estimator

To start, build a single-variable Euler estimation class as follows:


>>> def derivative(t):
        return t+1
        
>>> euler = EulerEstimator(derivative)

>>> initial_point = (1,4)
>>> euler.eval_derivative(initial_point) # evaluates derivative at point (1,4)
2

>>> step_size = 0.5
>>> num_steps = 4
>>> euler.estimate_points(initial_point, step_size, num_steps)
[
    (1,   4   ),   # starting point
    (1.5, 5   ),   # after 1st step
    (2,   6.25),   # after 2nd step
    (2.5, 7.75),   # after 3rd step
    (3,   9.5 )    # after 4th step
]

Then, use your Euler estimator to plot several solution curves to the following differential equation on the interval $x \in [0, 5].$ (Your Euler estimator generates a list of points, and then you can use that list of points to generate a plot.)

$\begin{align*} \dfrac{\textrm dy}{\textrm dx} = x - 2 \end{align*}$


For one curve, use the initial condition $y(0)=-2.$ For another curve, use $y(0)=-1.$ Then another curve with $y(0)=0,$ another with $y(0) = 1,$ and another with $y(0)=2.$ All $5$ of these curves can go on the same plot.

Based on your knowledge of calculus, you should be able to tell if your plots look right.

Exercise: Multivariable Euler Estimator

Once you’ve implemented a single-variable Euler estimator, you can generalize it to simulate systems of differential equations. For example, consider the following system:

$\begin{align*} a'(t) &= a(t) + 1 \\ b'(t) &= a(t) + b(t) \\ c'(t) &= 2b(t) + 3t \end{align*}$


To simulate this system starting with the initial state $a(-0.4) = -0.45,$ $b(-0.4) = -0.05,$ $c(-0.4) = 0,$ construct a multivariable Euler estimator as follows:


>>> initial_state = {'a': -0.45, 'b': -0.05, 'c': 0}

>>> initial_point = (-0.4, initial_state) # points take form (t, state)

>>> def da_dt(t, state):
        return state['a'] + 1

>>> def db_dt(t, state):
        return state['a'] + state['b']
        
>>> def dc_dt(t, state):
        return 2 * state['b'] + 3 * t
        
>>> derivatives = {
    'a': da_dt,
    'b': db_dt,
    'c': dc_dt
}
        
>>> euler = EulerEstimator(derivatives)

>>> euler.eval_derivative_at_point(initial_point)
{'a': 0.55, 'b': -0.5, 'c': -1.3}

>>> step_size = 2
>>> num_steps = 3
>>> euler.estimate_points(initial_point, step_size, num_steps)
[
   (-0.4, {'a': -0.45, 'b': -0.05, 'c': 0   }),
   (1.6,  {'a': 0.65,  'b': -1.05, 'c': -2.6}),
   (3.6,  {'a': 3.95,  'b': -1.85, 'c': 2.8 }),
   (5.6,  {'a': 13.85, 'b': 2.35,  'c': 17  })
]


This post is part of the book Introduction to Algorithms and Machine Learning: from Sorting to Strategic Agents. Suggested citation: Skycak, J. (2021). Euler Estimation. In Introduction to Algorithms and Machine Learning: from Sorting to Strategic Agents. https://justinmath.com/euler-estimation/