function I =trapezoid(F,ab,n,varargin)

% TRAPEZOID
% I =trapezoid (F,[a,b]) takes a function F and integrates it from a to b
% using the composite trapezoid rule. It does it for several values of n,
% the number determining how many times the function is evaluated and how
% small the spacing is

%Make F callable by feval
if ischar(F) & exist(F)~=2          %Will only return true if F is a character array 
    F=inline(F);
elseif isa(F,'sym')                 %If F is an obj of class 'sym' return true
    F=inline(char(F));
end
                      
a=ab(1);                             %Left Endpoint
b=ab(2);                             %Right Endpoint
fa=feval(F,a,varargin{:});           %Function at Left Endpoint
fb=feval(F,b,varargin{:});           %Function at Right Endpoint

h=(b-a)/(n-1);

L=(h*fa)/2
R=(h*fb)/2

sum=L+R;
for i=1 :n-2
    sum=sum+h*feval(F,a+i*h,varargin{:})
end

