## MA/CS 371 - Lab 2 The Newton-Raphson Method and the Secant Method

### Section 0: Finishing off Lab 1

Everyone should finish and run submit on their results for Lab 1 no later than 6:15pm. I will start lecturing shortly after 6:15pm.

If your answer for the calc_realmax program is greater than one-half the value returned by MATLAB when you type realmax, you're at least on the right track -- your value has the largest possible exponent. Don't get stuck on finishing this program if it prevents you from answering the rest of the questions.

### Section 1: Introduction

The goal of this lab is to introduce you to:
• implementing the Newton-Raphson zero-finding algorithm
• implementing the Secant Method zero-finding algorithm
• a little bit of graphing in MATLAB

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

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

### Section 2: Topics for Lecture

This week you will be expected to write some of your own graphics output inside a MATLAB function file, so I will go through the basics:
• The difference between MATLAB script files and MATLAB function files
• Various plotting commands (plot, semilogx, semilogy,loglog)
• Some cool things to put around your plots (xlabel, ylabel, title)
• Plotting more than one thing on the same set of axes with hold
• Putting more than one set of axes on a window with subplot
• zeroing in on a specific location in the graph with axis

### Section 3: The Newton-Raphson Zero-Finding Method

Recall from Section 3.2 of the book that the Newton-Raphson method is a way to find zeros of a function provided you can:
• Evaluate the function at arbitrary points
• Evaluate the derivative of the function at arbitrary points
The Newton-Raphson method is useful for a function which is too complicated for you to find the zeros directly but is not too complicated to determine its derivative. Let's consider the function
```                                 2
f(x) = sin(x) + x  - 1      .
```
It is usually a good idea, before running any zero-finding algorithm, to take a look at your function and see more or less where the zeros are. Take a look at the program plot_f.m, fill it in where the comments ask you, and see what you get.

It should look like f(x) has a zero somewhere around -1.4 or so and another one around .7 or so.

Now that we have an idea of what f(x) looks like - it looks smooth and fairly simple - we can choose good initial values and expect the Newton-Raphson method to work.

Note that to correctly compute the next step in the Newton-Raphson method, we need the derivative of f(x). Fortunately our f(x) is simple enough to do that algebraically. (I'm leaving that up to you.)

Now look at the program newton.m. Note that this is a MATLAB function instead of a MATLAB script. You will run it by specifying an initial value for it to start with. For example, you would type newton(2) to start with an initial guess of 2.

Finish the program newton.m and answer the corresponding questions on the answer sheet.

### Section 4: The Secant Method

Recall from lecture that the Secant Method is an iterative zero-finding algorithm which does not require evaluating the function's derivatives. The book claims that this method usually converges about as fast as the Newton-Raphson method. Well, let's check out that claim.

The program secant.m is again just a skeleton, and in fact it probably looks almost exactly like the skeleton for newton.m - this should not be surprising at these are fairly similar methods attacking the same problem.

Finish the program secant.m (for the same function f(x) used above) and answer the corresponding questions on the answer sheet.

Feel free to take a crack at the extra credit problem if you want.

### 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.