Skip to main content

Exercises 4.7 Homework

1.

Explain the output of the script in Section 4.4 mathematically. What does it converge to, and why does it sometimes fail to converge? (This problem does not involve Matlab.)

2.

Modify Example 4.6.1 so that it finds the first partial sum that is greater than \(20\text{.}\) Then use the tic toc commands to time two versions of this loop: one with reciprocals computed as 1/n and one with n^(-1). Which version runs faster?

(If the program takes more than a minute to run on your computer, replace \(20\) by \(15\text{.}\))

3.

Write a script that computes the arithmetic-harmonic mean which means is similar to Example 4.2.1 except that instead of geometric mean g = sqrt(x*y); we use the harmonic mean h = 2*x*y/(x+y). As a testing case, check that the arithmetic-harmonic mean of 5 and 20 is 10.

Since there is no square root in this computation, we can use negative numbers too: the arithmetic-harmonic mean of -5 and -20 is -10. But if you start with two numbers of opposite signs, the loop never ends, so you will need to press Ctrl+C to end it. As a second part of this task, write another form of the loop, using the method from Section 4.4 to put a limit on the number of tries, for example 1000.