function mnhf_trapz_example(N,subplot_index) %MNHF_TRAPZ_EXAMPLE estimates the integral of cos(x) from x=0 to x=10 % using the built-in function trapz. The exact solution is sin(10). % % N - Number of grid points % subplot_index - subplot in which graphical output is shown % % Usage mnhf_trapz_example(11,1); mnhf_trapz_example(101,2); % mnhf_trapz_example(1001,3); mnhf_trapz_example(10001,4); x = linspace(0.0,10.0,N); y = cos(x); subplot(2,2,subplot_index); box on; hold on plot(x,y,'ko','markersize',4); plot(x,y,'k','linewidth',1) h = x(2)-x(1); text(2,0.7,sprintf('h=%4.3f',x(2)-x(1))) title(['error = ' num2str(abs(sin(10)-h*trapz(y)))])