School of Mathematics and Statistics MAST30028 Numerical Methods & Scientific Computing Week 6
Drag and drop the folder Week6 from L: \MAST30028 to C:\…\MATLAB and include it in the path. Now MATLAB knows how to find the files in Week6. The folder includes a pdf file of Chapter 2 in Moler’s textbook (lu.pdf).
1 Newton’s method
This relates to material in Lecture 10.
Exercise Set 1
a. Show that Newton’s method
when applied to the function f (x) = x2 − a gives an algorithm you have already coded.
b. A function to implement Newton’s method is given is Newton.m. Read the code and make sure you understand how it works. What convergence criterion does it use? How does it differ from Bisection.m?
To see an example of usage, run driverNewton.
c. Investigate the roots of the following functions, with the specified initial values x0 at least.
• cos(x)−x
• loge(x)−exp(−x)=0 • x3−7×2+14x−8=0
Usex0 =1,3,6 Usex0 =2
Usex0 =1.1,1.2…1.9
d. You can visualize Newton’s method by a code such as vizNewton.m.
Use vizNewton.m to explain some of the behaviour seen in part c. Explain the M-file (Challenging)
Open MySqrt.m and try to figure out exactly what every MATLAB command is doing. Hint: what has this to do with Newton’s method?
Run the script ShowMySqrt.m to see how it performs over, say, the interval [0.1, 5].
xn+1 = xn − f(xn) f′(xn)
1
2 Secant method
This relates to material in Lecture 10.
Exercise Set 2
The secant method can be derived by replacing f′(xn) in Newton’s method by the forward difference approxi-
mation
f′(xn) ≈ f(xn) − f(xn−1) xn − xn−1
Note that this results in a second-order recurrence relation i.e. two previous iterates are required to generate the next iterate. This means that 2 initial iterates are required, not necessarily bracketing.
3
a. b. c.
By modifying Newton.m or otherwise, write your own function for the secant method. Test your program by finding the root of the function cos x − x.
Now see how the secant method performs in finding the root of loge x − exp(−x) using x0 = 1, x1 = 2. Compare with the performance of the other two methods.
Matrices in MATLAB
Work through or watch while I work through the M-file lab6.m introducing 2D arrays (matrices) in MATLAB. Then try the following:
Exercise set 3
a. How would you create the dot product of 2 vectors in MATLAB? How would you compute the Euclidean length of a vector?
Hint: don’t use length!
b. How could you create a random (entries uniformly distributed) symmetric matrix? a random skew-
symmetric matrix?
c. A magic square is a an n×n matrix in which each integer 1,2,…n2 appears once and for which all the row, column and diagonal sums are equal. MATLAB has a command magic that returns magic squares. Check its output at a few sizes and use MATLAB to verify the summation property.
Hint: flipud or similar commands may be useful.
d. For a 2D analogue of linspace, look up help for meshgrid. It creates matrices that are useful for creating
surface plots of functions of 2 variables. Plot the function
f (x1 , x2 ) = min(1 − |x1 |, 1 − |x2 |)
on [0, 1] × [0, 1] using mesh or surf.
e. Try out
fprintf(’%d\n’, eye(4,2)) fprintf(’%d %6.2f\n’, eye(4,2))
to see how fprintf handles matrix input.
Exercise Set 4
Run the function twodriver.m to see the effect of not pivoting in a rather extreme 2 × 2 case.
2