chapter4 - Chapter 4 Exponential Growth 4.1 Powers...

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

View Full Document Right Arrow Icon
Chapter 4 Exponential Growth § 4.1 Powers User-defned 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

chapter4 - Chapter 4 Exponential Growth 4.1 Powers...

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

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