% Example 12.4a (Physics 330) function s=defint(a,b,N) %********************************************************* % this function uses the midpoint rule on N subintervals % to calculate the integral from a to b of the function % defined in the sub function at the bottom of this % function % load dx and build the midpoint rule x-values %********************************************************* dx=(b-a)/N; x=a+.5*dx:dx:b-.5*dx; %********************************************************* % use the function f(x) defined in the sub function below % to obtain the midpoint approximation to the integral and assign % the result to s %********************************************************* s=sum(f(x))*dx; % here's the sub function function y=f(x) % define the function f(x) and assign it to y y=cos(x); %end defint.m