function [ a, fa, it ] = nonlin_newton ( f_string, fp_string, a )

%*****************************************************************************80
%
%% nonlin_newton is an interactive newton method program.
%
%  Discussion:
%
%    This function manages an interactive computation which uses Newton's
%    method to seek a zero of a function f(x), given a starting value.
%
%    The user enters formulas for f(x), f'(x), and the starting value a.
%
%    The program can be invoked by a function call, in which case the
%    string specifying f(x) must be quoted:
%      [ a, fa, it ] = nonlin_newton ( 'cos(x)-x', '-sin(x)-1', 0 )
%    or as an interactive command with arguments:
%      [ a, fa, it ] = nonlin_newton cos(x)-x -sin(x)-1 0
%    If called with no arguments, these are requested:
%      [ a, fa, it ] = nonlin_newton ( );
%      Enter function formula, like x^2: cos(x)-x
%      Enter derivative formula, like 2*x: -sin(x)-1
%      Enter starting point, a: 0
%
%    The function is specified as a string which is either:
%    * a MATLAB expression using the argument 'x', 
%    * the name of an M-file followed by the argument '(x)'.
%
%    The string should not contain any spaces between symbols, except
%    when it is passed as a function argument in quotes.
%
%    It is not necessary to use the "dot" notation for expressions involving
%    '*', '/', or '^', but it doesn't hurt either.
%
%    Examples of function specifications:
%
%      x^2
%      x.^2
%      3/(x^4+5*x-6)
%      sin(7*x)*sqrt(x)/8
%      wiggle(x)     <-- where "wiggle.m" is a user-provided M file.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    25 July 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string f_string: the function whose zero is desired.
%
%    string fp_string: the derivative of the function.
%
%    real a: the starting point.
%
%  Output:
%
%    real a: the current estimate for the zero.
%
%    real fa: f(a).
%
%    integer it: the number of steps taken.
%

%
%  Get F, FP, A.
%
  if ( nargin < 1 )
    f_string = input ( 'Enter function formula, like x^2: ', 's' );
  end
  f_element = vector_to_element ( f_string );
  f = str2func ( [ '@(x)', f_element ] );

  if ( nargin < 2 )
    fp_string = input ( 'Enter derivative formula, like 2*x: ', 's' );
  end
  fp_element = vector_to_element ( fp_string );
  fp = str2func ( [ '@(x)', fp_element ] );

  if ( nargin < 3 )
    a = input ( 'Enter starting point a: ' );
  elseif ( ischar ( a ) )
    a = str2num ( a );
  end
%
%  Carry out newton method.
%
  tol = 10.0 * eps * max ( abs ( a ), 1.0 );
  [ a, it, status ] = newton ( a, tol, f, fp );
  fa = f(a);

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Tolerance = %g\n', tol );
  fprintf ( 1, '  Number of steps = %d\n', it );
  fprintf ( 1, '  a = %24.16g, f(a) = %g\n', a, fa );
  fprintf ( 1, '  status = "%s"\n', status );

  return
end
function [ a, it, status ] = newton ( a, tol, f, fp )

%*****************************************************************************80
%
%% newton carries out the newton method.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    07 July 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real A, the starting point.
%
%    real TOL, a tolerance. 
%
%    function handle F, FP, pointers to a function and its derivative.  
%    These can be anonymous functions or the names of a MATLAB M-file function.
%
%  Output:
%
%    real A, the last iterate.
%
%    integer IT, the number of steps.
%
%    string STATUS, a message indicating why the procedure stopped.
%
  fa = f ( a );
  fpa = fp ( a );
  big = 100.0 * abs ( fa );
  small = 1.0E-08;
  it = 0;

  while ( true )

    if ( 50 <= it )
      status = 'Failure: too many steps!';
      return
    end

    if ( abs ( fpa ) <= small )
      status = 'Divergence: derivative value too small!';
      return
    end

    b = a - fa / fpa;
    fb = f(b);
    it = it + 1;
    a = b;
    fa = fb;
    fpa = fp ( a );

    if ( big <= abs ( fa ) )
      status = 'Divergence: function value grew too large!';
      return
    end

    if ( abs ( fa ) <= tol )
      status = 'Convergence: very small function value!';
      return
    end

  end

  return
end
function f_element = vector_to_element ( f_vector )

%*****************************************************************************80
%
%% vector_to_element rewrites a MATLAB vector function elementwise.
%
%  Discussion:
%
%    In MATLAB, by default, the operators '*', '/', and '^' are interpreted
%    as linear algebraic operations.  In many cases, a user simply desired
%    the elementwise operation instead, which MATLAB indicates using a 
%    dotted notation: '.*', './', and '.^'.
%
%    This function converts a string to a form that uses dotted operators.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 June 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string f_vector, a string presumably representing a MATLAB expression.
%
%  Output:
%
%    string f_element, a copy of f_vector in which all operators have been
%    replaced by their dotted (elementwise) form.
%
  n_vector = length ( f_vector );
  f_element = [''];

  c_prev = '';
  for iv = 1 : n_vector
    c = f_vector(iv);
    if ( ( c == '*' || c == '/' || c == '^' ) && ( c_prev ~= '.' ) )
      f_element = [ f_element, ' .', c, ' ' ];
    else
      f_element = [ f_element, c ];
    end
    c_prev = c;
  end

  return
end

