'I try to use Gekko to find Q, R1 and P1. Why do I report an error

import math
from gekko import GEKKO

m = GEKKO()
Q = m.Var(value=0)  # 给定初值为0
P1 = m.Var(value=0)  # 给定初值为0
R1 = m.Var(value=0)  # 给定初值为0

m.Equations([3.59067 * ((0.119215 * Q) ** 0.7215007215) * ((200 ** 0.278499278499278499) - R1)+P1 == 35,  # 方程1
             15.782 * Q * math.log(10*R1) + 0.051 * (R1 - 0.1)-P1+10 == 0,  # 方程2
             110410.872 * Q / R1 == 1715.2873809532941])

m.solve(disp=False)
Q, P1, R1 = Q.value, P1.value, R1.vaue
print(Q, P1, R1)

Result: Traceback (most recent call last): File "C:\Users\Administrator\Desktop\数模竞赛\1.py", line 11, in 15.782 * Q * math.log(10*R1) + 0.051 * (R1 - 0.1)-P1+10 == 0, # 方程2 TypeError: must be real number, not GK_Operators



Solution 1:[1]

When using Gekko, make sure you use Gekko functions. Rather than call math.log(), call m.log(). Additionally, the initial guess value for your variables are all 0, and this will cause an undefined value in your third equation (1/0), and will not help. Try changing the initial values to 1 instead. Here is a working version of your code.

import math
from gekko import GEKKO

m = GEKKO()
Q = m.Var(value=1)  # ?????0
P1 = m.Var(value=1)  # ?????0
R1 = m.Var(value=1)  # ?????0

m.Equations([3.59067 * ((0.119215 * Q) ** 0.7215007215) * ((200 ** 0.278499278499278499) - R1)+P1 == 35,  # ??1
             15.782 * Q * m.log(10*R1) + 0.051 * (R1 - 0.1)-P1+10 == 0,  # ??2
             110410.872 * Q / R1 == 1715.2873809532941])

m.solve(disp=False)
print(Q.value,P1.value,R1.value)

Solution 2:[2]

LaGrande has a correct solution. Here is another solution with the translated comments and reformulated equations to help with the solution. An initial guess of zero can be used as long as there are lower bounds on the variables.

from gekko import GEKKO

m = GEKKO()
Q = m.Var(value=0,lb=0)  # The given initial value is 0
P1 = m.Var(value=0,lb=0)  # The given initial value is 0
R1 = m.Var(value=0,lb=0)  # The given initial value is 0

m.Equations([3.59067 * ((0.119215 * Q) ** 0.7215007215) * \
             ((200 ** 0.278499278499278499) - R1)+P1 == 35,  # Equation 1
             15.782 * Q * m.log(10*R1) \
             + 0.051 * (R1 - 0.1)-P1+10 == 0,  # Equation 2
             110410.872 * Q == R1*1715.2873809532941])

m.solve(disp=True)
Q, P1, R1 = Q.value[0], P1.value[0], R1.value[0]
print(Q, P1, R1)
EXIT: Optimal Solution Found.
 
 The solution was found.
 
 The final value of the objective function is   0.000000000000000E+000
 
 ---------------------------------------------------
 Solver         :  IPOPT (v3.12)
 Solution time  :   8.700000002136221E-003 sec
 Objective      :   0.000000000000000E+000
 Successful solution
 ---------------------------------------------------
 
0.35771948494 41.876421839 23.025949297

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LaGrande Gunnell
Solution 2 John Hedengren