四个非线性方程的求解系统

时间:2018-08-23 12:25:27

标签: optimization scipy

我正在尝试使用array([(253, '12:00', [('area1', 'active'), ('area2', 'inactive'), ('area3', 'inactive')])], dtype=[('message_counter', '<i8'), ('alteration_time', '<U32'), ('area_states', [('area', '<U32'), ('state', '<U32')], (3,))]) 模块中的fsolve来解决由四个非线性方程组成的系统。雅可比矩阵(scipy.optimize)用fjac填充。我不明白我在做什么错。我一直在寻找类似的问题,但没有一个问题与我的问题相似,这使我得出结论,我的尝试存在根本上的错误。

我的代码:

nan

输出:

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

#Constants
Cn = 1.0
Cf = 0.8
Nn = 3.0
gamma = 1.4
R = 287.1 #J/kg*K
dn = 0.5 * 25.4 #mm
df = 2.1516 * 25.4 #mm
dv = 1.8553 * 25.4 #mm
dbt = 0.89 * 25.4 #mm
Tb = (40 + 273.15) #K
Pr = (13 + 1.01325) * 10 ** 5 #N/m2

guess_massflow = 0.3 # kg/s
guess_Pc = 12 * 10 ** 5 #N/m2
guess_Pb = 10 * 10 ** 5 #N/m2
guess_Ps = 14 * 10 ** 5 #N/m2

def f(p):

    massflow, Pb, Pc, Ps = p

    def pi_mod(d):
        return (math.pi * d ** 2) / 4 * 10 ** (-6)
    major_mod = Nn * Cn * Pb / (Tb ** 0.5)
    radical = (gamma + 1)/(gamma - 1)
    minor_mod = (gamma / R * (2 / (gamma + 1)) ** radical) ** 0.5

    radical2 = (gamma - 1) / gamma

    def main_mod(P1, P2):
        return P2 * (1/R/Tb * 2/radical2 * (P1/P2) ** (2/gamma) * (1 - (P1/P2) ** radical2)) ** 0.5

    f1 = massflow - major_mod * pi_mod(dn) * minor_mod
    f2 = massflow - Cf * pi_mod(df) * main_mod(Pb,Pc)
    f3 = massflow - Cf * pi_mod(dbt) * main_mod(Pc,Ps)
    f4 = massflow - Cf * pi_mod(dv) * main_mod(Ps,Pr)

    return (f1, f2, f3, f4)

solution = fsolve(f, (guess_massflow, guess_Pb, guess_Pc, guess_Ps), full_output = True)

我尝试使用Mathcad解决此问题。收敛到解决方案,这很现实:

(array([  3.00000000e-01,   1.10000000e+06,   1.10000000e+06,
     1.10000000e+06]), {'nfev': 19, 'fjac': array([[ nan,  nan,  nan,  nan],
   [ nan,  nan,  nan,  nan],
   [ nan,  nan,  nan,  nan],
   [ nan,  nan,  nan,  nan]]), 'r': array([ nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan]), 'qtf': array([ nan,  nan,  nan,  nan]), 'fvec': array([-0.65463805,  0.3       ,  0.3       , -3.45205928])}, 5, 'The iteration is not making good progress, as measured by the \n  improvement from the last ten iterations.')

1 个答案:

答案 0 :(得分:0)

我认为问题出在求解器使用main_mod(P1, P2)来调用P1>P2时,因此在这种情况下我强制该函数返回零...我不知道它在物理上是否正确。 ..但它似乎可行:

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

#Constants
Cn = 1.0
Cf = 0.8
Nn = 3.0
gamma = 1.4
R = 287.1 # J/kg/K  
dn = 0.5 * 25.4 # mm
df = 2.1516 * 25.4 # mm
dv = 1.8553 * 25.4 # mm
dbt = 0.89 * 25.4 # mm
Tb = (40 + 273.15) # K
Pr = (13 + 1.01325) * 1e5 # N/m2

def pi_mod(d):
    return (math.pi * d ** 2) / 4e6  # <- mm2 to m2?

radical = (gamma + 1)/(gamma - 1)
radical2 = (gamma - 1) / gamma
minor_mod = np.sqrt( gamma/R*( 2/(gamma + 1) )**radical )

def main_mod(P1, P2):
    if P1 < P2:
        return P2 * np.sqrt( 1/R/Tb * 2/radical2 * (P1/P2)**(2/gamma) * (1 - (P1/P2)**radical2) )
    else:
        return 0

def f(p):
    massflow, Pb, Pc, Ps = p

    major_mod = Nn*Cn/np.sqrt(Tb) * Pb 

    f1 = massflow - major_mod * pi_mod(dn) * minor_mod
    f2 = massflow - Cf * pi_mod(df) * main_mod(Pb, Pc)
    f3 = massflow - Cf * pi_mod(dbt) * main_mod(Pc, Ps)
    f4 = massflow - Cf * pi_mod(dv) * main_mod(Ps, Pr)

    return (f1, f2, f3, f4)

guess_massflow = 0.3 # kg/s
guess_Pc = 12e5 # N/m2
guess_Pb = 10e5 # N/m2
guess_Ps = 14e5 # N/m2

p_zero = (guess_massflow, guess_Pb, guess_Pc, guess_Ps)

solution = fsolve(f, p_zero, full_output=True)

我还更改了pi_mod中的单位转换(这是磁盘的面积吗?),并将常量值和函数放在f函数之外