function [ u, x ] = fd1d_poisson ( nx, xmin, xmax, f, g )

%*****************************************************************************80
%
%% fd1d_poisson is a simple program to solve 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 = f(x) in Omega
%            U = g(x) on dOmega
%
%    The user specifies 
%      the number of grid points (nx), 
%      the spatial extent (xmin,xmax), 
%      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 MIT license.
%
%  Modified:
%
%    05 December 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer NX, the number of grid points.
%
%    real XMIN, XMAX, 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 U(NX), a table of solution values.
%
%    real X(NX), the coordinates of grid points.
%

%
%  Compute the spacings.
%
  hx = ( xmax - xmin ) / ( nx - 1 );
%
%  Compute the coordinate vector.
%
  x = linspace ( xmin, xmax, nx );
%
%  Set the system matrix A and right hand side rhs.
%
  A = zeros ( nx, nx );
  rhs = zeros ( nx, 1 );

  eqn = 0;
  for i = 1 : nx
    eqn = eqn + 1;
    if ( i == 1 || i == nx )
      A(eqn,eqn) = 1.0;
      rhs(eqn) = g ( x(i) );
    else
      A(eqn,eqn) = 2.0 / hx / hx;
      A(eqn,eqn-1)  = - 1.0 / hx / hx;
      A(eqn,eqn+1)  = - 1.0 / hx / hx;
      rhs(eqn) = f ( x(i) );
    end
  end
%
%  Solve for u, the solution vector.
%
  u = A \ rhs;

  return
end

