function estimate = numint(f,n,a,b,method) % Thanks to Mary Begay for providing this template. % Input Notes: % f: @(x) FUNCTION => for example @(x) x^2 % a: lower limit => number % b: upper limit => number % n: Interval => number % method: => string, 'lhs' or 'rhs' % Checking method input is a string rule = isstr(method); if rule == 0 display('Error: Method input must be a string') end %% Left Hand Sum if method == 'lhs' h = (b-a)/n; % Creating a loop for the sum of f(xj) from j = 0 to n-1 sum = 0; for j = 0:1:n-1 xj = a+j*h; yj = feval(f,xj); sum = sum+yj; end estimate = h*sum; %% Right Hand Sum elseif method == 'rhs' h = (b-a)/n; % Creating a loop for the sum of f(xj) from j = 1 to n sum = 0; for j = 1:1:n xj = a+j*h; yj = feval(f,xj); sum = sum+yj; end estimate = h*sum; elseif true fprintf('Error: method '); fprintf(method) fprintf(' has not been programmed yet.\n'); estimate = 0; end end