Develop a Mat lab function myTrapezoidal that calculates the
Develop a Mat lab function myTrapezoidal that calculates the integral of y(x) using the composite trapezoidal method for a set of discrete data points (x_i, y_i, ) that are not necessarily equally spaced. As input the function shall take the vectors x and y that contain the data points. As output the function shall give the calculated integral.
Solution
function y = myTrapezoidal(f,l,r,m)
% Inputs
% f name of the function
% l left endpoint of [a,b]
% r right endpoint of [a,b]
% m number of subintervals
% Return
% y trapezoidal rule quadrature value
h = (r - l)/m;
y = 0;
for k=1:(m-1),
x = l + h*k;
y = y + feval(f,x);
end
y = h*(feval(f,l)+feval(f,r))/2 + h*y;
