function [ value, nf, qa, qb, qc ] = flin ( n, jsearch, l, f, x, nf, v, ...
  q0, q1, qd0, qd1, qa, qb, qc )

%*****************************************************************************80
%
%% flin() is the function of one variable to be minimized by MINNY.
%
%  Discussion:
%
%    F(X) is a scalar function of a vector argument X.
%
%    A minimizer of F(X) is sought along a line or parabola.
%
%    This function has been modified, by removing the occurrence of a
%    common block, so that it looks more like a "normal" function that does
%    not rely so strongly on peculiarities of FORTRAN.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    31 July 2016
%
%  Author:
%
%    Original FORTRAN77 version by Richard Brent.
%    MATLAB version by John Burkardt.
%
%  Reference:
%
%    Richard Brent,
%    Algorithms for Minimization with Derivatives,
%    Prentice Hall, 1973,
%    Reprinted by Dover, 2002.
%
%  Input:
%
%    integer N, the number of variables.
%
%    integer JSEARCH, indicates the kind of search.
%    If J is a legal column index, linear search in direction of V(*,JSEARCH).
%    Otherwise, then the search is parabolic, based on X, Q0 and Q1.
%
%    real L, is the parameter determining the particular
%    point at which F is to be evaluated.
%    For a linear search, L is the step size.
%    For a quadratic search, L is a parameter which specifies
%    a point in the plane of X, Q0 and Q1.
%
%    real F ( X, N ), the function to be minimized.
%
%    real X(N), the base point of the search.
%
%    integer NF, the function evaluation counter.
%
%    real V(N,N), a matrix whose columns constitute
%    search directions.
%
%    real Q0(N), Q1(N), two auxiliary points used to
%    determine the plane when a quadratic search is performed.
%
%    real QD0, QD1, values needed to compute the
%    coefficients QA, QB, QC.
%
%    real QA, QB, QC, coefficients used to combine
%    Q0, X, and A1 if a quadratic search is used.  (Yes, technically
%    these are input quantities as well.)
%
%  Output:
%
%    real VALUE, the value of the function at the
%    minimizing point.
%
%    integer NF, the function evaluation counter.
%
%    real QA, QB, QC, coefficients used to combine
%    Q0, X, and A1 if a quadratic search is used.
%
  q0 = q0(:);
  q1 = q1(:);
  x = x(:);
  t = zeros ( n, 1 );
%
%  The search is linear.
%
  if ( 1 <= jsearch )

    t(1:n) = x(1:n) + l * v(1:n,jsearch);
%
%  The search is along a parabolic space curve.
%
  else

    qa =                 l * ( l - qd1 ) /       ( qd0 + qd1 ) / qd0;
    qb = - ( l + qd0 ) *     ( l - qd1 ) / qd1                 / qd0;
    qc =   ( l + qd0 ) * l               / qd1 / ( qd0 + qd1 );

    t(1:n) = qa * q0(1:n) + qb * x(1:n) + qc * q1(1:n);

  end
%
%  The function evaluation counter NF is incremented.
%
  nf = nf + 1;
%
%  Evaluate the function.
%
  value = f ( t, n );

  return
end
