Docsity

Numerical Methods Homework 5: Root Finding using Newton and Secant Methods, Assignments of Computer Science

Toggle sidebar
bg1
CS 257: Numerical Methods
Spring 2006
Homework, Set 5 Due Thursday February 23, 2006
(-) Start EACH PROBLEM on a SEPARATE piece of paper (This is important since we may
assign each problem to a different grader).
(-) Put your NETID and HW NUMBER on top of EACH PAGE clearly,
e.g. “netid: zamani hw2”.
(-) Write descriptive solutions. Comment your code!
(-) Include your curves/graphs (and other supporting materials) in your write-up.
(-) Don’t use handwritten code (unless you want to lose points), copy-paste your code into
your write-up or attach a proper print of code to your papers.
(-) Please write everything in a “portrait” style (not landscape).
(-) Please number problems according to numbers presented in the homework write-up that
appears on the course page, NOT according to the numbers in the textbook.
(-) Please type your homework or hand-write it legibly (but yet attach a print of your codes
to your handwritten stuff).
(-) Show that your code works (even if the problem doesn’t explicitly asks to test your code!)
(1) Develop a Newton function (newton.m) and a secant function (secant.m) with the following structure:
(note: you can use your Newton implementation from the previous week as a base)
1function [x , err f ,e rr x ,r ati o ,e rr a] = ne wt on (f un ,x 0 ,x tol , ft ol , ve rbo se , a)
2% n ewt o n Ne w ton ’s m eth o d to f ind a r oot f ( x ) = 0
3%
4% Sy no p si s :
5% [x , e r rf ,e rr x ,r a t io ] = ne wt o n ( fu n , x 0 )
6% [x , e r rf ,e rr x ,r a t io ] = ne wt o n ( fu n , x 0 , x to l )
7% [x , e r rf ,e rr x ,r a t io ] = ne wt o n ( fu n , x 0 , xt o l , f t ol )
8% [x , e r rf ,e rr x ,r a t io ] = ne wt o n ( fu n , x 0 , xt o l , f to l , ve r bo s e )
9% [x , e r rf ,e rr x ,r a ti o , er ra ] = new t o n ( fu n , x 0 , xt o l , f to l , ver b os e ,a )
10 %
11 % In pu t :
12 % f un = ( s tr i n g ) n a me o f m f il e t ha t re t u rn s f (x ) an d f ’( x ).
13 % x0 = in i t i a l g u ess
14 % x to l = ( o pt i on a l ) a b so lu t e to l er a nc e on x . D e fa ul t : xt ol =5 * ep s
15 % f to l = ( o pt i on a l ) a b so lu t e to l er a nc e on f ( x ). D ef a ul t : f to l = 5* e ps
16 % v er b o se = ( o p t io n a l ) f l ag . De f a ul t : ve r b os e = 1 , pr in t t ab l e
17 % a = ( o pt i on a l ) e xa c t r oo t
18 %
19 % Output:
20 % x = v ect o r of a p p r o x imat i o n s to t he r o ot
21 % err f = ve c t o r of re l a t i v e e r ror s i n f
22 % err x = ve c t o r of re l a t i v e a p p r o xim a t e e r r o r s in x
23 % ra t i o = r a t io of a p p r oxim a t e e r ror s in x
24 % err a = v e c t or of r e l ati v e e r ror s in x if a i s g i v en
(2) Consider the function
f(x) = x3+x−1 (1)
pf3

Partial preview of the text

Download Numerical Methods Homework 5: Root Finding using Newton and Secant Methods and more Assignments Computer Science in PDF only on Docsity!

CS 257: Numerical Methods

Spring 2006

Homework, Set 5 Due Thursday February 23, 2006

(-) Start EACH PROBLEM on a SEPARATE piece of paper (This is important since we may

assign each problem to a different grader).

(-) Put your NETID and HW NUMBER on top of EACH PAGE clearly,

e.g. “netid: zamani hw2”.

(-) Write descriptive solutions. Comment your code!

(-) Include your curves/graphs (and other supporting materials) in your write-up.

(-) Don’t use handwritten code (unless you want to lose points), copy-paste your code into

your write-up or attach a proper print of code to your papers.

