function tank2 ( )

%% TANK2 makes the tank plot using a MATLAB function.

  r = 4.0;
  h = linspace ( 0.0, 2.0 * r, 21 );
  for i = 1 : 21
    v(i) = tank_volume ( r, h(i) );
  end

  plot ( h, v, 'r.', 'Markersize', 25 );

  xlabel ( '<---Height--->' )
  ylabel ( '<---Volume--->' )
  title ( 'Volume as a function of height, spherical tank' )

  return
end
function v = tank_volume ( r, h )

  v_total = pi * r^3 * 4.0 / 3.0;

  if ( h <= 0.0 )
    v = 0.0;
  elseif ( h <= r )
    v = pi * ( h^2 * r - h^3 / 3 );
  elseif ( h <= 2.0 * r )
    h2 = 2.0 * r - h;
    v2 = pi * ( h2^2 * r - h2^3 / 3 );
    v = v_total - v2;
  else
    v = v_total;
  end

  return
end
