In today's lab session, please be sure that your work on Lab #1 is done. If you did not finish Lab #1 last time, 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 last time:
How to access and use the unixs server;
How to navigate directories and create and modify files using pico;
How to transfer files from one computer to another using ftp and netscape; and
How to compile programs written in C and Fortran. When you compile a program and rename it to sample, then you make it run by typing
./sample
(Ask me why!)
During today's lab, we will be learning
How to run Matlab and the relation between Matlab programs and C or Fortran programs;
How a square root algorithm works.
Last time, we discussed several different Unix commands, and I suggested purchasing a book on Unix. Well, there are a few interesting web sites that offer this information for free. One site that seems excellent is a Unix tutorial at the University of Alberta.
Last time, we found that the setup command command sometimes complains that it needs a login shell. If you download the following .xsession file to your home directory, the shell you log into will be a login shell. (In Netscape, use the right mouse button to get a menu, and choose "save link as" to get the file saving menu for a link.) Log out (remember, you type exit to log out) and log back in to make this happen.
This course is primarily concerned with running Matlab. The Matlab language is, in fact, an object-oriented programming language with an extensive library of mathematical and scientific function calls entirely built-in. A student version of Matlab is available for $99.00, and this version is not a crippled version. The full set of manuals is on the web in html and also in Adobe PDF format! Look at the "Getting Started" manual. It is excellent, easy to read, provides a wonderful introduction, and is available both in html format and in Adobe PDF format. I would like you to look at six of the chapters in the html version of the Getting Started book:
Introduction
Expressions
Working with matrices
The command window
Flow control
Learning more
Please look at these chapters at home, from your home computer or from a computing lab somewhere. If you wish to do the exercises described in the text, you can do them using telnet from your PC. (If you cannot find telnet in one of your menus, open a MS-DOS window and type the command telnet unixs.cis.pitt.edu This will give you a login prompt.)
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 program you wrote last time, and call it sample.m. The .m extension is required by Matlab for it to recognize the file as a program. Here's how to turn it into a Matlab program:
We're going to need every line of code that actually "does something"; delete all other lines. We'll discuss this together.
Lines of Matlab programs can end with a semicolon, but they don't have to. (What difference will this make?)
To repeat several statements a given number of times, you put the following before and after:
for i = 1 : 5
first statement
...
last statement
end
Once you've gotten a version of sample.m that you think is right, try to run it. Do this by starting up Matlab:
matlab
Inside of the program, type
sample
You probably still have errors. I can help you fix them. Call me over.
Matlab might not print the results to very many digits. To see more detail, tell Matlab:
format long
(I'm sure you can figure out how to go back to short figures!) You can exit from Matlab by typing
quit
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?
Now we are going to do some numerical experimentation in Matlab. We will work on the exercises together. Wait until the results of one exercise are discussed before proceeding to the next one.
Preliminary work starting from your earlier Matlab file, sample.m, first copy it to one called, say, samp.m. Next, edit samp.m and remove the statements assigning values to W and X. Make sure that you have changed nothing else by typing
W=100;
X=1;
samp
You should see exactly the same result you saw when you ran sample.
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:
123456789
1.0E+20
0.000000001
1.0E-20
0.0
-4.0
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 Matlab, the absolute value of X is simply abs ( X ) .
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? To do this in Matlab, replace the line
for i = 1 : 10
by
while ( resid > 0.0001 )
Do not forget to set the value of resid to something large before entering the loop. Try your new code for the following values of W:
100
1000000
1000000000
0.000001
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 August 28, 2000.