Write a userdefined function with function call I Midpointc
Write a user-defined function with function call I = Midpoint_comp(f, a, b, n) that uses the midpoint strategy of the rectangular rule to estimate the value of the integral of f from a to b using n subintervals, and f is an anonymous function representing the integrand. Then apply Midpoint_comp to estimate the value of integral^4)_)1 sin x/x dx, n = 20 Find the actual value of the integral using the int command and calculate the % relative error for the estimate.
Solution
Save this as a function
function I= Midpoint_Comp(a,b,n)
h = (b-a)/n
i= 1:n;
c_i = a+0.5*(2*i-1)*h;
f = sin(c_i)./c_i % A sample function
I = h*sum(f)
now run this :
a=1;
b=4;
n=20;
Midpoint_Comp(a,b,n)
The ans by mid point rule is 0.8119
by integration method:
syms x
f=int(sin(x)/x, 1, 4)
vpa(f,5)
ans = 0.81212
