next up previous
Next: About this document ...

ESC 251 HW 14 Due: Oct. 5

Remember to indent the bodies of your IF-THEN and looping structures.

(5 pts) Write a MATLAB script that will ask the user to input a series of positive numbers and compute their geometric mean, defined as

\begin{displaymath}\sqrt[n]{x_1 \cdot x_2 \cdots x_n}.\end{displaymath}

You should use a while loop to do the input. You can trigger the end of the input by having the user input a negative value.
(5 pts) Suppose that MATLAB did not have the linspace function and you need to generate a vector of n+1 equally spaced points on some interval of the x-axis, [a,b], given the values of a, b and the number of subdivisions, n. There are 2 ways to do this:
      Method 1:
         h = (b-a)/n
         x(1) = a
         for i = 2:n+1
            x(i) = x(i-1) + h

      Method 2:
         h = (b-a)/n
         for i = 1:n+1
            x(i) = a + (i-1)*h
Test both methods using the values $a = -\displaystyle{\frac{\pi}{2}} , b = \displaystyle{\frac{\pi}{3}}$ and n = 123456789. For both cases, compute the relative error in the value of x(n+1) once the loop terminates (this should be equal to b). Recall that relative error is defined as

\begin{displaymath}\mbox{Relative Error} =
\frac{\mbox{Approximate value}-\mbox{Exact Value}}
{\mbox{Exact Value}}\end{displaymath}

Which method is more accurate (i.e., which method has the smaller relative error)? Explain why the more accurate method has the lower relative error.

next up previous
Next: About this document ...