Lab 2
Quadrature Rules - Finding Pi
The goal is to implement and compare the basic Quadrature Rules for   I = ∫ab F(x) dx ,
the integral of a given function F(x) over an interval [a,b], using M subintervals.
• The integrand F(x) should be coded as a Matlab function FCN in a separate file FCN.m

1. Implement the Rectangle Rule as a Matlab function in a code RectrUle.m , with both Left and with Right Heights.
a. Inputs to the function RectRule should be a, b, M
b. The code should print out: M and value of RL = Rectangle Rule approximation using Left heights
and value of RR = Rectangle Rule approximation using Right heights
nicely formated, like: RectRule for M= VALUE : RL= VALUE , RR= VALUE
c. Debug on 01 3 dx with M=4.
d. Debug on 01 (2x−1) dx with M=4.
e. Test it on 01 3(1−x2)dx with M=1000.
Looks like rather large M is needed to get a decent approximation...

2. Implement the Midpoint Rule as a Matlab function in a code MidptRule.m
Repeat a - e above to debug and test the Midpoint Rule.

3. Implement the Trapezoidal Rule as a Matlab function in a code TrapzRule.m
Repeat a - e above to debug and test the Trapezoidal Rule.

4. Create a Matlab function Quadr.m with input arguments a, b, M , which
A. calls each of your quadrature rules (with the same a, b, M),
B. computes the error of each rule by comparing with I_exact (see below)
C. prints a (nicely formatted) table like:
Comparison of Quadrature Rules using M= VALUE subintervals:
Rule     Value     Error

5. Value of Pi: How can we produce π (to machine accuracy) if we need it in a computation, in any language ?
Very easily, from the arctan(x) function: since   tan(π/4) = 1,   we have:   PI = 4*atan(1.e0)
In fact, this is the best way to set the value of PI = π in a code, in any language.
• Insert the formula as I_exact in your "Quadr.m" so you can compute and print the errors.

6. Use your Quadr.m to find an approximation to the number   PI   from the formula:
PI = 4 ∫01 √(1−x2) dx
a. Using M = 1000, how many correct digits does TrapzRule produce ?
b. Using M = 100000, how many correct digits does TrapzRule produce ?
c. What is the connection between the formulas for   PI   in 5. and in 6. ?

7. What to submit: In a single plain text file "Lab2.m" insert the following:
0. % NAME , Date , Lab2
1. Answers to questions 6a, 6b, 6c (as comments)
If your results are unexpected, any guesses as to what may be happening ?
% =============================================== (separator)
2. your "FCN.m" file, showing coding of the integrand for PI in 6.
% =============================================== (separator)