maths.euler_modified

Functions

euler_modified(→ numpy.ndarray)

Calculate solution at each step to an ODE using Euler's Modified Method

Module Contents

maths.euler_modified.euler_modified(ode_func: collections.abc.Callable, y0: float, x0: float, step_size: float, x_end: float) numpy.ndarray

Calculate solution at each step to an ODE using Euler’s Modified Method The Euler Method is straightforward to implement, but can’t give accurate solutions. So, some changes were proposed to improve accuracy.

https://en.wikipedia.org/wiki/Euler_method

Arguments: ode_func – The ode as a function of x and y y0 – the initial value for y x0 – the initial value for x stepsize – the increment value for x x_end – the end value for x

>>> # the exact solution is math.exp(x)
>>> def f1(x, y):
...     return -2*x*(y**2)
>>> y = euler_modified(f1, 1.0, 0.0, 0.2, 1.0)
>>> float(y[-1])
0.503338255442106
>>> import math
>>> def f2(x, y):
...     return -2*y + (x**3)*math.exp(-2*x)
>>> y = euler_modified(f2, 1.0, 0.0, 0.1, 0.3)
>>> float(y[-1])
0.5525976431951775