M371 - Alexiades
Lab 2: Finding Roots
[ For each lab, create a new dir (Lab#) and work in it ]
Consider the problem of finding the zeros of the function
F(x) = x3+2x2+10x−20
1. Are there any ? How many ? How do you know ?
2. Name at least five methods one could use to find the roots.
3. Is there a root in the interval [1,2] ?
Estimate how many iterations would the Bisection algorithm need
to find such a root accurate to 12 decimals.
4. Write a code Bisect (in double precision) implementing the
Bisection Method for F(x)=0 on an interval [a,b].
Evaluation of F(x) should be done in a subprogram FCN(x).
The code should ask for input of a, b, TOL, maxIT.
At each iteration n, should print out (nicely, in columns, use fprintf):
n xn Fn ERRn
(with appropriate format, like
'%d %f %e %e \n' )
Upon convergence, it should print out:
DONE: root=%f , residual=%e , in %d iters
or failure message(s).
Debug on a simple problem, like 1.3-x on [1,2].
5. Use your Bisect code to find the root mentioned in 3, with TOL=1.e-12 .
Output your final approximate root (with all appropriate digits),
the residual, and how many iterations it took.
6. Write another code Newton (in double precision) implementing Newton's Method
(copy your Bisect code and modify).
Evaluation of F(x) and F'(x) should be done in a subprogram FCN(x).
The code should ask for input of: x0, TOL, maxIT
(and should print output similar to Bisect code).
Debug on a simple problem, like x2−3 = 0.
Then use it to find root of F(x) in [1,2] with TOL=1.e-12.
Now consider the problem of finding zeros of
G(x) = x−tan(x) near x=99 (radians).
7. Are there any ? How many ? How do you know ?
8. Use your Newton code to find the zero of G(x) closest to x=99 (radians)
to 9 decimals ( use TOL=10-9 , maxIT=20 ).
Output your final approximate root, the residual, and how many iterations it took.
Note: Extremely accurate starting values are needed for this function.
Plot it using gnuplot or Matlab or... or produce values of G(x) around x=99
to see the nature of the function. Simplest is gnuplot: plot [98.9:99] x-tan(x)
then adjust the range [98.9:99] to zoom in further.
9. Matlab (and Maple, and ...) have built-in programs to solve nonlinear (systems of) equations, among which
fzero ( help fzero for help )
This rootfinder needs an interval [a,b] on which the function changes sign;
it performs bisection and then inverse quadratic interpolation.
Try fzero on the function F(x) and on G(x).
In a single plain text file "lab2.txt", submit (on Canvas):
Lab2 , Name, Date
a. Answers to each of 1, 2, 3, 5 (show ONLY final result).
b. Answers to 7, 8 (show ONLY final result).
c. Answers to 9.
Compare with yours from 5 and 8 on accuracy, efficiency (# of itereations).
d. Your bisection code, CLEANED UP, and FCN function.
e. Your Newton-Raphson code, CLEANED UP, and FCN function.