Write a userdefined function called midpointMEm except repla
Write a user-defined function called \"midpointME.m\" (except replace ME with your initials) that evaluates the following integral within a specified tolerance, using the Rectangle/Midpoint Rule. The function takes two inputs: the maximum relative tolerance allowed in the approximation, and a vector of the limits of integration (i.e. [a, b]). The function produces one output: the approximate value of the integral. integral^b_a x e^-x^2/sin(x) dx
Solution
function value = midpointME( a , b , maxRelativeTolerance )
%tolerance in terms of relative error
N = 5;
lastAnswer = 1000000;
while 1
h = (b - a)/N;
answer = 0;
xn = a;
for i=0:N-1
%find value for xn
answer = answer + xn*exp( (-xn*xn)/sin(xn) );
xn = xn + h;
end
answer = answer*h;
relativeError = abs( lastAnswer - answer)/abs( lastAnswer );
if relativeError < maxRelativeTolerance
value = answer;
break;
end;
lastAnswer= answer;
N = N + 1;
end
%sample call : midpointME( 1, 2, 0.5 )
