Foundation Chap4

Foundation Chap4 - Chapter 4 Designing Functions 4.1...

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

View Full Document Right Arrow Icon
Chapter 4 Designing Functions § 4.1 Packaging an Idea Computing Square Roots § 4.2 Identifying Parameters The Betsy Ross Problem § 4.3 Deciding What to Encapsulate Ellipse Perimeter There are a number of reasons why the built-in sin function is so handy. To begin with, it enables us to compute sines without having a clue about the method used. The design of an accurate and efficient sine function is somewhat involved but by taking the “black box” approach, we are able to be effective sin -users while being blissfully unaware of how the built-in function works. All we need to know is that sin expects a real input value and that it returns the sine of that value interpreted in radians. Another advantage of sin can be measured in keystrokes and program readability. In- stead of disrupting the “real business” of a program with lengthy compute-the-sine frag- ments, we merely invoke sin as required. The resulting program is shorter and reads more like traditional mathematics. Being able to write effective functions is central to the problem solving process. It supports the top-down methodology, enabling us to abstract away lower-level details while we work on higher-level software design issues. We start out in § 4.1 developing a function for computing square roots that is based on a rectangle averaging process. The fact that sqrt is a built-in function gives us a standard against which we can measure the quality of our implementation. The principles of top-down design are revealed in § 4.2 where we develop a graphics function that displays a colonial Fag. It relies on two other functions whose availability we assume but whose implementation remains hidden until the next chapter. ±inally, in § 4.3 we deal with a situation where there are two reasonable function-writing possibilities. The dilemma is resolved by looking at some efficiency issues. 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Chapter 4. Designing Functions Example 4.1 MySqrt Relative Error % Script Eg4_1 % Examines relative error associated with MySqrt. clc disp(’ A MySqrt(A) Relative Error’) disp(’--------------------------------------------------’) for k=-6:6 A = 10^k; s1 = MySqrt(A); if k~=0 relErr = abs(MySqrt(A) - sqrt(A))/sqrt(A); disp(sprintf(’10^%2d %20.15e %6.2e’,k,s1,relErr)) end end Sample Output: A MySqrt(A) Relative Error -------------------------------------------------- 10^-6 1.296191592706878e-003 2.96e-001 10^-5 3.172028655357482e-003 3.08e-003 10^-4 1.000000002549074e-002 2.55e-009 10^-3 3.162277660168379e-002 0.00e+000 10^-2 1.000000000000000e-001 0.00e+000 10^-1 3.162277660168379e-001 0.00e+000 10^ 1 3.162277660168379e+000 1.40e-016 10^ 2 1.000000000000000e+001 0.00e+000 10^ 3 3.162277660168379e+001 0.00e+000 10^ 4 1.000000002549074e+002 2.55e-009 10^ 5 3.172028655357483e+002 3.08e-003 10^ 6 1.296191592706879e+003 2.96e-001
Background image of page 2
4.1. Packaging an Idea 3 4.1 Packaging an Idea Problem The act of computing the square root of a positive number A is the act of “building” a square with area A . Thinking along these geometric lines, we make a pair of observations. First, if a square with side A has the same area as an L -by- W rectangle, then A is in between L and W . See Figure 4.1. Second, we can make any given rectangle “more square” Figure 4.1: A Square and Rectangle with the Same Area by replacing L with L new =( L + W ) / 2 and W with W new = A/L new . The process can obviously be repeated. See Figure 4.2.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/15/2008 for the course CS 100 taught by Professor Fan/vanloan during the Fall '07 term at Cornell.

Page1 / 18

Foundation Chap4 - Chapter 4 Designing Functions 4.1...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online