【问题标题】:Numerical Solutions for System of Non-Linear Equation in PythonPython中非线性方程组的数值解
【发布时间】:2021-01-16 05:57:21
【问题描述】:

我有两个简单的方程式:

k = 2.013*h^0.4917 and h = 3.57*k^0.4917

这些方程可以解析求解,其中 k = 5.77 和 h = 8.47。我尝试使用 fsolve 在 Python 中解决它,我遵循了以下方法: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html#scipy.optimize.fsolve

下面是我的代码:

from scipy.optimize import fsolve

def equations(x):
    return[x[0] - 2.013*x[1]**0.4917,x[1] - 3.57*x[0]**0.4917]

root =  fsolve(equations, [1, 1])

结果是

<ipython-input-133-11ce0ecaa7e4>:2: RuntimeWarning: invalid value encountered in double_scalars
  return[x[0] - 2.013*x[1]**0.4917,x[1] - 3.57*x[0]**0.4917]
RuntimeWarning: The iteration is not making good progress, as measured by the 
  improvement from the last ten iterations.
  warnings.warn(msg, RuntimeWarning)


print(root)
array([1., 1.])

我不确定我在这里做错了什么,以至于我没有得到正确的结果。你想指出我在这个问题上的错误吗?谢谢。

【问题讨论】:

    标签: python scipy-optimize


    【解决方案1】:
    1. fsolve 不知道您的变量是非负的。求解器进入负区(因为从 (1, 1) 梯度告诉我们要向负区移动),在那里得到 NaN,然后​​卡住了。您应该以某种方式告诉您在哪里寻找解决方案。 fsolve 不直接支持边界。 least_squares 可以做到这一点。

    2. 有几种解决方案。 (0, 0) 也是合适的。你可能不得不以某种方式摆脱它,因为它似乎不是你想从求解器那里得到的。

    你可以试试这样的。

    from scipy.optimize import least_squares
    
    def equations(x):
        return[x[0] - 2.013*x[1]**0.4917,x[1] - 3.57*x[0]**0.4917]
    
    root = least_squares(equations, [2, 2], bounds=([1, 1], [10, 10]))
    print(root)
    
    x: array([5.74279193, 8.43196966])
    

    【讨论】:

      猜你喜欢
      • 2019-04-05
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 2022-09-27
      • 1970-01-01
      相关资源
      最近更新 更多