简体   繁体   English

Python中非线性方程的数值解

[英]Numerical solution for non-linear equations in Python

I have a collection, the number of which may vary, of non-linear equations with constraints that I'd like to solve using some numerical approach.我有一个非线性方程的集合,其数量可能会有所不同,我想使用一些数值方法来解决这些约束。

I've been able to solve a simple (one equation) case in Excel using Solver, but haven't put anything like this together in Python before so would appreciate suggestions on approach.我已经能够使用解算器在 Excel 中解决一个简单的(一个方程)案例,但之前还没有在 Python 中将类似的东西放在一起,因此希望对方法提出建议。

Having done a bit of digging, it looks like fsolve is a popular approach for solving systems like these.经过一些挖掘,看起来 fsolve 是解决此类系统的流行方法。 For a simple, two equation case, my problem takes the following form, broken out into parts for clarity:对于一个简单的两个方程的情况,我的问题采用以下形式,为了清楚起见,将其分解为几个部分:

在此处输入图片说明 在此处输入图片说明

And the same form for the second, b, equation.第二个方程 b 的形式相同。

A is a constant, variable Z, S and x are constants for each entity i, and the only values that are independent are exponent a and b; A是常数,变量Z、S和x是每个实体i的常数,唯一独立的值是指数a和b; two equations, two unknowns, so there should be a single unique solution.两个方程,两个未知数,所以应该有一个唯一的解。

As I'd said, I set up the simple one equation case in Excel and successfully solved for using Solver.正如我所说,我在 Excel 中设置了一个简单的等式案例,并成功解决了使用 Solver 的问题。 Any guidance on setting this up in Python is appreciated.任何有关在 Python 中进行设置的指导表示赞赏。

The problem you're describing is one of root finding .您所描述的问题是寻根问题之一。 You want to find (a,b) for which f(a,b)=0你想找到 f(a,b)=0 的 (a,b)

A simple approach would be fixed point iteration.一种简单的方法是定点迭代。 Since you have an analytical expression for f(a,b), you could calculate the derivatives and use Newton's method.由于您有 f(a,b) 的解析表达式,您可以计算导数并使用牛顿法。 To set this up using fsolve you'll need to define a function:要使用 fsolve 进行设置,您需要定义一个函数:

def myfunc(x):
    val1 = #evaluate your first expression here using Z and S
    val2 = #evaluate your second expression here
    return np.ndarray([val1 val2])

You can optionally pass in your values for S and Z using the *args argument.您可以选择使用 *args 参数传入 S 和 Z 的值。

Then solve using:然后解决使用:

fsolve(myfunc,x0)

where x0 is an initial guess.其中 x0 是初始猜测。

Note that fsolve may not respect your condition on w.请注意, fsolve 可能不尊重您对 w 的条件。 If that isn't satisfied identically for your problem, I'd look into a method that supports constrained optimization such as fmin_slsqp .如果这对您的问题不完全满意,我会研究一种支持约束优化的方法,例如fmin_slsqp The syntax should be very similar to what I described for fsolve in either case.在任何一种情况下,语法都应该与我为 fsolve 描述的非常相似。

Was able to put together a solution with help from the above, appreciate it.能够在上述帮助下整理出解决方案,不胜感激。 I've accepted John's answer;我已经接受了约翰的回答; solution code below for reference.下面的解决方案代码供参考。

import pandas as pd
import numpy as np
from scipy.optimize import fsolve

Aq = .6
Av = .6

def eqs(p):
    a, b = p    
    return(np.dot(x*(qS**a*vS**b)/np.dot(x,qS**a*vS**b),qZ)-Aq 
            , np.dot(x*(qS**a*vS**b)/np.dot(x,qS**a*vS**b),vZ)-Av)

sol = fsolve(eqs, (1,1), full_output=True)
x, y = sol[0]

Here is an example of how to setup a Python solution for non-linear equations:以下是如何为非线性方程设置 Python 解决方案的示例:

import numpy as np
from scipy.optimize import fsolve
from math import cos

# non-linear equations:
#  x0 cos(x1) = 4.
#  x0x1-x1 = 5.
def func2(x):
    out = [x[0]*cos(x[1]) - 4]
    out.append(x[1]*x[0] - x[1] - 5)
    return out


x02 = fsolve(func2, [1, 1])
print("x02: "+str(x02))

Prints: x02: [6.50409711 0.90841421]打印:x02:[6.50409711 0.90841421]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM