Chap 19 Exercises

WebR Status

🟑 Loading...

Loading webR...

Exercise 1 In mathematical theory, we speak of limhβ†’0. It’s tempting to translate this as β€œmake h as small as possible,” but the mathematical meaning doesn’t involve the concept of β€œas possible.” Smallerβ€”but not zero!β€”is better. However, in numerical calculations, such as those done by numD(), a sufficiently small h makes the calculation unreliable. That is, you can easily go too far and set h too small in the slope function. The problems this causes will become obvious in the graph when h is too small.

calculates and plots a numerical derivative of the sinusoid function, using a specified value of h. (That’s the argument named .h.) Remake the plot, using every smaller .h, until you start to see imperfections in the calculated values.

Active R chunk 1

Which of these values for h is the smallest you can go before the problems of too-small h start showing up?

h=0.00001

h=0.00000001

h=0.000000000001

h=0.00000000000001

h=0.000000000000000001

question id: h-too-small-1

Exercise 2  

Figure 1

In , which of the line segments is tangent to the curve at the point marked with a dot?

A       B       C       all of them       none of them      

question id: reptile-put-kitchen-1

Exercise 3 Sometimes a bit of algebra can help us see what’s going on with the instantaneous rate of change. Consider the exponential function ex.

Rather than writing the slope function definition with a 0.1, let’s substitute in the symbol h. This gives Dexp⁑(x)=ex+hβˆ’exh Extracting out the common term ex in the numerator, we arrive at (1)Dex=exehβˆ’exh=ex[ehβˆ’1h]

shows that the slope function of ex is proportional to ex itself. The constant of proportionality is [ehβˆ’1]/h.

  1. Is [ehβˆ’1]/h a function of x?
Yes       No      

question id: lamb-put-bulb-1

Keep in mind that our interest is in Dex. The role of h is analogous to scaffolding on a building site. We seek to remove it eventually. Ideally, if we make h small enough, it will vanish.

  1. Using , find the value of [ehβˆ’1]/h when h=0.1. Which of these is it?
1.271828       1.694343       1.718282       2.718282 (that is, e itself)      

question id: lamb-put-bulb-2

Active R chunk 2

The instantaneous rate of change involves making h very small, but not quite zero. If you make h exactly zero, the result will be ambiguous.

  1. Using , compute [e0βˆ’1]/0. What’s the result?
Active R chunk 3
Inf       NaN       Bogus       An error message result.      

question id: lamb-put-bulb-3

Active R chunk 4
  1. Using , compute [ehβˆ’1]/h. Make h as small as you can, for instance 0.00001 or 0.00000001. What’s the result?
Active R chunk 5

1

It varies with h, but is always around 1.5 for h small enough

There is no particular result. 

question id: lamb-put-bulb-4

Active R chunk 6

Exercise 4  

Which of the line segments is tangent to the curve at the point marked with a dot?

A       B       C       all of them       none of them      

question id: goat-pay-pot

Exercise 5 Suppose you want to borrow L= $10,000 to buy a car. You will have to make equal monthly payments P for five years (60 months) to pay off the loan. At an interest rate of r per month, your payment will be: P(L,r)=Lr(1+r)60(1+r)60βˆ’1

For example, if the interest rate is 0.005 per month (roughly 6% = 0.06 per year), your monthly payment would be

Active R chunk 7

You are a good negotiator and are trying to talk the car dealer down to a zero-percent loan. The dealer plugs r=0 into the car-loan calculator. His response to you: β€œSorry, but our system cannot handle zero-percent loans.”

  1. Explain what about the output of the calculator supports the dealer’s response. (Use to simulate the dealer’s calculator.)

  1. Look at the formula in . At some point, for r=0, the calculation divides by zero and multiplies by zero. Explain what about the formula causes this to happen.

You press the point. β€œExcuse me, Sir, but I’m reading MOSAIC Calculus. That NaN appearing on your screen is the result of a division by zero in the formula. But there is no reason to do that division. At zero percent interest, the monthly payment will simply be the amount borrowed divided by the number of months of the loan, so $10,000 divided by 60, giving a monthly payment of $166.6666… .”

β€œI’m sure you’re very good at math,” the dealer says, β€œand I’m willing to agree to a monthly payment of $166.67, but I cannot process any loan without going through this computer system. And zero percent won’t fly.”

Having studied limits, you have an idea. β€œSuppose we agree on a non-zero interest rateβ€”which your loan system can handleβ€”that produces a monthly payment of $166.67? Can we write up the loan that way?”

Dealer: β€œI’d be happy to do that, but obviously it is impossible to find an interest rate greater than zero that gives the same result as your calculation for zero interest.”

You: β€œLet’s try. Instead of 0.005 for the monthly interest rate, put in r=0.0005. You’re computer can work with that, right?”

Dealer: β€œOK, let’s see. … Yes, the payment amount is $169.22. That’ll work.”

You: β€œWe are making progress. But my sense of mathematical honor insists that we find an interest rate that gives $166.67, as close as we can practically get to the exact answer result $166.6666… .”

  1. Work through the sequence r=0.005, r=0.0005, r=0.00005, making r smaller and smaller. Is there a small but non-zero r that brings the payment to the agreed-upon goal of $166.67?. Explain what about the output of the calculator supports the dealer’s response. If so, what’s the largest r that will bring you there.
5e-04       5e-05       5e-06       0.0000005       5e-08       5e-09       5e-10       no such r      

question id: low-low-rates-3

Later in the day, you tell the story to your roommate, who is a computer science major. She says that you were lucky. β€œComputer arithmetic isn’t the same as mathematical arithmetic. Computer calculations with very small numbers sometimes give different results than you would expect mathematically. I bet if you tried an even smaller r, you would have gotten different answers or even just nonsense.”

  1. Work through smaller and smaller r in the car-loan calculator. What happens as you continue the sequence from the previous question, adding in more zeros before the 5?

The result steadily gets closer to 166.6666….

At first, the result gets closer to 166.6666, but then as r gets smaller the result stays the same.

The result falls below 166.666 and stays there.

The computer output shows a result of infinity.

question id: low-low-rates-4

See this news story from 1991 for a real-world example of a similar flaw.

Exercise 6 Use for evaluating sin⁑(x)/x at tiny values of x or for plotting it on a domain that is somewhat wider. To answer the questions, you just have to change tinyx.

Active R chunk 8
  1. Using , add more zeros to tinyx to make it even smaller. You can stop when you get tired. Does sin⁑(x)/x evaluate to something sensible for such tiny input values? If so, what value?
0       1/2       1       answer varies with x as x gets smaller      

question id: rhinosaurus-toss-gloves-1

Saying, β€œso small that I got tired typing the zeros” is not a convincing definition of β€œsmall” to a mathematician. For example, 0.0000000000000001 parsec (a unit of length) seems small but it is equivalent to about 10 feetβ€”not so small. Mathematicians want you to take β€œsmall” to the limit, an arbitrarily large number of zeros, and when you’re done with that, even more zeros.

Fortunately, R and other computer languages have a scientific notation that allows you just to name the number of zeros you want after the decimal point. For instance 1e-2 is 0.01β€”one zero. Similarly 1e-20 is 0.00000000000000000001, nineteen zeros.

  1. Again use , but this with scientific notation so that you can look at x as small as 1e-31 (30 zeros) or even smaller. Starting at x = 1e-31, calculate sin(x)/x. Then double the number of zeros, keep on doubling the number of zeros. The result will continue to be 1 … until it eventually becomes NaN. How many zeros are there in the x that produces NaN as the answer to sin(x)/x?
127       191       323       379       1281      

question id: rhinosaurus-toss-gloves-2

What’s happening here has more to do with the nature of computers than the nature of numbers. Computers (in the manner they are ordinarily programmed) use packets of bits to represent numbers, and the chips have been engineered to make those bit packets respond to arithmetic operations as if they were the numbers they represent. A typical computer number, like 0.001, uses 64 bits in a special, standard format. Since there is a finite number of bits, there is a largest possible non-Inf number and a smallest possible non-zero number. According to the IEEE standard for β€œfloating-point” arithmetic the largest non-Inf number is around 1e300 and the smallest non-zero number is around 1e-320. This failure to behave like genuine mathematical numbers is called β€œoverflow” (for large numbers which turn into Inf) and β€œunderflow” (for small numbers which turn into 0).

  1. Play around with numbers in the format 1e300, 1e301 and so on until you find the smallest 1e??? that prints as Inf. Similarly, try numbers in the format 1e-320 and 1e-321 until you find the largest one that prints out as exactly zero. What are those two numbers?

1e305 and 1e-322

1e306 and 1e-323

1e308 and 1e-324

1e309 and 1e-327

question id: rhinosaurus-toss-gloves-3

Exercise 7 It is simple enough to say to an elementary-school pupil that β€œdivision by zero is an error.” But what is a computer to do when, for whatever reason, it divides by an amount that is zero generated by a not-erroneous earlier computation? In many computer languages (including R) the programmer can arrange that a unacceptable situation causes an error message to be β€œthrown.” This generally causes the program to stop. See this account of the USS Yorktown (under β€œSmart ship testbed”) for an example of why stopping the program is not always an acceptable practice.

Modern computer arithmetic systems, implemented in hardware, take a different approach to division by zero. Rather than an error, divide-by-zero calculations return a definite value called β€œnot a number” and printed as NaN. Rather than stopping the program, calculations can continue on. Any calculation involving NaN, such as 3 + NaN or NaN/NaN produces a NaN. This clever design allows the system-level programmer to determine where and whether a division by zero needs to be dealt with.

Active R chunk 9
  1. Warm up: Using the sandbox, determine which one of these arithmetic expressions does not produce NaN as the output.
1/0       0/0       0*NaN       NaN/NaN       5e20 + NaN*1e-50       NaN - NaN       NaN * Inf       sin(NaN)       sqrt(NaN)       Inf/Inf       1/Inf      

question id: NaN-numerics-0

The NaN strategy is effective in plotting. Programs such as slice_plot() simply ignore points whose value is NaN. As a result, the shape of the graph is determined by those points nearby, not at, the singularity. The overall effect is that the graph indicates whether the singularity is removable without the user having to do any juggling with NaN.

For each of the following functions of x, use the following procedure:

  1. Plug in the indicated value of x=x⋆ to find out if the computer returns NaN. If so, the function has a singularity at x⋆.
  2. Draw a graph of the function for a region very close to x⋆ and see if the function behaves smoothly very close to x⋆. If so, find the function’s limit xβ†’x⋆. (Pay attention to the vertical axis scale.)
  3. Look at the formula for the function to see if there are equal terms in the numerator and the denominator that you can cancel out to create a new function that is the same, everywhere but x⋆, as the original.

Example: f(x)≑x2+2xx3+4x at x⋆=0. The sandbox code has been set up for steps (1) and (2). You will have to do step (3) by hand, using algebra. Notice that f(xstar) returns NaN, indicating that there is a singularity at xstar. The graph, however, is perfectly smooth and has a value of 0.5 at xstar. So the limit limxβ†’0f(x)=0.5. Using algebra, we can rewrite f(x)

f(x)=x2+2xx3+4x=xxx+2x2+4. Eliminating the xx, the remainder of the expression evaluates to 1/2 at x=0 without any problematic division by zero. This agrees perfectly with the graph made by .

Active R chunk 10
  1. True or false: f(x)≑x2+2xx3βˆ’4x behaves nicely near x⋆=0.
TRUE       FALSE      

question id: NaN-numerics-1

  1. True or false: f(x)≑x2+2xx3βˆ’4x behaves nicely near x⋆=βˆ’2.
TRUE       FALSE      

question id: NaN-numerics-2

  1. True or false: f(x)≑x2+2xx3βˆ’4x behaves nicely near x⋆=2.
TRUE       FALSE      

question id: NaN-numerics-3

  1. True or false: f(x)≑x2+xx+x+3 behaves nicely near x⋆=βˆ’2.
TRUE       FALSE      

question id: NaN-numerics-4

  1. True or false: f(x)≑xlog⁑(x) behaves nicely when approaching x⋆=0 from above.
TRUE       FALSE      

question id: NaN-numerics-5

Exercise 8 Consider the function f(x)≑xβˆ’3xβˆ’3. Simple algebra suggests that the output will be 1 for any x value input, but the rules are division mean that the output is undefined for input x=3. Thus, the function has a β€œsingularity” at x=3. We want to test whether this singularity is removable, that is, whether the function has a finite limit as xβ†’3. The computer can help us out. The basic idea is to try inputs closer and closer to x=3 and see if the output behaves nicely. To help you distinguish between non-removable and removable singularities (or, in the words of the song β€œSanta Claus is Coming to Town”, to know β€œwho’s naughty or nice”), defines the function f() and a similar function g(). Try out both f() and g() for a sequence of inputs that get closer and closer to x=3 but are not exactly 3.

Active R chunk 11

Essay 1: Explain what the options(digits=20) statement is doing. You can do some experiments by changing the 20 to something much smaller.

Which of these statements is true about the successive values in x_sequence?

They approach 3 from below.

They approach 3 from above.

They oscillate, getting nearer and farther from 3.

question id: singularity-numerics-1

Which of these statements is true about the successive values in f(x_sequence)?

They approach 1 from below.

They approach 1 from above.

Their value is constant at 1.

question id: YFpUUK

Essay 2: Explain what about the output of the function g() qualifies it for being numerically β€˜naughty’ near x=3.

Activities

Exercise 9  

