lab10-extra

New topic: optimisation (optimal_can)

optimal_can(volume=330)

Minimise can surface area for fixed volume.

https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Can_of_Sangaria_Quality_Coffee_Black.png/960px-Can_of_Sangaria_Quality_Coffee_Black.png?20220227155458

A cylindrical drinks can has radius \(r\) and height \(h\). Its volume is

\[V = V(r, h) = \pi r^2 h\]

and its total surface area (top + bottom + side wall) is

\[A = A(r, h) = 2\pi rh + 2\pi r^2.\]

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 eliminate h:

    \[h = \frac{V}{\pi r^2}\]

    and substitute into

    \[A = 2\pi rh + 2\pi r^2\]

    so the area A depends only on r for a given and fixed V.

  • In optimal_can, minimise this one-variable area function cylinder_area_from_radius_and_volume with fmin. In other words, using fmin, find the value r for the radius, so that the area A is minimal.

  • fmin returns a numpy array (of length 1); extract the optimal radius from it.

  • The fmin function takes on optional keyword argument with name args. This is a list of tuples which will be passed onto the objective function. You need to use it, to pass the volume to cylinder_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.