Z--Daisy-CS100M SP08-FVL-Chap3

# Z--Daisy-CS100M SP08-FVL-Chap3 - Chapter 3 Rational...

This preview shows pages 1–4. Sign up to view the full content.

Chapter 3 Rational Approximation 3.1 Practically Pi 22/7-ths and Other Fractions 3.2 Nearly Perfect Rectangles Fibonacci Quotients and the Golden Ratio We continue with the theme of approximation by looking at how well we can ap- proximate π and the golden ratio r = 1+ 5 2 with fractions. Of course, π needs no introduction. It is the most famous and important number in all of mathematics. But right up there in the “top 3” along with e =2 . 71828182845905 ... is the golden ratio. All of these numbers have the property that they cannot be expressed as a ratio of two whole numbers. However, they can be closely approximated by fractions, e.g., π 22 / 7. In mathematics a fraction is referred to as a rational number and in this chapter we will develop a computational taste for rational approximation. Starting with π , we will derive a script that computes the best rational ap- proximation p/q subject to “size constraints.” In particular, we will insist that p M and q M where M is a given (presumably large) integer. A “brute force” search procedure would simply check all possible quotients and identify the best. Note that if M =10 6 , then this would require the checking of a trillion quotients. We show with a little analysis that the number of necessary trial fractions can be dramatically reduced. This will be the f rst of many examples in the book that highlight the importance of e ciency. Rational approximation of the golden ratio turns out to involve the Fibonacci number sequence. This sequence of integers is interesting from the computational point of view because it is recursively de f ned, i.e., the n th Fibonacci number is de f ned in terms of its predecessors. Recursive de f nitions/formulas are extremely important in computational science and engineering and our golden ratio problem serves as a nice introduction. 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Chapter 3. Rational Approximation 3.1 22/7ths and Other Fractions Problem Statement Write a script that inputs a positive integer M , and then prints the best approximation to π of the form p/q where p and q are integers that satisfy 1 p<M and 1 q<M . For example, 22/7 is the closest approximation to π with numerator and denominator less than 100. Program Development One way to locate the best p and q is simply to try all possible quotients. Breaking this down, we f rst construct a loop that steps through all possible de- nominators: % Initializations for q = 1:M % Check all possible quotients having denominator q end Checking for all possible numerators requires another loop: % Initializations for q = 1:M % Check all possible quotients having denominator q for p=1:M % Check the quotient p/q end end The next task is to implement a strategy that checks to see if the current value of p/q is the “best so far” and if so, remembers the fact. To do this we maintain a triplet of variables: pBest : the “best” numerator encountered so far qBest : the “best” denominator encounted so far Error : the error associated with pBest/qBest We initialize the search with pBest = 1; qBest = 1; Error = abs(pBest/qBest - pi);
3.1. 22/7ths and Other Fractions

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 04/30/2008 for the course CS 100 taught by Professor Fan/vanloan during the Spring '07 term at Cornell.

### Page1 / 11

Z--Daisy-CS100M SP08-FVL-Chap3 - Chapter 3 Rational...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online