function errors_test22 ( )

%*****************************************************************************80
%
%% errors_test22()
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    21 August 2017
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, '+=============================================+\n' );
  fprintf ( 1, '|                                             |\n' );
  fprintf ( 1, '|  Exercise 22                                |\n' );
  fprintf ( 1, '|                                             |\n' );
  fprintf ( 1, '|  Define F(X,N) to be the difference between |\n' );
  fprintf ( 1, '|  EXP(X) and its Taylor series, multiplied   |\n' );
  fprintf ( 1, '|  by N!.                                     |\n' );
  fprintf ( 1, '|                                             |\n' );
  fprintf ( 1, '|  Formula 1:                                 |\n' );
  fprintf ( 1, '|                                             |\n' );
  fprintf ( 1, '|    F(X,N) = N! *                            |\n' );
  fprintf ( 1, '|      ( EXP(X) - (1+X+X^2/2+...+X^N/N!) )    |\n' );
  fprintf ( 1, '|                                             |\n' );
  fprintf ( 1, '|  But we can also determine a second         |\n' );
  fprintf ( 1, '|  definition, namely:                        |\n' );
  fprintf ( 1, '|                                             |\n' );
  fprintf ( 1, '|  Formula 2:                                 |\n' );
  fprintf ( 1, '|                                             |\n' );
  fprintf ( 1, '|    F(X,N) = N*F(X,N-1) - X^N                |\n' );
  fprintf ( 1, '|                                             |\n' );
  fprintf ( 1, '|  Compare formula 1 with formula 2 at X = 1  |\n' );
  fprintf ( 1, '|                                             |\n' );
  fprintf ( 1, '+=============================================+\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '    N  Formula 1  Formula 2  x^n/n\n' );
  fprintf ( 1, '\n' );

  x = 1.0;
  facn = 1.0;
  sum2 = 1.0;
  fx2old = exp ( x ) - 1.0;

  for n = 0 : 20

    fx1 = facn * ( exp ( x ) - sum2 );

    if ( n == 0 )
      fx2 = fx1;
      fx3 = 0.0;
    else
      fx2 = n * fx2old - x ^ n;
      fx3 =  x ^ n / n;
    end

    fx2old = fx2;
    facn = facn * ( n + 1 );
    sum2 = sum2 + x ^ ( n + 1 ) / facn;

    fprintf ( 1, '  %4d  %20.16g  %20.16g  %20.16g\n', n, fx1, fx2, fx3 );

  end

  return
end
