%% Lab 1 -- Numerical Integration
%% Plotting and Calculating
% Below is a copy of the code from the Pre-Lab. Play around with it! Move the slider to see how the number of rectangles change, try plugging different functions, endpoints, whatever!
% When you feel comfortable move on to Exercise 1 below.

% Feel free to play around with the function definition for f 
%  and visualize what happens for different functions. 
f = @(x) sin(x.^2) + 1 

% The interval we care about is [a,b]
a = 0
b = 3
fplot(f, [a, b])
hold on

% number of divisions
N = 10

% if we divide the interval [a,b] into N pieces, each piece would have
% width
width = (b-a) / N

% The command below says to look at all values of x, starting from a, 
% increasing by the variable 'width' each time, until the final value 
% where x = b -width
for x = a:width:(b-width)
    % for each of the x values, we draw a rectangle with the lower left
    % corner at coordinate (x,0), width the variable 'width', and height
    % the value f(x).
    rectangle('Position', [x 0 width f(x)], 'EdgeColor', 'r')
end
hold off

% Compute the corresponding Riemann sum
approximated_integral = sum(f(a:width:(b-width)) .* width)

%
%% Exercise 1
% Let g(x) be an increasing function. We wish to approximate the integral \int_0^{10} g(x) ~\mathrm{d}x by the sum
% \sum_{j = 0}^{99} g(0 + j \cdot 0.1) \cdot 0.1
% Is the approximation an underestimate or an overestimate? Why? What if g(x) is instead a decreasing function?
% Record your answers on the Lab Worksheet.
%
%% Exercise 2
% Below you can find the code for drawing the rectangles and computing the approximate integral  copied from above.
% Your tasks:
% Edit the line of code that reads rectangle('Position', ...) so that the rectangles drawn are such that the top right corner sits on the graph of f(x).
% Edit the line of code that reads approximated_integral = ... so that the areas computed agree with these new rectangles.
% Record your answers on the Lab Worksheet.

f = @(x) sin(x.^2) + 1;

% The interval we care about is [a,b]
a = 0;
b = 3;
fplot(f, [a, b])
hold on

% number of divisions
N = 8;

% if we divide the interval [a,b] into N pieces, each piece would have
% width
width = (b-a) / N;

% The command below says to look at all values of x, starting from a, 
% increasing by the variable 'width' each time, until the final value 
% where x = b -width
for x = a:width:(b-width)
    % for each of the x values, we draw a rectangle with the lower left
    % corner at coordinate (x,0), width the variable 'width', and height
    % the value f(x).
    rectangle('Position', [x 0 width f(x)], 'EdgeColor', 'r')
end
hold off

% Compute the corresponding Riemann sum
approximated_integral = sum(f(a:width:(b-width)) .* width)

%
%% Exercise 3
% As discussed above, the formula
% \sum_{j = 0}^{N-1} f\big(a + j \cdot \frac{b-a}{N}\big) \cdot \frac{b-a}{N}
% approximates the integral \int_a^b f(x) ~\mathrm{d}x using N rectangles with the top-left corners lined up with the graph of f(x).
% Write, in Sigma notation, a formula for the case where instead the rectangles have the top-right corners aligned with the graph.
%
%% Exercise 4
% Let A(N) be the approximate integral to \int_a^b f(x)~\mathrm{d}x using N rectangles with top left corners on the graph of f(x).
% Let B(N) be the approximate integral using N rectangles with top right corners on the graph of f(x).
% Justify the formula
% B(N) - A(N) = \left( f(b) - f(a) \right) \cdot \frac{b - a}{N}
% Hint: For N being a small number (like 3, 4, or 5), expand the formula for the approximate integral using rectangles with top left corners on the graph of f(x) as
% \sum_{j = 0}^{N-1} f\big(a + j \cdot \frac{b-a}{N}\big) \cdot \frac{b-a}{N} = f(a) \cdot \frac{b-a}{N} + f(a + \frac{b-a}{N}) \cdot \frac{b-a}{N} + \cdots + f(b - \frac{b-a}{N}) \cdot \frac{b-a}{N}
% Now do the same for the case with the top right corner. Subtract the two: do you see a pattern?
%
%% Some final remarks
% The identity alluded to in Exercise 4 tells us that integration can be approximate by the sums, when you look at functions that are either increasing or decreasing.
% The reason is that by Exercise 1, for increasing functions, one of A(N) and B(N) is an overestimate, and the other an underestimate. So we know that the integral has to lie between the two values. But as N gets larger and larger, since the endpoints a and b are fixed, the difference B(N) - A(N) has to get smaller and smaller. And since the integral is between the two values, it must be closer to B(N) than A(N) is, and vice versa. So both B(N) and A(N) must become closer and closer to the true integral.
% This is, in a nut shell, why we can often use sums to approximate integrals in many applications.