P8.4.1A simple constrained minimization problem
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.
Show solution
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:
constrainedminimization
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)
Output:
message: Optimization terminated successfully
success: True
status: 0
fun: 282.84271247440637
x: [ 1.414e+02 7.071e+01]
nit: 11
jac: [ 1.000e+00 2.000e+00]
nfev: 33
njev: 11
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