同步操作将从 编程语言算法集/Python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
from collections.abc import Callableimport numpy as npdef euler_modified(ode_func: Callable, y0: float, x0: float, step_size: float, x_end: float) -> np.ndarray:"""Calculate solution at each step to an ODE using Euler's Modified MethodThe 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_methodArguments:ode_func -- The ode as a function of x and yy0 -- the initial value for yx0 -- the initial value for xstepsize -- the increment value for xx_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"""n = int(np.ceil((x_end - x0) / step_size))y = np.zeros((n + 1,))y[0] = y0x = x0for k in range(n):y_get = y[k] + step_size * ode_func(x, y[k])y[k + 1] = y[k] + ((step_size / 2) * (ode_func(x, y[k]) + ode_func(x + step_size, y_get)))x += step_sizereturn yif __name__ == "__main__":import doctestdoctest.testmod()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。