function value = dot_l2 ( f_string, g_string, a, b )

%*****************************************************************************80
%
%% dot_l2 estimates the L2 dot product of two functions.
%
%  Discussion:
%
%    dot_l2(f,g,a,b) = integral ( a <= x <= b ) f(x) g(x) dx
%
%    The value is estimated by calling the MATLAB function integral().
%
%    The user enters formulas for f(x) and g(x), and the values of a and b.
%
%    The program can be invoked by a function call, in which case the
%    strings specifying f(x) and g(x) must be quoted:
%      value = dot_l2 ( 'x^2', 'sin(x)', -1, 3 );
%    or as an interactive command with arguments:
%      value = dot_l2 x^2 sin(x) -1 3
%    or, if called with no arguments, it will request them:
%      value = dot_l2 ( );
%      Enter formula for f(x), like x^2: x^2
%      Enter formula for g(x), like x^2: sin(x)
%      Enter left limit, a: -1
%      Enter right limit, b: 3
%
%    Each 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:
%
%    28 July 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string f_string, g_string: the functions, using 'x' as the variable.
%
%    real a, b: the integration limits.
%
%  Output:
%
%    real value: the estimated L2 dot product.
%

%
%  Get F, G, A, B.
%
  if ( nargin < 1 )
    f_string = input ( 'Enter formula for f(x), like x^2: ', 's' );
  end
  f_element = vector_to_element ( f_string );

  if ( nargin < 2 )
    g_string = input ( 'Enter formula for g(x), like x^2: ', 's' );
  end
  g_element = vector_to_element ( g_string );

  if ( nargin < 3 )
    a = input ( 'Enter left limit a: ' );
  elseif ( ischar ( a ) )
    a = str2num ( a );
  end

  if ( nargin < 4 )
    b = input ( 'Enter right limit b: ' );
  elseif ( ischar ( b ) )
    b = str2num ( b );
  end

  fg_string = [ '@(x)', '( ', f_element, ' ).*(', g_element, ')' ];

  fg = str2func ( fg_string );

  value = integral ( fg, a, b, 'AbsTol', 1.0e-14, 'RelTol', 1.0e-12 );

  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

