Industrial Math - Alexiades
Lab 2: Newton rootfinder
(always read the entire Lab before you start working on it...)
1. Implement Newton's method to find a root of a given function F(x), i.e. solve F(x)=0.
The main program should read in:
x0 = initial guess for the root,
maxIT = maximum number of iterations to be performed,
TOL = tolerance for testing convergence.
and should call the Newton rootfinder (see below).
Your program should output the input values, and then the iterates
(neatly, in columns):
n xn F(xn)
(with formats: %d %f %e )
and, upon convergence, the root.
The values of F(x) and F'(x) should be computed in a single
function subprogram FCN(xn) called by the rootfinder
[ in Matlab: [Fn, DFn] = FCN(xn) ].
A good way to code Newton rootfinder and decide convergence
is something like this (pseudocode):
subprogram Newton1D( x0, TOL, maxIT )
xn = x0
dx = 1000.0 [something big]
print '# n xn Fn' [labels for values]
for n=1:maxIT
call FCN( xn, Fn, DFn ) [returns Fn=F(xn), DFn=F'(xn)]
print: n xn Fn [use appropriately formated printing, in columns]
if( abs(dx) < TOL ) then
if( abs(Fn) < TOL ) then
print: 'DONE: root=',xn,' F=',Fn,' in ',n,' iters'
break out of the loop [how depends on the language...]
else
print: 'STUCK: dx < TOL BUT residual =', Fn,' > TOL'
break out of the loop
endif
endif
dx = −Fn/DFn [take Newton step]
xn = xn + dx
endfor
print: 'BAD: reached maxIT'
END subprogram
2. Debug your code by finding SQRT(3) as root of F(x) = x2 − 3.
Try a couple of different x0's, with maxIT=20, TOL=1.e-14 (full double precision).
[ While debugging, can set exact=sqrt(3) and also
print the error: xn − exact ]
3. Create a PLAIN TEXT file "Lab2.txt" containing:
M475 Lab2: Name, Date
=============================================== (separator line)
the input and output of ONE run of your code from part 2.
=============================================== (separator line)
your code (may copy with the mouse, but indent appropriately)
4. Upload "Lab2.txt" on Canvas (under Lab2).