Math171 - Alexiades
                          Lab 9
                 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. Now 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
        Errors should be printed with %e

    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 of this integral with the value of π ?

    7. What to submit: In a single plain text file "Lab9.m" insert the following:
      0. % NAME , Date , Lab9
      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)
      3. your "Quadr.m" file
        % =============================================== (separator)
      4. your "TrapzRule.m" file
        % =============================================== (separator)

  • Clean up the file, and make sure your Lab9.m runs: >>  Lab9
  • Now produce a PDF file: >>  publish('Lab9.m', 'pdf')   (works in Matlab, not sure it works in Octave).
      This inserts the 'Lab9.m' file into the PDF, and executes it and inserts the output into the PDF.
      The file 'Lab9.pdf' will be in the html subfolder of your current folder.
  • View the .pdf to make sure it contains what it should (items in 7. and output from code).
    If the PDF does not contain the output of the code, run Lab9.m and
      copy-paste the output at the end of Lab9.m as comment lines.
      Then publish again to produce PDF.
  • Submit Lab9.pdf on Canvas by due date.