Solving for extrema in trigonometric functions