Foundation Chap4

# Foundation Chap4 - Chapter 4 Designing Functions 4.1...

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

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 flag. It relies on two other functions whose availability we assume but whose implementation remains hidden until the next chapter. Finally, 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

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

View Full Document
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
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.

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.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern