简体   繁体   English

求解多个非线性方程(带幂函数)

[英]solving multiple non linear equation (with power function)

I would like to solve this kind of equations:我想解决这种方程:

a*85**b+c=100
a*90**b+c=66
a*92**b+c=33

I tried this我试过这个

import scipy.optimize

def fun(variables) :
    (a,b,c)= variables
    eq0=a*85**b+c-100
    eq1=a*90**b+c-66
    eq2=a*92**b+c-33
    return [eq0,eq1,eq2]
result = scipy.optimize.fsolve(fun, (1, -1, 0)) 
print(result)

But I get ValueError: Integers to negative integer powers are not allowed.但我得到ValueError: Integers to negative integer powers are not allowed.

Then I tried the equivalent然后我尝试了等效的

    def fun(variables) :
    (a,b,c)= variables
    eq0=log(a)+b*log(85)-log(100-c)
    eq1=log(a)+b*log(90)-log(66-c)
    eq2=log(a)+b*log(92)-log(33-c)
    return [eq0,eq1,eq2]
result = scipy.optimize.fsolve(fun, (1, -1, 0)) 
print(result)

I get a solution but that is equal to the initial values (1, -1, 0) Thus when I test fun(result) , I get values different from zero.我得到了一个解决方案,但它等于初始值(1, -1, 0)因此,当我测试fun(result)时,我得到的值不同于零。

I have noticed that for this example the same problem is observed我注意到对于此示例,观察到相同的问题

import scipy.optimize
def fun(variables) :
    (x,y)= variables
    eqn_1 = x**2+y-4
    eqn_2 = x+y**2+3
    return [eqn_1,eqn_2]
result = scipy.optimize.fsolve(fun, (0.1, 1)) 
print(result)

fun(result)

Does anyone would know how I could do?有谁会知道我该怎么做? Thank you谢谢

PS I have posted here about sympy last week Resolution of multiple equations (with exponential) PS 我上周在这里发布了关于 sympy 的多个方程的分辨率(带指数)

When the initial condition is not well known, sometimes its best to try other methods first.当初始条件不清楚时,有时最好先尝试其他方法。 For small problems, simplex minimization is useful:对于小问题,单纯形最小化很有用:

import numpy as np
def func(x):
    a,b,c= x
    eq0=np.log(a)+b*np.log(85)-np.log(100-c)
    eq1=np.log(a)+b*np.log(90)-np.log(66-c)
    eq2=np.log(a)+b*np.log(92)-np.log(33-c)
    return eq0**2+ eq1**2 + eq2**2

def func_vec(x):
    a,b,c= x
    eq0=np.log(a)+b*np.log(85)-np.log(100-c)
    eq1=np.log(a)+b*np.log(90)-np.log(66-c)
    eq2=np.log(a)+b*np.log(92)-np.log(33-c)
    return eq0, eq1, eq2

from scipy.optimize import fsolve, minimize
out = minimize(func, [1,1,0], method="Nelder-Mead", options={"maxfev":100000})
print("roots:", out.x)
print("value at roots:", func_vec(out.x))

# roots: [ 7.87002460e+11 -1.07401055e-09 -7.87002456e+11]
# value at roots: (6.0964566728216596e-12, -1.2086331935279304e-11, 6.235012506294879e-12)

Note, I also tried [1,1,1] as an initial condition and found it converged to the wrong solution.请注意,我还尝试将 [1,1,1] 作为初始条件,发现它收敛到错误的解决方案。 Further increasing maxfev from 1e5 to 1e7 allowed [1,1,1] to converged to the proper solution, but then perhaps there are better methods to solve this.进一步将 maxfev 从 1e5 增加到 1e7 允许 [1,1,1] 收敛到正确的解决方案,但也许有更好的方法来解决这个问题。

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

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