返回介绍

Finding roots

发布于 2025-02-25 23:43:53 字数 2427 浏览 0 评论 0 收藏 0

For root finding, we generally need to proivde a starting point in the vicinitiy of the root. For iD root finding, this is often provided as a bracket (a, b) where a and b have opposite signs.

Univariate roots and fixed points

def f(x):
    return x**3-3*x+1
x = np.linspace(-3,3,100)
plt.axhline(0)
plt.plot(x, f(x));

from scipy.optimize import brentq, newton
brentq(f, -3, 0), brentq(f, 0, 1), brentq(f, 1,3)
(-1.8794, 0.3473, 1.5321)
newton(f, -3), newton(f, 0), newton(f, 3)
(-1.8794, 0.3473, 1.5321)

Finding fixed points

Finding the fixed points of a function \(g(x) = x\) is the same as finding the roots of \(g(x) - x\). However, specialized algorihtms also exist - e.g. using scipy.optimize.fixedpoint .

from scipy.optimize import fixed_point
def f(x, r):
    """Discrete logistic equation."""
    return r*x*(1-x)
n = 100
fps = np.zeros(n)
for i, r in enumerate(np.linspace(0, 4, n)):
    fps[i] = fixed_point(f, 0.5, args=(r, ))
plt.plot(np.linspace(0, 4, n), fps);

Mutlivariate roots and fixed points

from scipy.optimize import root, fsolve
def f(x):
    return [x[1] - 3*x[0]*(x[0]+1)*(x[0]-1),
            .25*x[0]**2 + x[1]**2 - 1]
sol = root(f, (0.5, 0.5))
sol
 status: 1
success: True
    qtf: array([ -1.4947e-08,   1.2702e-08])
   nfev: 21
      r: array([ 8.2295, -0.8826, -1.7265])
    fun: array([ -1.6360e-12,   1.6187e-12])
      x: array([ 1.1169,  0.8295])
message: 'The solution converged.'
   fjac: array([[-0.9978,  0.0659],
      [-0.0659, -0.9978]])
f(sol.x)
[-0.0000, 0.0000]
sol = root(f, (12,12))
sol
 status: 1
success: True
    qtf: array([ -1.5296e-08,   3.5475e-09])
   nfev: 33
      r: array([-10.9489,   6.1687,  -0.3835])
    fun: array([  4.7062e-13,   1.4342e-10])
      x: array([ 0.778 , -0.9212])
message: 'The solution converged.'
   fjac: array([[ 0.2205, -0.9754],
      [ 0.9754,  0.2205]])
f(sol.x)
[0.0000, 0.0000]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文