为了计算电线,我在计算同时连接的电路时遇到了从最小功能中作出不正确反应的问题。 例如:
Creating a matrix of connections and fill in the resistance matrix: Equation of the form: A*x = V ,where function sum(F * x**3) -> minimum
import numpy as np
from scipy.optimize import minimize, LinearConstraint
def CreateArrays(A,V):
A = np.array(A) #matrix of connections
V = np.array(V)
buf = [0.1 for i in range(len(A[0]))]
x0 = np.array(buf)
return A,V,x0
def start(A,V,F):
def obj_func(x):
return np.sum(F * x**3)
A,V,x0 = CreateArrays(A,V)
lincon = LinearConstraint(A, lb=V, ub=V)
res = minimize(obj_func, x0, method= trust-constr , constraints=lincon)
return res.x
#Matrix of connections, vector V, vector F
I = start([[1, 0, 1], [0, -1, -1], [-1, 1, 0]],
[100, -100, 0],[1e-4, 1e-4, 8e-4])
print(I)
The result is [33.36666667 33.36666667 66.63333333] but it s a mistake; the true result is [66.63333333, 66.63333333, 33.36666667], where value of function is minimum. The higher the resistance, the lower the electric currents.
我发现这一法典最低:
f = [1e-4,1e-4,8e-4]
s=0
save = 0.5
x = [0.5,0.5,99.5]
for i in range(len(x)):
s = s + f[i] * x[i]**3
mini = s #start minimum
print(mini)
j = 0.5
while j < 100:
x = [j, j, 100-j]
s=0
for i in range(len(x)):
s = s + f[i] * x[i]**3
if s < mini:
mini = s
save = x
j =j + 0.5
print(mini,save)