【问题标题】:How to optimize non-linear equations in python?如何优化python中的非线性方程?
【发布时间】:2020-01-19 11:57:19
【问题描述】:

我已经写到这里了,但我无法继续。如何定义 x0、x1 和 x2?我该如何解决这个问题?

def nonlin(a,b,c):
    return [3*x0-cos(x1*x2)+ a, x0*x0-81*(x1+0.1)**2+ sin(x2)+b, exp(-x0*x1)+20*x2+c]

【问题讨论】:

  • 你可能想看看here

标签: python arrays numpy scipy


【解决方案1】:

您可以尝试使用scipy.optimize.fsolve 解决此问题:

from scipy.optimize import fsolve
from numpy import cos, sin, exp

# Define the non-linear equations
def equations(X, a,b,c):
    x0, x1, x2 = X
    return [3*x0-cos(x1*x2)+ a, x0*x0-81*(x1+0.1)**2+ sin(x2)+b, exp(-x0*x1)+20*x2+c]

a = 1.0
b = 0.5
c = 2.5

# Using (0.0, 0.0, 0.0) as an initial vector
x0, x1, x2 = fsolve(equations, x0=(0.,0.,0.), args=(a,b,c)) 
print(x0, x1, x2)

输出

-6.826133854836766e-06 -0.03657006638562982 -0.17499998751839313

【讨论】:

  • 你为什么这样做? ---> x0, x1, x2 = X 为什么假设 a b 和 c 为 1?
猜你喜欢
  • 1970-01-01
  • 2014-03-13
  • 2018-06-26
  • 1970-01-01
  • 1970-01-01
  • 2021-04-04
  • 2016-08-28
  • 2013-04-20
  • 1970-01-01
相关资源
最近更新 更多