% Usage: r = mid3p(f,a,b,n) % Composite 3-point open Newton-Cotes rule % % Input: % f - Matlab inline function % a,b - integration interval % n - number of subintervals (panels) % % Output: % r - computed value of the integral % % Examples: % r=mid3p(@sin,0,1,10); % r=mid3p(@myfunc,0,1,10); here 'myfunc' is any user-defined function in M-file % r=mid3p(inline('sin(x)'),0,1,10); % r=mid3p(inline('sin(x)-cos(x)'),0,1,10); function r = mid3p(f,a,b,n) h = (b - a) / (n*4); hh = h * 2; x = a + h; r = 0; for i=1:n r = r + 2 * f(x); x = x + h; r = r - f(x); x = x + h; r = r + 2 * f(x); x = x + hh; end; r = r * h*4/3;