function tank3 ( )

%% TANK3 makes the tank plot using a vector function.

  r = 4.0;
  h = linspace ( 0.0, 2.0 * r, 21 );
  v = tank_volume ( r, h );

  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;

  i1 = find (                h < 0.0 );
  i2 = find (     0.0 <= h & h < r );
  i3 = find (       r <= h & h < 2.0 * r );
  i4 = find ( 2.0 * r <= h );

  v(i1) = 0.0;

  v(i2) = pi * ( h(i2).^2 * r - h(i2).^3 / 3 );

  h2(i3) = 2.0 * r - h(i3);
  v2(i3) = pi * ( h2(i3).^2 * r - h2(i3).^3 / 3 );
  v(i3) = v_total - v2(i3);

  v(i4) = v_total;

  return
end
