Physics 3730/6720 Assignment 4

Reading and references:

For each of the following exercises create the specified files with your answer(s). Submit your homework using the course submit utility.


Exercise 1. Modular programming: Newton Raphson root finding code

The modular programming style (used by professionals) breaks a problem down into small units that each have a distinct logical function. That makes it much easier to understand, maintain and upgrade the code. So in this exercise you are asked to write a program to find a root of a function using the Newton-Raphson method in a modular programming style. The exercise also provides more practice with subprogram linkage.

Your code is to consist of three C++ files and a make file. We provide the file f.cc. See ~p6720/examples/root/f.cc.

findroot.cc   	Main program
nr.cc    	Subprogram for finding the zero of a function.
                Name the subprogram "nr".
f.cc		Function whose zero is sought (provided)
Makefile	Makefile.
You should hand in the three files that you create, namely findroot.cc, nr.cc, and Makefile

The main program reads the parameters of the problem and then calls the subprogram nr in the file nr.cc, which finds the root using the Newton-Raphson algorithm. The subprogram nr, in turn, calls the subprogram f in the file f.cc to get the value of the function and its derivative. The subprogram nr then returns the answer (if it finds it) to the main program, which prints out the result or complains if the method did not converge.

So we have separated the logical functions of (1) interacting with the user, (2) running the Newton-Raphson algorithm, and (3) evaluating the function whose zero is sought.

Here are more details about your code:

findroot.cc The main program should prompt for and read two values in exactly this order: (1) The starting guess for the solution and (2) the tolerance. When it calls nr it should pass those two input values and get back two output values, namely, the solution (if found) and an integer giving the status of the solution. The status integer should be equal to zero if the solution was found and one if the method did not converge. That way the main program knows whether to print the solution or to print a complaint. You may let the input variable for your starting guess also be the output variable with the solution. You may also let the status integer be the return value of the subprogram. If you do both of those, your function nr would have only two arguments.

nr.cc So you can follow the progress of your method, at each iteration write out the following values immediately after assigning the new x:

   n  x 
where
   n         = iteration number
   x         = the newly assigned estimate x
If the method does not converge, your subprogram should still return to the main program and let the main program handle the error condition. As described above, it returns a status integer that tells the main program whether it succeeded.

Makefile In your make file the name of the make target (and executable) should be findroot, so you compile with the command make findroot and execute with the command findroot. Please read the Computer Lab Manual section about make with multiple source files. See "Reading and references" above for the link.

f.cc Please do not modify the code in f.cc. When we grade your assignment, we will be replacing that function with a different one.

You may run my solution code in ~p6720/examples/root/findroot to check your answers.


Exercise 2. Approaching a problem in several ways

In this exercise we compare the merits of various methods at our disposal for finding the lowest two positive roots of the equation
 
  x*tan(x/2) = 3.7
According to instructions below, put your answers in the file manysolve.txt.
(a) Try to use Maple fsolve to solve the problem. Hand in the text (input and output). Discuss any problems you encountered in getting all the roots and the strategies needed to overcome them.
(b) Find an approximate location of the roots, by making a plot of f(x) using gnuplot. In your answer file, give the gnuplot command for generating the plot and your estimate.
(c) Use your (or my) code findroot to solve for the roots. Hand in your results.
(d) Consider finding the exact roots by an analytic (algebraic) method. (Don't spend a lot of time on this one! It can't be done.)

Finally, discuss the relative merits of the four approaches in terms of accuracy and cost in time and effort. In this comparison, suppose in each case you started cold, not knowing anything in advance about where the roots might be.

Submit manysolve.txt.


This page is maintained by:
Carleton DeTar Mail Form
Last modified 22 September 2008