Skip to content
DeepForgeby svx
All problems
HardCalculusca-088

Lagrange Multiplier on a Circle

Problem

Maximize f(x, y) = a*x + b*y on the circle x^2 + y^2 = r^2.

The Lagrange condition makes the gradient (a, b) parallel to (x, y), so the maximizer is (r*a, r*b) / sqrt(a^2 + b^2) and the maximum value is r * sqrt(a^2 + b^2). Return [x, y, max]. If a = b = 0, return [0.0, 0.0, 0.0].

Hint

The maximum of a linear function on a circle is the radius times the gradient norm.

Test cases

case 1
in: [3,4,1]
expected: [0.6,0.8,5]
case 2
in: [1,0,2]
expected: [2,0,2]
case 3
in: [0,1,3]
expected: [0,3,3]
case 4
in: [1,1,2]
expected: [1.414213562373095,1.414213562373095,2.8284271247461903]
case 5
in: [0,0,5]
expected: [0,0,0]
Starter codePython
def lagrange_circle(a, b, r):
    # Return [x, y, max_value]
    # Your code here
    pass

5 test cases · run your code against them in the editor