A rectangular field with area $A = 10000\;\mathrm{m^3}$ is to be fenced-off beside a straight river (the boundary with the river does not need to be fenced). What dimensions $a,b$ minimize the amount of fencing required? Verify that a constrained minimization algorithm gives the same answer as the algebraic analysis.
Solution P8.4.1
The amount of fencing required is $L = a+2b$ and the area is constrained to be $A=ab=10000$. The analytical answer is easily obtained by calculus to be $a = \sqrt{2A}, b = \sqrt{A/2}$. Using numerical minimization gives the same result:
from scipy.optimize import minimize
A = 10000
res = minimize(
lambda X: X[0] + 2 * X[1],
(100, 100),
method="slsqp",
constraints={"type": "eq", "fun": lambda X: A - X[0] * X[1]},
)
print(res)
The result is reported to be $a=141.4$ m and $b=70.71$ m. The exact result is:
In [x]: import numpy as np
In [x]: A = 10000
In [x]: print("a =", np.sqrt(2 * A))
a = 141.4213562373095
In [x]: print("b =", np.sqrt(A / 2))
b = 70.71067811865476