function collocation_test04 ( n )

%*****************************************************************************80
%
%% collocation_test04() collocates a second-order initial value problem.
%
%  Discussion:
%
%    A function y(x) is assumed to exist, and to be twice continuously
%    differentiable, over [-3.0,2.0], satisfying the following conditions:
%
%      y (-3.0) = a
%      y'(-3.0) = b
%      y''(x) - 6y'(x) + 13 y(x) = 0 for -3.0 < x < 2.0
%
%    We use collocation to construct a function g(x) such that
%
%      g(x(1)=-3.0) = a
%      g'(x(1)=-3.0) = b
%      g''(x(i)) - 6 g'(x(i))+13 g(x(i)) = 0 for x(2) through x(n-1)
%
%    exactly, at a set of N-1 collocation points between -3.0 and 2.0,
%    with X(1) = -3.0 and X(N) = 2.0, and assuming G(X) is a polynomial.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    01 April 2015
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'collocation_test04():\n' );
  fprintf ( 1, '  Collocate a second order initial value ODE.\n' );
  fprintf ( 1, '  Let N be the number of collocation points.\n' );
  fprintf ( 1, '  Choose N points X in [-3,2].\n' );
  fprintf ( 1, '  Let G(X) be a polynomial in X of degree N.\n' );
  fprintf ( 1, '    G(-3) = A\n' );
  fprintf ( 1, '    G''(-3) = B\n' );
  fprintf ( 1, '    G"(X(I)) - 6 G''(X(I) + 13 G(X(I)) = 0, I = 2...N\n' );
  fprintf ( 1, '  is N equations for the N unknown coefficients in G.\n' );
%
%  Set the collocation points.
%  For this case, we have 
%    N = 6 distinct collocation points
%    7 constraints
%    7 polynomial coefficients.
%
  n1 = 6;
  xl = -3.0;
  xr = 2.0;
  d = n1;
  x1 = linspace ( xl, xr, n1 );
  x1 = x1';
%
%  Choose alpha and beta.
%  e(x) = exp ( 3 x ) * ( alpha * cos ( 2x ) + beta * sin ( 2x ) );
%
  alpha = 1.0;
  beta = 2.0;
%
%  Set up the linear system for the coefficients.
%
  A = zeros ( n1+1, n1+1 );
  y1 = zeros ( n1 + 1, 1 );
%
%  y(-3) = exact(-3.0).
%
  i = 1;
  A(1,1) = 1.0;
  for j = 2 : n1 + 1
    A(1,j) = A(1,j-1) * x1(1);
  end

  y1(i) = exact ( xl, alpha, beta );
%
%  y'(-3) = exact'(-3.0).
%
  i = 2;
  A(2,1) = 0.0;
  A(2,2) = 1.0;
  for j = 2 : n1 + 1
    A(2,j) = ( j - 1 ) * x1(1) ^ ( j - 2 );
  end
  y1(i) = exact1 ( xl, alpha, beta );
%
%  y''-6y'+13y = 0 at nodes 2 through N-1.
%
  for i = 3 : n1 + 1
    xp0 = 1.0;
    xp1 = 0.0;
    xp2 = 0.0;
    for j = 1 : n1 + 1
      A(i,j) = xp2 - 6.0 * xp1 + 13.0 * xp0;
      xp2 = j * xp1;
      xp1 = j * xp0;
      xp0 = xp0 * x1(i-1);
    end
    y1(i) = 0.0;
  end
%
%  Solve the system.
%
  A
  y1

  c = A \ y1;
%
%  Set up a table of the collocation function.
%
  n2 = 101;
  x2 = linspace ( xl, xr, n2 );
  y2 = r8poly_values_horner ( d, c, n2, x2 );
%
%  Set up a table of the exact solution.
%
  n3 = 101;
  x3 = linspace ( xl, xr, n2 );
  y3 = exact ( x3, alpha, beta );
%
%  Plot.
%
  clf
  hold on
% axis ( [ xl, xr, 0.0, 8.0 ] )
  plot ( x2, y3, 'r-', 'LineWidth', 8 );
  plot ( x2, y2, 'b-', 'LineWidth', 2 );
  grid on
  title ( 'Collocate second order ODE using polynomial.', 'FontSize', 24 )
  hold off
%
%  Save plot in a file.
%
  filename = 'test04.png';
  print ( '-dpng', filename );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Created plot file "%s".\n', filename );

  return
end
function value = exact ( x, alpha, beta )

  value = exp ( 3.0 * x ) .* ( alpha * cos ( 2.0 * x ) + beta * sin ( 2.0 * x ) );

  return
end
function value = exact1 ( x, alpha, beta )

  value = 3.0 * exp ( 3.0 * x ) ...
    .* ( alpha * cos ( 2.0 * x ) + beta * sin ( 2.0 * x ) ) ...
    + exp ( 3.0 * x ) ...
    .* ( - 2.0 * alpha * sin ( 2.0 * x ) + 2.0 * beta * cos ( 2.0 * x ) );

  return
end
