calculate numeric solution at each step to an ode using euler s method for reference to euler s method refer to https en wikipedia orgwikieulermethod args odefunc callable the ordinary differential equation as a function of x and y y0 float the initial value for y x0 float the initial value for x stepsize float the increment value for x xend float the final value of x to be calculated returns np ndarray solution of y for every step in x the exact solution is math expx def fx y return y y0 1 y expliciteulerf y0 0 0 0 01 5 y1 144 77277243257308 calculate numeric solution at each step to an ode using euler s method for reference to euler s method refer to https en wikipedia org wiki euler_method args ode_func callable the ordinary differential equation as a function of x and y y0 float the initial value for y x0 float the initial value for x step_size float the increment value for x x_end float the final value of x to be calculated returns np ndarray solution of y for every step in x the exact solution is math exp x def f x y return y y0 1 y explicit_euler f y0 0 0 0 01 5 y 1 144 77277243257308
from collections.abc import Callable

import numpy as np


def explicit_euler(
    ode_func: Callable, y0: float, x0: float, step_size: float, x_end: float
) -> np.ndarray:
    
    n = int(np.ceil((x_end - x0) / step_size))
    y = np.zeros((n + 1,))
    y[0] = y0
    x = x0

    for k in range(n):
        y[k + 1] = y[k] + step_size * ode_func(x, y[k])
        x += step_size

    return y


if __name__ == "__main__":
    import doctest

    doctest.testmod()