## Copyright (C) 2007 Tiago Charters de Azevedo
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

function I=intsimpson(a,b,n)
%Simpson integration rule
%Integrates a function f:[a,b]->R  with  n = 2*k (even) subintervals

  format short g;
  if (nargin != 3)
    disp("")
    usage ("Integrates f:[a,b], n=2k sub-intervals");
    disp("")
  endif


disp("")
k=[0:n];
mod(k,2);
h=(b-a)/(n);
x=[a:h:b];
aux1=sum(mod(k,2).*f(x));
aux2=sum((1-mod(k,2)).*f(x));

I=h/3.*(2*aux2+4*aux1-f(a)-f(b));
endfunction