2
votes

I have an error in a Python Gekko program that says there is a problem with an equation. I know that there are two solutions to this problem where the unit circle intersects the line.

enter image description here

from gekko import GEKKO
m = GEKKO()
x = m.Var()
y = m.Var()
m.Equation(x**2+y**2=1)
m.Equation(x=y)
m.solve()

When I put the equations together, it gives a different error SyntaxError: invalid syntax.

from gekko import GEKKO
m = GEKKO()
x = m.Var()
y = m.Var()
m.Equations([x**2+y**2=1,x=y])
m.solve()

I can get a solution by including the equations as an objective function but the solver IPOPT reports x=0, y=0 if I give that initial guess. When I guess x=1, y=1 it gives one of the correct solutions as x=0.707, y=0.707. I would like to have the solver enforce those as hard (not soft) constraints.

m.Obj((x**2+y**2-1)**2)
m.Obj((x-y)**2)

What can I do to solve this problem with Python Gekko?

1
Surely you want to use == everywhere? - Davis Herring

1 Answers

1
votes

Use == in your equations as David mentioned.

from gekko import GEKKO
m = GEKKO()
x = m.Var()
y = m.Var()
m.Equation(x**2+y**2==1)
m.Equation(x==y)
m.solve()

A common error when writing equations is to use a single equal sign (=) instead of a double equal sign (==). The (=) assigns the quantity on the right to the variable on the left. The (==) is a comparison operator and is used in Gekko to build equality constraints. You can also use other operators such as (<=), (<), (>), and (>=). The not equal to (!=) is not supported in Gekko.

One other thing to consider for your problem is that you are starting from x=0 and y=0 when you don't give a starting guess with x=m.Var() and y=m.Var(). You can find one solution or the other by starting closer to the solution such as x=m.Var(1) and y=m.Var(1) or add constraints to limit the search space as x=m.Var(lb=0) and y=m.Var(lb=0).