COM S
chapter4

chapter4 - Chapter 4 Exponential Growth 4.1 Powers...

• Notes
• 17

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

Chapter 4 Exponential Growth § 4.1 Powers User-defined function, function declarations, preconditions and post conditions, parameter lists, formal and actual parameters, functions that call other functions, scope rules, development through generalization. § 4.2 Binomial Coefficients Weakening the precondition 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. It so happens that 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. Instead of disrupting the “real business” of a program with lengthy compute-the-sine fragments, we merely invoke sin as required. The resulting program is shorter and reads more like traditional mathematics. Most programming languages come equipped with a library of built-in functions. The design- ers of the language determine the library’s content by anticipating who will be using the language. If that group includes scientists and engineers, then invariably there will be built-in functions for the sine, cosine, log, and exponential functions because they are of central importance to work in these areas. It turns out that if you need a function that is not part of the built-in function library, then you can write your own. The art of being able to write efficient, carefully organized functions is an absolutely essential skill for the computational scientist because it suppresses detail and permits a higher level of algorithmic thought. To illustrate the mechanics of function writing we have chosen a set of examples that highlight a number of important issues. On the continuous side we look at powers, exponentials, and logs. These functions are monotone increasing and can be used to capture different rates of growth. 115

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

116 Chapter 4. Exponential Growth Factorials and binomial coefficients are important for counting combinations. We bridge the continuous/discrete dichotomy through a selection of problems that involve approximation. 4.1 Powers To raise x to the n -th power, in Matlab we use the expression x ˆ n . Some programming lan- guages, however, do not include a power operator and a programmer would have to write a code fragment, such as the one below, to evaluate x n : xpower= 1; for k= 1:n xpower= x*xpower; % { xpower = x ˆ k } end Each pass through the loop raises the “current” power of x by one. Without the power operator, it is not unreasonable to insert this single-loop calculation as required in a program that requires the computation of a power in just a few places. However, it is not hard to imagine a situation where exponentiations are required many times throughout a program. It is then a major inconvenience
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