## MA/CS 371 - Lab 1 Machine Constants and Series Approximation

### Section 1: Introduction

The goal of this lab is to introduce you to:
• the machine constants Inf, NaN (Not-a-Number), eps, realmax, and realmin
• implementing series approximations in MATLAB and observing their convergence behavior
• reformulating a problem to accelerate the series convergence

If you have not already done so, create a directory for this lab and copy the lab 1 files into it. If you start off in your home directory you can do the following:

```	mkdir ~/cs371/lab1
cd ~/cs371/lab1
cp ~cs371/lab1/* .
```
and (as with last week) don't forget to type that last period.

### Section 2: Topics for Lecture

Each week at the beginning of lab I'm going to present a few random topics - either MATLAB ideas that will make your lives easier or topics that were a problem to a lot of people the previous week. This week's lecture will include
• UNIX file system abbreviations (tilde, dot, dot-dot)
• The method MATLAB uses for representing numbers - double precision floating point
• The 'colon' operator in MATLAB which generates nice vectors
• Building up matrices in MATLAB with square brackets and semicolons

### Section 3: Finding the largest and smallest representable real numbers

Last week you were given a program called mach_eps.m which calculates machine epsilon. Take another look at that program and make sure you understand what it is doing. It essentially takes a number and keeps dividing it by two until it is too small to affect the value of 1.0 by addition.

Write two similar programs: calc_smallest.m to calculate the smallest representable positive double precision number, and calc_realmax.m to calculate the largest representable double precision number. You can use mach_eps.m from last week as a template. For calc_smallest.m, you might keep dividing a test value by two until some end condition happens, and for calc_realmax.m, you might keep multiplying a test value by two until some end condition happens. You might want to take a look at the finite function in MATLAB for the latter.

Verify calc_realmax against the MATLAB-supplied number. If you have been careful not to use a variable named realmax, then typing that at the MATLAB prompt will return the actual value.

Note: There is a MATLAB value called realmin which will not be the same as the value your calc_smallest.m program returns. I'll explain that at the beginning of lab next week.

### Section 4: Estimating pi by computing arctan(1)

Pi is a transcendental value which measures the ratio of the circumference of a circle to its diameter. The term transcendental means "a function that cannot be expressed by a finite number of algebraic operations." This implies that we cannot compute an exact value for pi, so we must use an approximation.

One way to approximate pi is to use the fact that arctan(1) = pi/4. Multiplying through by 4 we see that pi = 4*arctan(1). This solution is particularly appealing because the series for arctan(1) is fairly straightforward.

While arctan(1) is also a transcendental value, it has several infinite series representations. We will consider two different series approximations. (I apologize for the ASCII graphics)

```
Inf
---        k
arctan(1) = \      (-1)
/     ------
---   2k + 1
k=0
```
is due to Gregory (1671) and Leibniz (1674), and arctan(1) = arctan(1/2) + arctan(1/3)
```		Inf
---               2k+1        2k+1
\         k  (1/2)     + (1/3)
/     (-1)   ---------------------
---                 2k + 1
k=0
```
is due to Hutton (1776).

The program calc_pi.m was prepared for this lab. It is a skeleton which will evaluate two series approximations a number of times and produce two plots: one plot showing the series values after each term and one showing the errors after each term. Using calc_pi.m as a template, add in the required statements (the loop condition and the lines calculating each new element of the series) and then run your program calc_pi. When you are confident that the program works correctly, answer this week's lab questions.

Note: to answer some of the questions you may need to edit calc_pi.m or type things into MATLAB after running it. Make certain that when you're done answering the questions that the file calc_pi.m calculates the Gregory-Leibnitz series to 3 digits of precision and the Hutton series to machine epsilon. Please be sure also that your final program does not generate a lot of clutter on the command window and do not change the commands to generate the plots.

### Section 5: Turning in your programs

When you have completed this lab assignment, you should have the following programs in your ~/cs371/lab1 directory:
• calc_smallest.m
• calc_realmax.m
• calc_pi.m
When these are ready to turn in, type the following command:
`~cs371/submit lab1 `
This is a UNIX program which will copy those files into my directory so I can grade your work. If the script runs successfully, it will print no error messages and say "Files copied successfully." If anything else happens, let me know. When you have completed the questions and turned them in (or e-mailed them) to me, and successfully run the submit program, you are done.

### This week's questions

There will be hard copies of this week's questions available in lab. If you want to be a cool paperless 'net citizen of the '90s you can fill out an electronic copy of these questions and e-mail it to me. I must receive it by 11:59pm Thursday for it to count.