簡體   English   中英

解決變量受約束的非線性方程組

[英]Solve a nonlinear equation system with constraints on the variables

一些用fsolve解決非線性方程組的假設示例:

from scipy.optimize import fsolve
import math

def equations(p):
    x, y = p
    return (x+y**2-4, math.exp(x) + x*y - 3)

x, y =  fsolve(equations, (1, 1))

print(equations((x, y)))

是否可以使用scipy.optimize.brentq以一定間隔(例如[-1,1])解決該問題? 在這種情況下,拆包如何工作?

正如sascha所建議的那樣,約束優化是最簡單的方法。 least_squares方法在這里很方便:您可以直接將equations傳遞給它,它將最小化其成分的平方和。

from scipy.optimize import least_squares
res = least_squares(equations, (1, 1), bounds = ((-1, -1), (2, 2)))

bounds的結構是((min_first_var, min_second_var), (max_first_var, max_second_var)) ,或者對於更多變量類似。

結果對象具有一堆字段,如下所示。 最相關的是: res.cost本質上為零,這意味着找到了根; res.x指出根是什么:[0.62034453,1.83838393]

 active_mask: array([0, 0])
        cost: 1.1745369255773682e-16
         fun: array([ -1.47918522e-08,   4.01353883e-09])
        grad: array([  5.00239352e-11,  -5.18964300e-08])
         jac: array([[ 1.        ,  3.67676787],
       [ 3.69795254,  0.62034452]])
     message: '`gtol` termination condition is satisfied.'
        nfev: 7
        njev: 7
  optimality: 8.3872972696740977e-09
      status: 1
     success: True
           x: array([ 0.62034453,  1.83838393])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM