我想使用scipy中的root
解决一个非线性系统。我用循环生成了方程。每次求解器迭代查找解决方案时,所有生成方程式的循环都会再次运行,这会延长求解时间。我想创建一个仅生成一次方程式的代码,然后使用生成的方程式求解系统。
例如,以下循环在代码中。对于每次迭代,它都会再次生成方程式。我想一次生成方程式,然后用根来求解系统,而不是在求解器的每次迭代中都一遍又一遍地生成方程式。
def Colebrook(R,Re,d,f):
F = (-2.0*np.log10((R/(3.7*d) + (2.51/(Re*(np.sqrt(f)))))) - 1.0/(np.sqrt(f)))
return F
def ec(y):
if usar_darcy ==True:
for i in range(len(n)):
for a in range(len(n[i])):
for b in range(n[i][a]):
if y[Cont_col+Cont_cau]>=2000 and usar_darcy == True:
col = Colebrook(Rl,y[Cont_col+Cont_cau],Dl,
y[Cont_col + Cont_cau + Contador_re])
colebrook2.append(col)
if y[Cont_col+Cont_cau]<=2000 and usar_darcy == True:
col = Laminar2(y[Cont_col+Cont_cau],
y[Cont_col + Cont_cau + Contador_re])
colebrook2.append(col)
Cont_col = Cont_col + 1
return list_with_all_The_equations
def solver():
sol = root(ec, Guess,method='hybr', jac=False)
print(sol)
return sol
solver()