TypeError: Must be real number not GKvariable

Viewed 120

I get an error when running the following code:

  from gekko import GEKKO
  m= GEKKO()
  sigma = m.Var()
  m.Equation((0.5*1.225*(Vt**2)*s*b)*(pb*(beta-sigma) \
             + Cndr*((((Vw**2)*Ss*CDy)/((Vt**2)*s) \
             - Cyb*(beta-sigma))/Cydr)) + Fw*dc*math.cos(sigma)==0)
  m.solve(disp=False)
  print(sigma.value)

In the above code, I want to find the value of the variable sigma. All other variables are constants that have been defined. While executing the above code, I got the following error.

m.Equation((0.5*1.225*(Vt**2)*s*b)*(x[10]*(beta-sigma) 
           + Cndr*((((Vw**2)*Ss*CDy)/((Vt**2)*s)
           - Cyb*(beta-sigma))/Cydr)) + Fw*dc*math.cos(sigma)==0)
TypeError: Must be a real number, not GKvariable

I think the issue is with math.cos(sigma), because when I removed it - it worked.

How do I solve this error?

1 Answers

You can't use math.cos from the standard library, because as it says it wants an actual number (like float or int). GEKKO has its own functions that can handle GKvariables. You should use m.cos instead of math.cos

https://gekko.readthedocs.io/en/latest/model_methods.html#equation-functions

Special function besides algebraic operators are available through GEKKO functions. These must be used (not numpy or other equivalent functions):

Related