function [ a, b, fa, fb, it ] = nonlin_secant ( f_string, a, b )

%*****************************************************************************80
%
%% nonlin_secant is an interactive secant method program.
%
%  Discussion:
%
%    This function manages an interactive computation which uses the secant
%    method to seek a zero of a function f(x), given two starting values.
%
%    The user enters a formula for f(x), and the values of a and b.
%
%    The program can be invoked by a function call, in which case the
%    string specifying f(x) must be quoted:
%       [ a, b, fa, fb, it ] = nonlin_secant ( 'x^2', -1, 3 )
%    or as an interactive command with arguments:
%       [ a, b, fa, fb, it ] = nonlin_secant x^2 -1 3
%    If called with no arguments, these are requested:
%       [ a, b, fa, fb, it ] = nonlin_secant ( );
%      Enter function formula, like x^2: x^2
%      Enter first point, a: -1
%      Enter second point, b: 3
%
%    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 GNU LGPL license.
%
%  Modified:
%
%    29 June 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string f_string: the function whose zero is desired.
%
%    real a, b: two starting points.
%
%  Output:
%
%    real a, b: the most recent iterates.
%
%    real fa, fb: f(a) and f(b).
%
%    integer it: the number of iterations.
%

%
%  Get F, A, B.
%
  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 )
    a = input ( 'Enter first point a: ' );
  elseif ( ischar ( a ) )
    a = str2num ( a );
  end

  if ( nargin < 3 )
    b = input ( 'Enter second b: ' );
  elseif ( ischar ( b ) )
    b = str2num ( b );
  end
%
%  Carry out secant method.
%
  tol = 10.0 * eps * abs ( b - a );
  [ a, b, it, status ] = secant ( a, b, tol, f );
  fa = f(a);
  fb = f(b);

  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, '  b = %24.16g, f(b) = %g\n', b, fb );
  fprintf ( 1, '  status = "%s"\n', status );

  return
end
function [ a, b, it, status ] = secant ( a, b, tol, f )

%*****************************************************************************80
%
%% secant carries out the secant method.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    07 July 2019
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real A, B, two starting points.
%
%    Input, real TOL, a tolerance. 
%
%    Input, pointer value=F(x), a pointer to a function.  This can be
%    an anonymous function or the name of a MATLAB M-file function.
%
%    Output, real A, B, the last two iterates.  B is the most recent.
%
%    Output, integer IT, the number of steps.
%
%    Output, string STATUS, a message indicating why the procedure stopped.
%
  fa = f(a);
  fb = f(b);
  big = 100.0 * ( abs ( fa ) + abs ( fb ) );
  it = 0;

  while ( true )

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

    if ( fa - fb == 0.0 )
      status = 'Failure: fa = fb!'
      return
    end

    c = ( a * fb - b * fa ) / ( fb - fa );
    fc = f(c);
    it = it + 1;

    a = b;
    fa = fb;

    b = c;
    fb = fc;

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

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

    if ( sign ( fb ) ~= sign ( fa ) )
      if ( abs ( a - b ) <= tol )
        status = 'Convergence: very small change-of-sign interval!';
        return
      end
    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 GNU LGPL 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