A demonstration that βˆ‚xex=ex can be made with h-theory. Recall that

βˆ‚xex=limhβ†’0ex+hβˆ’exh=limhβ†’0exehβˆ’1h

Since there is no h in ex, we can pull it out of the limit. βˆ‚xex=exlimhβ†’0ehβˆ’1h.

Now the question of what is the value, if any, of eh+1h?

We can solve this with a simple plot. looks at eh+1h as a function of h, plotting it on a domain where h is β€œsmall”.

Active R chunk 12

Is the graph behaving nicely as h→0? Look carefully at the labels on the vertical axis.

Not convinced that 0.001 is a small value for h. Then change it to 0.00001 or whatever you like. How small can you make h before you run into numerical round-off error? Remember to look carefully at the labels on the vertical axis.

Exercise 10  

A demonstration that βˆ‚xsin⁑(x)=cos⁑(x) can be made with h-theory. Recall that

(2)βˆ‚xsin⁑(x)=limhβ†’0sin⁑(x+h)βˆ’sin⁑(xβˆ’y)2h

An identity known since before calculus was invented is:

sin⁑(x+h)=sin⁑(x)cos⁑(h)+cos⁑(x)sin⁑(h).

Similarly, sin⁑(xβˆ’h)=sin⁑(x)cos⁑(h)βˆ’cos⁑(x)sin⁑(h).

Plugging these identities into gives

limhβ†’0[sin⁑(x)cos⁑(h)+cos⁑(x)sin⁑(h)βˆ’(sin⁑(x)cos⁑(h)βˆ’cos⁑(x)sin⁑(h))]/2h

The sin⁑(x)cos⁑(h) terms cancel out, leaving limhβ†’02cos⁑(x)sin⁑(h)2h=cos⁑(x)limhβ†’0sin⁑(h)h where we have used the fact that cos⁑(x) does not have any h in it to pull it outside the limit.

Now the question of what is the value, if any, of sin⁑(h)h?

We can solve this with a simple plot. looks at sin⁑(h)h as a function of h, plotting it on a domain where h is β€œsmall”.

Active R chunk 13

Is the graph behaving nicely as h→0? Look carefully at the labels on the vertical axis.

Not convinced that 0.001 is a small value for h. Then change it to 0.00001 or whatever you like. How small can you make h before you run into numerical round-off error? Remember to look carefully at the labels on the vertical axis.

Exercise 11 For each exercise, you are given a series of intervals that get smaller and smaller. Your job is to calculate the average rate of change of the function f(x)≑x2 for each of the intervals. As the width of the intervals approach zero, our average rates of change become better approximations of the instantaneous rate of change. You should use the results you calculate to make an informed estimate of the instantaneous rate of change.

A. Use these three intervals to estimate the instantaneous rate of change βˆ‚xf(x=3) - [3, 3.1] - [3, 3.01] - [3, 3.001]

B. Use these three intervals to estimate the instantaneous rate of change βˆ‚xf(x=5) - [4.9, 5] - [4.99, 5] - [4.999, 5]

C. Use these three intervals to estimate the instantaneous rate of change βˆ‚xf(x=βˆ’2) - [-2, -1.9] - [-2, -1.99] - [-2, -1.999]

Exercise 12 Using numerical finite-difference differentiation, confirm the rules given in MOSAIC Calculus Sec 19.3 for the pattern-book functions.

  1. Find a small-enough h so that the error over the domain 0 to 1 is less than 10βˆ’6.

  2. Find an even smaller h that produces a substantially larger error than in (a).

Exercise 13 Let’s explore the derivative βˆ‚x1x using the evanescent-h method.

The general definition of the derivative of a function f(x) is the limit as hβ†’0 of a ratio: βˆ‚xf(x)≑limhβ†’0⏟limit f(x+h)βˆ’f(x)h⏟ratio . For an evanescent-h calculation, we put aside for a moment limhβ†’0 and operate algebraically on the ratio. In our operations, we treat h as non-zero.

  1. Substitute in f(x)=1/x and show step-by-step that the ratio (for non-zero h) is equivalent to βˆ’1x2+hx.

  2. The domain of the function 1/x is the entire number line, except zero. Examine the expression βˆ’1x2+hx to see if setting h to zero would ever involve a division by zero for any x in the domain of 1/x.

  3. If setting h=0 cannot cause a division by zero, it is legitimate to do so. Set h=0 and simplify the expression βˆ’1x2+hx. Compare this to the rule for βˆ‚x1x given in MOSAIC Calculus Sec 19.3.

No answers yet collected
Γ—

R History Command Contents

Download R History File