LAB #1a: Lots of Square Roots



In today's lab session, I'd like you to finish up your work on Lab #1; I'll talk about corrections to the lab, and questions that came up. We'll try to use Octave, the MATLAB look-alike program. If we're lucky, we'll get the square root program running with Octave. We will spend a lot of time looking closely at the square root problem and how various changes affect its performance.

Finish Lab 1

If you did not finish lab #1 on Monday, please do so now. If you skipped parts, or had trouble, or forgot how to do something, look into it today.

Here's some things we found out on Monday:

Try Octave

MATLAB isn't available to us in this lab, and we don't know when it will be. Octave is available, and is supposedly very similar. Let's try to find out right now just how true that is.

To begin with, make sure you have a matlab subdirectory of your home directory. We're going to want to create a file and put it there. You can move to the matlab directory immediately, or edit the file anywhere, and then move it to the directory.

I'd like you to think a little bit, so I'm not going to print out the text of the program I want you to write. Instead, make a copy of the C or FORTRAN program you wrote on Monday, and call it sample.m. Here's how to turn it into a MATLAB program:

Once you've gotten a version of sample.m that you think is right, try to run it. Do this by starting up Octave:

Inside of the program, type
You probably still have errors. I can help you fix them. Call me over. Also, there's an editor built in to MATLAB that makes it easy to make corrections and try them out. Is this available in Octave?

Octave might not print the results to very many digits. To see more detail, tell Octave:

format long
(I'm sure you can figure out how to go back to short figures!) You can exit from Octave by typing

The Square Root Problem

Now we've written programs in three languages to find the square root of a number. We don't understand the languages we've used, but worse, we don't even know why these programs work. There will be plenty of time to talk about all those things. I'd still like to take all that on faith, and just spend some time today testing out the programs, and examining their behavior.

Discussion question - Do the programs we wrote compute the square root of a number? If someone asks you for the square root of 17, what is the correct response?

You can pick any of the three versions of the square root program. We will work on the exercises together. Wait til the results of one exercise are discussed before proceeding to the next one.

Exercise 1 Fix the value of W to be 100, and try the starting points X = 0.00001, 1.0, 5.0, 9.0, 10.0, 11.0, 20.0, 100.0, 1000.0. Which starting values converge the fastest? What is another property of the sequence of approximate values?

Exercise 2: Can we "break" the program? Can you find a starting value X that causes the program to stop suddenly; not to converge, or converge to the "wrong" result?

Exercise 3: What happens for "weird" values of W: Try the following values:

If we're trying to find the square root of 100, we're looking for a number X that satisfies a particular equation:

X * X = 100
Since we presumably don't know what the square root of 100 is (that's why we're trying to compute it), we can't really compute the error, that is, the difference between our estimate X and the true square root of 100. However, we can always determine how well our estimate satisfies the equation. We move everything to one side first, and take the absolute value, and call this the residual error:
Residual ( X ) = Abs ( X * X - 100 )

Exercise 4: Each time you compute a new value of X, compute and print out a variable called resid, the residual error. In FORTRAN and MATLAB/Octave, the absolute value of X is simply abs ( X ) If you are using C, the function is fabs ( X ), and you will have to add the line

        #include < math.h >
to your program, and change your compile statement to:
cc sample.c -lm
(The -lm adds in the necessary "math library".

Discussion: what is the relationship between the size of the residual, and the size of the error? What if one of them is exactly zero? What about the more general case where we are solving an arbitrary problem F(X)=0?

Exercise 5: Now use the residual value you have calculated to control your iteration. We are going to carry out the loop not 5 times, or 20. We are going to do it not too many times or too few, but exactly enough times. Let's decide that the iteration should stop when the residual error is less than 0.0001. How do we express that?

Be careful that you define the value of resid before you enter the loop, and that you update it each time you reset X! Try your new code for the following values of W: What goes wrong?

Many problems are solved by iterations. In many iterations, the residual error can be evaluated. Then the size of this residual error can be used to determine whether to repeat the iteration. Using a test like

resid < 0.0001
is called used an absolute error tolerance. But if we know in advance that the size of the residual depends on the kind of problem we are solving (big numbers have big square roots, small numbers have small square roots), we might want to try a more flexible error approach.

Back to the MATH2070 page.

Last revised on 01 September 1999.