44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
'''
|
||
You must manufacture a closed cylindrical can (top and bottom included). You have a fixed amount of material so the total surface area 𝐴 of the can is fixed.
|
||
Find the radius 𝑟 and height ℎ of the cylinder that maximize the enclosed volume 𝑉. What is the relation between ℎ and 𝑟 at the optimum? What is the maximal
|
||
volume 𝑉 sub(max) expressed in terms of the fixed surface area 𝐴?
|
||
'''
|
||
|
||
import numpy as np
|
||
from scipy.optimize import minimize
|
||
|
||
# Fixed surface area
|
||
A_fixed = 2000 # You can change this value
|
||
|
||
# Volume function to maximize (we'll minimize the negative)
|
||
def volume(params):
|
||
r, h = params
|
||
return -np.pi * r**2 * h # Negative for maximization
|
||
|
||
# Constraint: surface area must equal A_fixed
|
||
def surface_area_constraint(params):
|
||
r, h = params
|
||
return 2 * np.pi * r**2 + 2 * np.pi * r * h - A_fixed
|
||
|
||
# Initial guess
|
||
initial_guess = [1.0, 1.0]
|
||
|
||
# Bounds: radius and height must be positive
|
||
bounds = [(0.0001, None), (0.0001, None)]
|
||
|
||
# Define constraint dictionary
|
||
constraints = {'type': 'eq', 'fun': surface_area_constraint}
|
||
|
||
# Run optimization
|
||
result = minimize(volume, initial_guess, bounds=bounds, constraints=constraints)
|
||
|
||
# Extract optimal values
|
||
if result.success:
|
||
r_opt, h_opt = result.x
|
||
V_max = np.pi * r_opt**2 * h_opt
|
||
print(f"Optimal radius: {r_opt:.4f}")
|
||
print(f"Optimal height: {h_opt:.4f}")
|
||
print(f"Maximum volume: {V_max:.4f}")
|
||
else:
|
||
print("Optimization failed:", result.message)
|