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.
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 xwhere
n = iteration number x = the newly assigned estimate xIf 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.
x*tan(x/2) = 3.7According to instructions below, put your answers in the file manysolve.txt.
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.