(-) Please write everything in a “portrait” style (not landscape).

(-) Please number problems according to numbers presented in the homework write-up that

appears on the course page, NOT according to the numbers in the textbook.

(-) Please type your homework or hand-write it legibly (but yet attach a print of your codes

to your handwritten stuff).

(-) Show that your code works (even if the problem doesn’t explicitly asks to test your code!)

(1) Develop a Newton function (newton.m) and a secant function (secant.m) with the following structure:

(note: you can use your Newton implementation from the previous week as a base)

1 function [x , errf , errx , ratio , erra ] = newton ( fun , x0 , xtol , ftol , verbose , a )

2 % newton Newton ’ s method to find a root f ( x ) = 0

3 %

4 % Synopsis :

5 % [x , errf , errx , ratio ] = newton ( fun , x0 )

6 % [x , errf , errx , ratio ] = newton ( fun , x0 , xtol )

7 % [x , errf , errx , ratio ] = newton ( fun , x0 , xtol , ftol )

8 % [x , errf , errx , ratio ] = newton ( fun , x0 , xtol , ftol , verbose )

9 % [x , errf , errx , ratio , erra ] = newton ( fun , x0 , xtol , ftol , verbose , a )

10 %

11 % Input :

12 % fun = ( string ) name of mfile that returns f ( x ) and f ’( x ).

13 % x0 = initial guess

14 % xtol = ( optional ) absolute tolerance on x. Default : xtol =5* eps

15 % ftol = ( optional ) absolute tolerance on f ( x ). Default : ftol =5* eps

16 % verbose = ( optional ) flag. Default : verbose =1 , print table

17 % a = ( optional ) exact root

18 %

19 % Output :

20 % x = vector of approximations to the root

21 % errf = vector of relative errors in f

22 % errx = vector of relative approximate errors in x

23 % ratio = ratio of approximate errors in x

24 % erra = vector of relative errors in x if a is given

(2) Consider the function

f (x) = x

3

  • x − 1 (1)

(A) Find the exact real root, α, (to machine precision) using built-in function roots

(B) Use α and the functions developed above to fill in the table for both methods

k x k f (x k ) x k − x k− 1 α − x k

|α−xk |

|α−xk− 1 |

  • Run until |α − x k | < 1 e − 11
  • use x0 = −.07 for Newton’s Method and x0 = 0.0 and x 1 = 1.0 for the secant method

(C) From your data, determine the rate of convergence p so that

|α−xk |

|α−xk− 1 | p is constant in each case.

Show all of your work and verification for your data. Hint: Consider p = 2 for Newton and

p =

1+

√

5

2

for Secant.

(3) Use your Newton function to find the smallest root of

f (x) = − 4 .68999 + x(9.1389 + x(− 5 .56 + x)) (2)

(A) Fill in the table above with x 0

(B) What do you observe about convergence for Newton’s method in this case? Interpret the result.

(4) (NMM Chap. 7 #7) When describing computation as an abstract algorithmic level, it is convenient

to use matrix and vector operations. When the algorithm is translated into actual code, the matrix

and vector operations may or may not be used. In some cases they are very efficient, but in other

cases they are not. The following matrix-matrix multiplications are not usually carried out exactly

as written.

(a) Row scaling: B = DA, where D is a diagonal matrix

(b) Column scaling: B = AD, where D is a diagonal matrix

(c) Row permutation: B = P A, where P is a permutation matrix

(d) Column permutation: B = AP , where P is a permutation matrix

Describe a more efficient procedure for each of these operations. Compare the flop count for the

matrix-matrix multiplication with the alternative procedure you recommend.

(5) (NMM Chap. 7 #10) Compare the efficiency of norm calculations in MATLAB with the following

statements (note the use of commas, not semicolons, between the tic, norm(u,2), and toc):

u = linspace(1,2,32000);

tic, norm(u,2), toc

tic, norm(u,1), toc

tic, norm(u,inf), toc

You should repeat these calculations for three times and report the average time for the last two

runs.

(6) (NMM Chap. 7 #39) Given the matrices

A =

[

cos(θ) sin(θ)

− sin(θ) cos(θ)

]

, and B =

[

cos(θ) sin(θ)

sin(θ) − cos(θ)

]