function h = tank4 ( v )

%% TANK4 seeks the height H which corresponds to a given volume V.

  r = 4.0;

  h_min = 0.0;
  v_min = 0.0;

  h_max = 2.0 * r;
  v_max = pi * r^3 * 4.0 / 3.0;

  if ( v < v_min | v_max < v )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'TANK4 - Fatal error!\n' );
    fprintf ( 1, '  There is no solution H for this value of V!\n' );
    error ( 'TANK4 - Fatal error!' )
  end
%
%  Keep splitting the interval until the two V's are close.
%
  while ( 0.01 < abs ( v_max - v_min ) )

    h_mid = ( h_min + h_max ) / 2.0;
    v_mid = tank_volume ( r, h_mid );

    fprintf ( 1, '\n' );
    fprintf ( 1, '  %14g  %14g  %14g\n', h_min, h_mid, h_max );
    fprintf ( 1, '  %14g  %14g  %14g\n', v_min, v_mid, v_max );

    if ( v_mid < v )
      h_min = h_mid;
      v_min = v_mid;
    else
      h_max = h_mid;
      v_max = v_mid;
    end

  end
 
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Solution H is between %g and %g\n', h_min, h_max );
  fprintf ( 1, '  %g <= %g <= %g\n', v_min, v, v_max );

  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
