%In what follows, we write a FUNCTION to implement 
%the trapezoid rule. The function takes as parameters 
%a=lower limit of integral, b=higher limit of integral,
%n=number of intervals in the trapezoidal estimate. In 
%example below, we use the function 2**x.
%########################################################
function dummy
function t=trap(a,b,n)
h=(b-a)/n
%h is the distance between each x point
x=linspace(a,b,n+1)
%linspace just creates a LIST of n+1 numbers beginning at 
%a and ending at b
y=2.^x
%we use here .** instead of **. It means, from a vector/list
%of x values, we can get a vector/list of y values
%we now have the vector of y values: use the trapezium rule 
%to calculate the integral
t = 0
%in the following for loop, calculate all the terms except the 
%first and last term
for i=2:n
t=t+h*y(i)
end
%in the following, the first and last terms have weigth h/2
t=t+h*y(1)/2
t=t+h*y(n+1)/2
end
%############################################################
%now, lets run a few trapezoidal estimates, for n=1 up to 
%n=10. We are looking at the integral between 0 and 4 here:
for i=1:10
ans=trap(0,4,i)
end
end
