function poisson( ) 
%
% MATLAB function to solve the Poisson equation,
%	
%  -u'' = f, 0<x<1, u(0)=u(1)=0.
%
% -------------------------------------------- %

clear all, clc, clf

% Create a uniform partition of 0<x<1 into N nodes.  

h = 0.1; 
x = 0:h:1;
N = length(x);  

% Assemble stiffness matrix A and load vector b.

A = zeros(N,N); 
b = zeros(N,1); 

for i = 1: length(x)-1;
  
  A(i,i) = A(i,i) + 1/h;   
  A(i+1,i) = A(i+1,i) - 1/h; 
  A(i,i+1) = A(i,i+1) - 1/h;
  A(i+1,i+1) = A(i+1,i+1) + 1/h;

  b(i) = b(i) + f(x(i)) * h/2;  
  b(i+1) = b(i+1) + f(x(i+1)) * h/2; 
  
end

% Enforce the boundary conditions.

A(1,1) = 1.e4;
A(end,end) = 1.e4;

% Solve the system of equations.

U = A\b;

% Visualize the solution.

figure(1)
hold on
plot(x,U)
plot(x,U,'ro')

xlabel('x')
ylabel('U(x)')
grid, hold off, box on

% Define the right-hand side function f as, say, sin(x). 

function y=f(x)
y = sin(x);
