Gradient Descent on a Sinusoidal Objective
Problem statement
Use gradient descent to seek a minimum of f(x) = x*x - 2*x + 1 + 3*sin(pi*x), where pi is π and trigonometric arguments are in radians.
Start at x = initialX and perform exactly iterations updates with the constant learningRate. Each update uses the derivative at the current x:
x = x - learningRate * (2*x - 2 + 3*pi*cos(pi*x)).
Return [x, f(x)] after the last update. With zero iterations, return the initial x and its objective value. Use double precision; an absolute or relative error of 1e-6 is accepted for each returned number. A sufficiently accurate numerical derivative is also allowed.
For this exercise, the starting interval and step-size bounds keep the updates in a stable convex region. A finite update budget returns an estimate; do not replace the requested updates with an unrelated optimizer or a hardcoded minimizer.
Function
gradientDescent(initialX: double, learningRate: double, iterations: int) → double[]Examples
Example 1
initialX = 1.5learningRate = 0.01iterations = 0return = [1.5,-2.75]No update is made: x=1.5 and f(x)=-2.75.
Example 2
initialX = 1.5learningRate = 0.01iterations = 1return = [1.49,-2.7584196810971946]The initial derivative is 1, so the next x is 1.49. Evaluate the original objective at that updated x.
Example 3
initialX = 1.5learningRate = 0.01iterations = 100return = [1.4683142604203359,-2.7658306044951817]Apply 100 gradient updates. The result is an estimate from this finite budget, rather than a general global-minimum guarantee.
Constraints
1.0 <= initialX <= 2.0.0.000001 <= learningRate <= 0.03.0 <= iterations <= 10000.- The inputs are finite numbers. All trigonometric calculations use radians.