lab10-extra¶
New topic: optimisation (optimal_can)
optimal_can(volume=330)¶
Minimise can surface area for fixed volume.
A cylindrical drinks can has radius \(r\) and height \(h\). Its volume is
and its total surface area (top + bottom + side wall) is
Write two functions:
cylinder_area_from_radius_and_volume(radius, volume)optimal_can(volume)
For optimal_can(volume), return (diameter, height) for the
minimum-area can with that fixed volume volume.
If no volume is provided, assume the default value of 330.
Use scipy.optimize.fmin to solve the optimisation.
Hints:
In
cylinder_area_from_radius_and_volume, use the volume relation to eliminateh:\[h = \frac{V}{\pi r^2}\]and substitute into
\[A = 2\pi rh + 2\pi r^2\]so the area A depends only on
rfor a given and fixedV.In
optimal_can, minimise this one-variable area functioncylinder_area_from_radius_and_volumewithfmin. In other words, usingfmin, find the valuerfor the radius, so that the areaAis minimal.fminreturns a numpy array (of length 1); extract the optimal radius from it.The
fminfunction takes on optional keyword argument with nameargs. This is a list of tuples which will be passed onto the objective function. You need to use it, to pass the volume tocylinder_area_from_radius_and_volume. For example:args = (volume, ) # needs to be a tuple, even if it contains only one argument fmin(cylinder_area_from_radius_and_volume, r_initial, args=args)
To convert the optimum radius \(r\) into height and diametre: \(h = V / (\pi r^2)\) and \(d = 2r\).
Examples
In [ ] : cylinder_area_from_radius_and_volume(1, 10)
Out[27]: 26.283185307179586
In [ ] : cylinder_area_from_radius_and_volume(2, 10)
Out[28]: 35.132741228718345
For this example we express the distances – such as the radius,
diameter (\(d = 2r\)) and height – in centimetres (cm), and area in square cm and
volumina in cubic cm. For 330 cm^3 and a call of optimal_can(330), both returned values for height and diametre should be
close to about 7.5 (which then means 7.5 cm).
Voluntary follow up question (nothing to submit here): the standard 330cl drink can (for example from this provider) is slimmer: diametre is smaller than 7cm and height is above 11cm. Can you think why that may be the case? In addition to any PR-stunts, there is an engineering/economy reason.
using_quad()¶
Numerical integration
Write a function using_quad() that computes and returns the integral
\(\int\limits_a^b x^2 f(x) \mathrm{d}x\) with \(f(x) = x^2\)
from \(a=0\) to \(b=2\) using scipy.integrate.quad. The
function using_quad should return the value that the quad()
function returns, i.e. a tuple (y, abserr) where y is
quad’s best approximation of the true integral, and abserr an
absolute error estimate.
You should find that this method (with the default settings) is (far) more accurate than our trapez function.
Please include the extra tasks in your file lab10.py
and submit as lab10 assignment.
Back to lab10.
End of lab10-extra.