For the function \(f(x) = \sin(x^2)\) on the interval \([1, 3]\text{,}\) use Matlab to compute the left-, right, and midpoint approximations with \(n=10\)
Solution.
f = @(x) sin(x.^2);
a = 1;
b = 3;
n = 10;
h = (b-a)/n;
x = a:h:b;
L = sum(f(x(1:end-1)))*h;
R = sum(f(x(2:end)))*h;
midpoints = (x(1:end-1) + x(2:end))/2;
M = sum(f(midpoints))*h;
fprintf('Leftpoint %g, Rightpoint %g, Midpoint %g\n', L, R, M);
The output is “Leftpoint 0.483958, Rightpoint 0.398087, Midpoint 0.474599”. (One can check with WolframAlpha that the actual value of the integral is about 0.4633.)
