function [ t, y ] = mad_setup ( f_string, a, b, ya, tau0, reltol, abstol )

%*****************************************************************************80
%
%% mad_setup() sets up a call to mad_compute(), the adaptive midpoint solver.
%
%  Discussion:
%
%    This function can be used to set up a call to the adaptive implicit
%    midpoint method solver for ODEs.  It allows the user to input some or
%    all of the input arguments, problems for the missing arguments,
%    and then calls mad_compute.
%
%    The ODE right hand side function is specified as a character string
%    for the name of the corresponding M file.
%
%    The user may omit some, all, or none of the input arguments; however
%    MATLAB expects that if the user has omitted arguments, they are
%    at the end of the argument list.  
%
%    Thus, mad_setup() can handle calls like:
%      [ t, y ] = mad_setup ( 'lotka_deriv', a, b, ya, tau0, reltol, abstol )
%      [ t, y ] = mad_setup ( 'lotka_deriv', a, b, ya )
%      [ t, y ] = mad_setup ( )
%    but will NOT correctly process
%      [ t, y ] = mad_setup ( 'lotka_deriv', ya, tau0, reltol, abstol )
%    because the missing arguments a and b are followed by more information.
%
%    If no input arguments are supplied initially, the prompting process
%    looks like this:
%
%      [ t, y ] = mad_setup ( );
%      Enter function M-file name, such as lotka(t,y): lotka(t,y)
%      Enter starting point a: 0.0
%      Enter ending point b: 10.0
%      Enter the initial condition vector y(a), such as [1,2,3]: [4,2]
%      Enter the initial timestep: 1.e-3
%      Enter relative tolerance for LTE: 1.e-5
%      Enter absolute tolerance for LTE: 1.e-5
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    21 February 2022
%
%  Author:
%
%    John Burkardt, Catalin Trenchea
%
%  Reference:
%
%    Catalin Trenchea, John Burkardt,
%    Refactorization of the midpoint rule,
%    Applied Mathematics Letters,
%    Volume 107, September 2020.
%
%  Input:
%
%    character string f_string: the name of the M file that evaluates the
%    right hand side of the differential equation.  
%
%    real a, b: the starting and ending points.
%
%    real ya(m): the (row) vector of initial conditions at the starting time. 
%
%    real tau0: the initial timestep to take.
%
%    real reltol, abstol: the relative and absolute tolerances for 
%    the local truncation error.
%
%  Output:
%
%    real t(n+1), y(n+1,m): time and solution values after n steps.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'mad_setup():\n' );
  fprintf ( 1, '  Interactive call to mad_compute():\n' );
%
%  Get f, a, b, ya, tau0, reltol, abstol.
%
  if ( nargin < 1 )
    f_string = input ( 'Enter function M-file name in quotes, such as ''lotka_deriv'': ', 's' );
  end
  f_element = vector_to_element ( f_string );
  f = str2func ( [ '@(t,y)', f_element ] );

  if ( nargin < 2 )
    a = input ( 'Enter the starting point a: ' );
  elseif ( ischar ( a ) )
    a = str2num ( a );
  end

  if ( nargin < 3 )
    b = input ( 'Enter the ending point b: ' );
  elseif ( ischar ( b ) )
    b = str2num ( b );
  end

  if ( nargin < 4 )
    ya = input ( 'Enter the initial condition vector y(a), such as [1,2,3]: ' );
  elseif ( ischar ( ya ) )
    ya = str2num ( ya );
  end

  if ( nargin < 5 )
    tau0 = input ( 'Enter the initial timestep, tau0: ' );
  elseif ( ischar ( tau0 ) )
    tau0 = str2num ( tau0 );
  end

  if ( nargin < 6 )
    reltol = input ( 'Enter the relative tolerance for LTE, reltol: ' );
  elseif ( ischar ( reltol ) )
    reltol = str2num ( reltol );
  end
  
  if ( nargin < 7 )
    abstol = input ( 'Enter the absolute tolerance for LTE, abstol: ' );
  elseif ( ischar ( absTol ) )
    abstol = str2num ( absTol );
  end
  
  [ t, y ] = mad_compute ( f, a, b, ya, tau0, reltol, abstol );

  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
