Open aaronjohnsabu1999 opened 1 year ago
try using Taylor series: https://math.stackexchange.com/questions/1459045/how-do-you-write-the-taylor-series-for-arctanx2
The atan2()
function uses conditional statements to return the correct value based on the sign of x
and y
:
def calculate_atan2(x, y):
# Check if x is positive or negative
if x > 0:
return math.atan(y / x)
# Check if x is negative and y is non-negative
elif x < 0 and y >= 0:
return math.atan(y / x) + math.pi
# Check if x is negative and y is negative
elif x < 0 and y < 0:
return math.atan(y / x) - math.pi
# Check if x is zero and y is positive
elif x == 0 and y > 0:
return math.pi / 2
# Check if x is zero and y is negative
elif x == 0 and y < 0:
return -math.pi / 2
# Handle the case where x is zero and y is also zero (undefined)
else:
raise ValueError("atan2 is undefined for (0, 0)")
# Example usage:
x = 3.0
y = 4.0
angle = calculate_atan2(x, y)
print(f"The angle for ({x}, {y}) is {angle} radians.")
This can be implemented with two sign3()
functions or if3()
functions to determine the sign of x
and y
and return the correct answer. This is required in Gekko instead of the if
python statements because Gekko needs continuous 1st and 2nd derivatives for the gradient-based solvers. I've added this as a requested feature for a future release of gekko
.
@APMonitor thanks! Will it cause continuity issues if if2
is used instead of if3
?
No problems with if2()
, but it is generally less reliable as an MPCC form especially when the solution is at the switch point. It does tend to solve faster, however, when it works.
Is there an existing function that implements the atan2 function in Gekko? Is it in the process of being implemented or is there an equivalent technique in implementing it using the existing functions in Gekko?