function [ x, u ] = poisson2_solve ( n, a, b, f, g )

%*****************************************************************************80
%
%% poisson2_solve solves a Poisson equation in an interval.
%
%  Discussion:
%
%    This program uses the finite difference method to estimate the solution
%    to a Poisson problem over an interval:
%
%      - Del U + u = f(x) in Omega
%            U = g(x) on dOmega
%
%    The user specifies 
%      the number of grid points (n), 
%      the spatial extent (a,b), 
%      the functions f(x), g(x).
%
%    No attempt is made to efficiently construct the matrix, or to store
%    it sparsely.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    03 May 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer n, the number of grid points.
%
%    real a, b, the spatial extent.
%
%    function handle f, the name of the MATLAB function which evaluates
%    the right hand side of the Poisson equation.
%
%    function handle g, the name of the MATLAB function which evaluates
%    the Dirichlet boundary conditions.
%
%  Output:
%
%    real x(n), the coordinates of grid points.
%
%    real u(n), a table of solution values.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'poisson2_solve\n' );
  fprintf ( 1, '  Solve a Poisson equation over an interval.\n' );
%
%  Compute the spacings.
%
  h = ( b - a ) / ( n - 1 );
%
%  Compute the coordinate vector.
%
  x = linspace ( a, b, n );
%
%  Set the system matrix A and right hand side rhs.
%
  A = zeros ( n, n );
  rhs = zeros ( n, 1 );

  i = 1;
  A(i,i) = 1.0;
  rhs(i) = g ( x(i) );

  for i = 2 : n - 1
    A(i,i-1)  = - 1.0 / h^2;
    A(i,i)    =   2.0 / h^2 + 1.0;
    A(i,i+1)  = - 1.0 / h^2;
    rhs(i)    =   f ( x(i) );
  end

  i = n;
  A(i,i) = 1.0;
  rhs(i) = g ( x(i) );
%
%  Solve for u.
%
  u = A \ rhs;

  return
end

