+ All Categories
Home > Documents > MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A ›...

MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A ›...

Date post: 06-Jun-2020
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
25
2.1 The Bisection Method Solutions by Jon Lo Kim Lin - Fall 2014 MATH 104A HW 03 SOLUTION KEY You are encouraged to collaborate with your classmates and utilize internet resources provided you adhere to the following rules: (i) You read the problem carefully and make a serious effort to solve it before you ask for help. (ii) You write the solution in your own words/ handwriting/ L A T E X document. (iii) You credit the people/books/websites who helped you, especially if they showed you how to do most of the problem. (iv) The TA is not omniscient. This course is aimed at developing your deductive reasoning and incredulity. For those who need a quick primer on programming, I highly recommend the python course by http://www.codecademy.com. It’s free and they claim you can learn python in a mere 13 hours. 2.1 The Bisection Method 1. Use the bisection method to find p 3 for f (x )= x - cos(x ) on [0, 1]. SOLUTION. The following python code reveals that p 3 =0.625 with f (p 3 )= -0.0203937044631. # Purpose : Performs N -many iterations of the bisection method #============================================================== # Record of revisions : # # Date Programmer Description of change # ======== ================= =================== # 10/22/14 Jon T. Lo Kim Lin Original code # 11/03/14 Jon T. Lo Kim Lin Added the sgn function # for improved stability # #============================================================== from math import sqrt , cos # Only import what you need def f(x): # The function of interest return sqrt(x) - cos(x) def sgn(x): # The signum function if x < 0.0: return - 1.0 elif x == 0.0: return 0.0 else : return 1.0 1
Transcript
Page 1: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.1 The Bisection Method Solutions by Jon Lo Kim Lin - Fall 2014

MATH 104A HW 03 SOLUTION KEY

You are encouraged to collaborate with your classmates and utilize internet resources provided youadhere to the following rules:

(i) You read the problem carefully and make a serious effort to solve it before you ask for help.

(ii) You write the solution in your own words/ handwriting/ LATEX document.

(iii) You credit the people/books/websites who helped you, especially if they showed you how todo most of the problem.

(iv) The TA is not omniscient. This course is aimed at developing your deductive reasoning andincredulity.

For those who need a quick primer on programming, I highly recommend the python course byhttp://www.codecademy.com. It’s free and they claim you can learn python in a mere 13 hours.

2.1 The Bisection Method

1. Use the bisection method to find p3 for f (x) =√x − cos(x) on [0, 1].

SOLUTION. The following python code reveals that p3 = 0.625 with f (p3) = −0.0203937044631.

# Purpose : Pe r fo rms N−many i t e r a t i o n s o f the b i s e c t i o n method#==============================================================# Record o f r e v i s i o n s :## Date Programmer D e s c r i p t i o n o f change# ======== ================= ===================# 10/22/14 Jon T. Lo Kim L i n O r i g i n a l code# 11/03/14 Jon T. Lo Kim L i n Added the sgn f u n c t i o n# f o r improved s t a b i l i t y##==============================================================from math import s q r t , cos # Only impo r t what you need

def f ( x ) : # The f u n c t i o n o f i n t e r e s tr e tu rn s q r t ( x ) − cos ( x )

def sgn ( x ) : # The s ignum f u n c t i o ni f x < 0 . 0 :

r e tu rn −1.0e l i f x == 0 . 0 :

r e tu rn 0 .0e l s e :

r e tu rn 1 .0

1

Page 2: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.1 The Bisection Method Solutions by Jon Lo Kim Lin - Fall 2014

def b i s e c t i on_N ( a , b , N ) :i = 1 # I n i t i a l i z e the i n d e xwh i l e i <= N:

p = a + ( b − a ) / 2 .0 # Compute p_ii = i + 1 # Inc r emen t the i n d e xi f sgn ( f ( a ) ) ∗ sgn ( f ( p ) ) > 0 :

a = pe l s e :

b = py = [ i − 1 , p ]

r e tu rn y

# I n i t i a l i z e the n e c e s s a r y c o n s t a n t sa = 0 .0b = 1 .0

x = b i s e c t i on_N ( a , b , 3)# Wri te the ou tpu t to the c o n s o l ep r i n t "p%s ␣=␣" % x [ 0 ] , "␣ f ( p%s ) ␣=␣" % x [ 1 ] , f ( x [ 1 ] )

6. Use the bisection method to find solutions, accurate to within 10−5 for the following problems.

(a) 3x − ex = 0 for 1 ≤ x ≤ 2.(b) x + 3cos x − ex = 0 for 0 ≤ x ≤ 1(c) x2 − 4x + 4− ln x = 0 for 1 ≤ x ≤ 2 and 2 ≤ x ≤ 4(d) x + 1− 2 sinπx = 0 for 0 ≤ x ≤ 0.5 and 0.5 ≤ x ≤ 1

SOLUTION. The following python code reveals that

For fa(x) = 3x − ex p17 = 1.5121383667 on [1,2].

For fb(x) = x + 3cos x − ex p17 = 0.976768493652 on [0,1].

For fc(x) = x2 − 4x + 4− ln x p17 = 1.4123916626 on [1,2].

For fc(x) = x2 − 4x + 4− ln x p18 = 3.05710601807 on [2,4].

For fd(x) = x + 1− 2 sinπx p16 = 0.206031799316 on [0,0.5].

For fd(x) = x + 1− 2 sinπx p16 = 0.681968688965 on [0.5,1].

2

Page 3: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.1 The Bisection Method Solutions by Jon Lo Kim Lin - Fall 2014

# Purpose : Imp l ement s the b i s e c t i o n a l g o r i t hm 2 .1## F i nd s the s o l u t i o n to f ( x ) = 0 g i v e n the# con t i n u o u s f u n c t i o n f on the i n t e r v a l [ a , b ] ,# where f ( a ) and f ( b ) have o p p o s i t e s i g n s#==============================================================# Record o f r e v i s i o n s :## Date Programmer D e s c r i p t i o n o f change# ======== ================= ===================# 10/22/14 Jon T. Lo Kim L i n O r i g i n a l code##==============================================================from math import exp , cos , s i n , l og , p i # Only impo r t what you need

# The f u n c t i o n s o f i n t e r e s tdef f_a ( x ) :

r e tu rn 3 .0 ∗ x − exp ( x )def f_b ( x ) :

r e tu rn x + 3 .0 ∗ cos ( x ) − exp ( x )def f_c ( x ) :

r e tu rn x ∗∗ 2 − 4 .0 ∗ x + 4 .0 − l o g ( x )def f_d ( x ) :

r e tu rn x + 1 .0 − 2 .0 ∗ s i n ( p i ∗ x )

# Our l i s t o f f u n c t i o n sf _ l i s t = [ f_a , f_b , f_c , f_c , f_d , f_d ]

# Our l i s t o f l e f t i n t e r v a l end−p o i n t sa = [ 1 . 0 , 0 . 0 , 1 . 0 , 2 . 0 , 0 . 0 , 0 . 5 ]

# Our l i s t o f r i g h t i n t e r v a l end−p o i n t sb = [ 2 . 0 , 1 . 0 , 2 . 0 , 4 . 0 , 0 . 5 , 1 . 0 ]

def sgn ( x ) : # The s ignum f u n c t i o ni f x < 0 . 0 :

r e tu rn −1.0e l i f x == 0 . 0 :

r e tu rn 0 .0e l s e :

r e tu rn 1 .0

def b i s e c t i o n ( a , b , TOL, N, f ) :i = 1 # I n i t i a l i z e the i n d e xFA = f ( a )

3

Page 4: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.1 The Bisection Method Solutions by Jon Lo Kim Lin - Fall 2014

wh i l e i <= N:p = a + ( b − a ) / 2 .0 # Compute p_iFP = f ( p )i f FP == 0 .0 or ( b − a ) / 2 .0 < TOL:

y = ( i , p )r e tu rn ys y s . e x i t ( )

i = i + 1 # Inc r emen t the i n d e xi f sgn (FA) ∗ sgn (FP) > 0 :

a = pFA = FP

e l s e :b = p

r e tu rn "Method␣ f a i l e d ␣ a f t e r ␣%s ␣ i t e r a t i o n s . " % \(N)

# I n i t i a l i z e the n e c e s s a r y c o n s t a n t sTOL = 10 ∗∗ (−5) # Set the t o l e r a n c eN = 10 ∗∗ 2 # Set the maximum number o f i t e r a t i o n s

f u n c t i o n s = [ " 3 .0 ␣∗␣x␣−␣ exp ( x ) " , " x +3.0∗ cos ( x)−exp ( x ) " , \"x ∗∗2−4.0∗ x+4.0− l o g ( x ) " , " x ∗∗2−4.0∗ x+4.0− l o g ( x ) " , \"x +1.0−2.0∗ s i n ( p i ∗ x ) " , " x +1.0−2.0∗ s i n ( p i ∗ x ) " ]

f o r i , f i n enumerate ( f _ l i s t ) :x = b i s e c t i o n ( a [ i ] , b [ i ] , TOL, N, f _ l i s t [ i ] )# P r i n t to the c o n s o l ep r i n t "For ␣ f ( x ) ␣=␣" , f u n c t i o n s [ i ] , "␣p%s ␣=␣" % ( x [ 0 ] ) , x [ 1 ] , \"␣on␣[%s ,%s ] . " % ( a [ i ] , b [ i ] )

12. Find an approximation to√3 correct within 10−4 using he bisection algorithm. Hint: Consider

f (x) = x2 − 3.

SOLUTION. The following python code reveals that p15 = 1.73199462891 with absoluteerror 5.61786626272e-05

# Purpose : Imp l ement s the b i s e c t i o n a l g o r i t hm 2 .1## F i nd s the s o l u t i o n to f ( x ) = 0 g i v e n the# con t i n u o u s f u n c t i o n f on the i n t e r v a l [ a , b ] ,# where f ( a ) and f ( b ) have o p p o s i t e s i g n s#==============================================================# Record o f r e v i s i o n s :## Date Programmer D e s c r i p t i o n o f change

4

Page 5: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.1 The Bisection Method Solutions by Jon Lo Kim Lin - Fall 2014

# ======== ================= ===================# 10/22/14 Jon T. Lo Kim L i n O r i g i n a l code##==============================================================from math import s q r t , f a b s # Only impo r t what you need

def f ( x ) : # The f u n c t i o n o f i n t e r e s tr e tu rn x ∗∗ 2 − 3

def sgn ( x ) : # The s ignum f u n c t i o ni f x < 0 . 0 :

r e tu rn −1.0e l i f x == 0 . 0 :

r e tu rn 0 .0e l s e :

r e tu rn 1 .0

def b i s e c t i o n ( a , b , TOL, N) :i = 1 # I n i t i a l i z e the i n d e xFA = f ( a )wh i l e i <= N:

p = a + ( b − a ) / 2 .0 # Compute p_iFP = f ( p )i f FP == 0 .0 or ( b − a ) / 2 .0 < TOL:

y = [ p , i ]r e tu rn ys y s . e x i t ( )

i = i + 1 # Inc r emen t the i n d e xi f sgn (FA) ∗ sgn (FP) > 0 :

a = pFA = FP

e l s e :b = p

r e tu rn "Method␣ f a i l e d ␣ a f t e r ␣%11.7 e␣ i t e r a t i o n s . " % \(N)

# I n i t i a l i z e the n e c e s s a r y c o n s t a n t sa = 0 .0b = 2 .0TOL = 10 ∗∗ (−4) # The e r r o r t o l e r a n c eN = 10 ∗∗ 3 # At most 1000 i t e r a t i o n s

x = b i s e c t i o n ( a , b , TOL, N)# P r i n t the ou tpu t to the c o n s o l ep r i n t x

5

Page 6: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.1 The Bisection Method Solutions by Jon Lo Kim Lin - Fall 2014

# P r i n t the e r r o r to the c o n s o l ep r i n t "The␣ a b s o l u t e ␣ e r r o r ␣=" , f a b s ( x [ 1 ] − s q r t ( 3 . 0 ) )

19. A trough of length L has a cross section in the shape of a semicircle with radius r. (See theaccompanying figure.) When filled with water to within a distance h of the top, the volume Vof water is

V = L(0.5πr 2 − r 2 arcsin(h/r)− h(r 2 − h2)1/2

).

Suppose L = 10ft, r = 1ft, and V = 12.4ft3. Find the depth of water in the trough to within0.01ft.

SOLUTION. From the image supplied in our textbook it is plain that L is the length of thetrough, r is the radius of the semicircle cross section of the trough, V is the volume of water inthe trough, and h is the distance from the water level to the top of the trough. Let d denotethe depth of the water. Since the radius of the trough is given to be r = 1ft, the depth of thewater will be given by

d = 1− h. (1)

Plugging in the values for the length, radius, and volume given above, we have the followingequation to solve for h :

12.4 = 10(0.5π − arcsin(h)− h(1− h2)1/2

).

Therefore, solving for h is equivalent to identifying a zero of the function

f (h) = 12.4− 10(0.5π − arcsin(h)− h(1− h2)1/2

).

The trough is full when h = 0, in other words, when

f (0) ≈ −3.30796326795 < 0

When h = 0.5,f (0.5) ≈ 6.25815150696 > 0,

by the intermediate value theorem it must be the case that h is confined to the interval[0, 0.5].We proceed to solve for h by method of bisection with the error tolerance 10−2 = 0.01.The following python code below reveals that

p6 = 0.1640625

satisfies the required error tolerance. Thus, by (1) we see that

d = 1− 0.1640625 = 0.8359375

6

Page 7: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.1 The Bisection Method Solutions by Jon Lo Kim Lin - Fall 2014

# Purpose : Imp l ement s the b i s e c t i o n a l g o r i t hm 2 .1##==============================================================# Record o f r e v i s i o n s :## Date Programmer D e s c r i p t i o n o f change# ======== ================= ===================# 11/01/14 Jon T. Lo Kim L i n O r i g i n a l code##==============================================================from math import s q r t , p i , a s i n # Only impo r t what you need

def f ( h ) : # The f u n c t i o n o f i n t e r e s tf 1 = 0 .5 ∗ p if 2 = a s i n ( h )f 3 = h ∗ s q r t (1 − ( h ∗∗ 2) )r e tu rn 12 .4 − 10 .0 ∗ ( f 1 − f 2 − f 3 )

def sgn ( x ) : # The s ignum f u n c t i o ni f x < 0 . 0 :

r e tu rn −1.0e l i f x == 0 . 0 :

r e tu rn 0 .0e l s e :

r e tu rn 1 .0

def b i s e c t i o n ( a , b , TOL, N) :i = 1 # I n i t i a l i z e the i n d e xFA = f ( a )wh i l e i <= N:

p = a + ( b − a ) / 2 .0 # Compute p_iFP = f ( p )i f FP == 0 .0 or ( b − a ) / 2 .0 < TOL:

y = [ i , p ]r e tu rn ys y s . e x i t ( )

i = i + 1 # Inc r emen t the i n d e xi f sgn (FA) ∗ sgn (FP) > 0 :

a = pFA = FP

e l s e :b = p

r e tu rn "Method␣ f a i l e d ␣ a f t e r ␣%s ␣ i t e r a t i o n s . " % \(N)

7

Page 8: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.2 Fixed-point Iteration Solutions by Jon Lo Kim Lin - Fall 2014

# I n i t i a l i z e the n e c e s s a r y c o n s t a n t sa = 0 .0b = 0 .5TOL = 10 ∗∗ (−2) # The e r r o r t o l e r a n c eN = 10 ∗∗ 3 # At most 1000 i t e r a t i o n s

x = b i s e c t i o n ( a , b , TOL, N)# P r i n t the ou tpu t to the c o n s o l ep r i n t "p%s ␣=␣" % x [ 0 ] , x [ 1 ] , " depth ␣=␣" , 1 . 0 − x [ 1 ]

2.2 Fixed-point Iteration

1. Use algebraic manipulation to show that each of the following functions has a fixed point at pprecisely when f (p) = 0, where f (x) = x4 + 2x2 − x − 3.

(a) g1(x) = (3 + x − 2x2)1/4 (b) g2(x) =

(x + 3− x4

2

)1/2

(c) g3(x) =

(x + 3

x2 + 2

)1/2(d) g4(x) =

3x4 + 2x2 + 3

4x3 + 4x − 1

SOLUTION. For i = 1, 2, 3, 4, we have a fixed point when gi(p) = p, that is, if and only if,

for g1, p =(3 + p − 2p2

)1/4 ⇐⇒ p4 = 3 + p − 2p2,

for g2, p =

(p + 3− p4

2

)1/2⇐⇒ 2p2 = p + 3− p4 ⇐⇒ p4 = 3 + p − 2p2,

for g3, p =

(p + 3

p2 + 2

)1/2⇐⇒ p2(p2 + 2) = p + 3 ⇐⇒ p4 = 3 + p − 2p2,

for g4, p =3p4 + 2p2 + 3

4p3 + 4p − 1 ⇐⇒ 4p4 + 4p2 − p = 3p4 + 2p2 + 3,

or all equivalently, p4 + 2p2 − p − 3 = 0. Indeed, by the Fundamental Theorem of Algebra,accounting for multiplicity, our functions gi have precisely 4 roots, hence; precisely 4 fixedpoints. �

3. The following four methods are proposed to compute 211/3. Rank them in order, based ontheir apparent speed of convergence, assuming p0 = 1.

(a) pn =20pn−1 + 21/p

2n−1

21(b) pn = pn−1 −

p3n−1 − 213p2n−1

(c) pn = pn−1 −p4n−1 − 21pn−1p2n−1 − 21

(d) pn =

(21

pn−1

)1/2

8

Page 9: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.2 Fixed-point Iteration Solutions by Jon Lo Kim Lin - Fall 2014

SOLUTION. For i = 1, 2, 3, 4 let gi(x) denote the contractive function on the interval [a, b].A sufficient but not at all a necessary condition for the convergence of the fixed pointiteration algorithm is that |g′i (x)| < 1 on (a, b). It is now apparent that it remains to minimizethe derivatives.

(a) For g1(x) =20x + 21/x2

21, we have g′1(x) =

20

21−2

x3, and so

∣∣g′1(211/3)∣∣ = ∣∣∣∣2021 − 221∣∣∣∣ = 67 = 0.857143.

(b) For g2(x) = x −x3 − 213x2

, we have g′2(x) =2

3−14

x3, and so

∣∣g′2(211/3)∣∣ = ∣∣∣∣23 − 1421∣∣∣∣ = 0.

(c) For g3(x) = x −x4 − 21xx2 − 21 , we have g′3(x) =

−63x2 + 84x3 + x4 − 2x5

441− 42x2 + x4 , and so

∣∣g′3(211/3)∣∣ = ∣∣∣∣1764 + 21 3√21− (105)212/3441 + 21 3√21− (42)212/3

∣∣∣∣ ≈ 5.70559.(d) For g4(x) =

(21

x

)1/2, we have g′4(x) = −

1

2

√21

x3/2, and so

∣∣g′4(211/3)∣∣ = ∣∣∣∣−12√21√21

∣∣∣∣ = 12 = 0.5.Armed with these results we see that ranking the order of convergence is: g2 as the fastest,followed by g4, then g1, and finally, g3 as the slowest. �

7. Use Theorem 2.3 to show that g(x) = π+0.5 sin(x/2) has a unique fixed point on [0, 2π]. Usefixed-point iteration to find an approximation to the fixed point that is accurate to within 10−2.Use Corollary 2.5 to estimate the number of iterations required to achieve 10−2 accuracy, andcompare this theoretical estimate to the number actually needed.

SOLUTION. Since −1 ≤ sin x ≤ 1 for all x, we see that π − 0.5 ≤ g(x) ≤ π + 0.5 for everyx. More specifically, 0 ≤ g(x) ≤ 2π for all x, thus, g satisfies condition (a) of Theorem 2.2.Moreover,

g′(x) = 0.25 cos x2,

in other words, |g′(x)| ≤ 0.25 for all x and it follows that condition (b) is satisfied as soon aswe take k = 1/4. Therefore, g indeed admits a unique fixed point as desired.

The following python code reveals that by choosing the midpoint p0 = π we obtain thesequence which rounds off to p ≈ 3.63, and this accuracy is already attained at p2. Onceformula in Corollary 2.4 states that the error would theoretically be at most

|pn − p| ≤1

4nmax{p0 − a, b − p0} =

π

4n.

9

Page 10: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.2 Fixed-point Iteration Solutions by Jon Lo Kim Lin - Fall 2014

Therefore, theoretically, if we imposed a tolerance of at most 10−2 = 0.01, we would choosen so that

π

4n≤ 0.01,

or equivalently,n ≥ 4.

The other formula in Corollary 2.4 tells us to use p1 − p0, and this formula begets:

|pn − p| ≤4

3

1

4n(0.5) = 0.01,

or equivalently,n > 3.

The following python code demonstrates the implementation on the interval [3, 2π]:

# Purpose : Imp l ement s the f i x e d −p o i n t a l g o r i t hm 2 .2## F i nd s a s o l u t i o n to p = g ( p ) g i v e n an# i n i t i a l a p p r o x ima t i o n p_0 :##==============================================================# Record o f r e v i s i o n s :## Date Programmer D e s c r i p t i o n o f change# ======== ================= ===================# 11/03/14 Jon T. Lo Kim L i n O r i g i n a l code##==============================================================from math import s i n , \

s q r t , f a b s # Only impo r t what you need

# The f u n c t i o n o f i n t e r e s tdef g ( x ) :

r e tu rn p i + 0 .5 ∗ s i n ( x / 2 . 0 )

def f i x e d_po i n t ( p0 , TOL, N, g ) :i = 1 # I n i t i a l i z e the i n d e xwh i l e i <= N:

p = g ( p0 ) # Compute p_ii f f a b s ( p − p0 ) < TOL:

y = [ i , p ]r e tu rn ys y s . e x i t ( )

e l s e :i = i + 1 # Inc r emen t the i n d e xp0 = p # Update p_0

r e tu rn "The␣method␣ f a i l e d ␣ a f t e r ␣%s ␣ i t e r a t i o n s , ␣N␣=␣" % \

10

Page 11: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.2 Fixed-point Iteration Solutions by Jon Lo Kim Lin - Fall 2014

(N) , N

# I n i t i a l i z e the n e c e s s a r y c o n s t a n t sa = 3 .0b = 2 .0 ∗ p ip0 = a # Set the i n i t i a l a p p r o x ima t i o nTOL = 10 ∗∗ (−2) # Set the t o l e r a n c eN = 10 # Set the maximum number o f i t e r a t i o n s

x = f i x e d_po i n t ( p0 , TOL, N, g )# P r i n t to the c o n s o l ep r i n t "For ␣g ( x ) ␣=␣" , "␣p%s ␣=␣" % ( x [ 0 ] ) , \

x [ 1 ] , "␣on␣[%s ,%s ] . " % ( a , b )

11. For each of the following equations, determine an interval [a, b] on which fixed-point iterationwill converge. Estimate the number of iterations necessary to obtain approximations accurateto within 10−5, and perform the calculations.

(a) x =2− ex + x2

3(b) x =

5

x2+ 2

(c) x = (ex/3)1/2 (d) x = 5−x

(e) x = 6−x (f) x = 0.5 (sin x + cos x)

SOLUTION. The following python code reveals that

For ga(x) = (2− ex + x2)/3 p9 = 0.257531806268 on [0, 1].

For gb(x) = 5/x2 + 2 p17 = 2.69064989328 on [2.5, 3].

For gc(x) = (ex/3)1/2 p14 = 0.909999383517 on [0.25, 1].

For gd(x) = 5−x p39 = 0.46962520372 on [0.3, 0.7].

For ge(x) = 6−x p42 = 0.448059075556 on [0.3, 0.6].

For gf (x) = 0.5(sin x + cos x) p6 = 0.704811677201 on [0, 1].

# Purpose : Imp l ement s the f i x e d −p o i n t a l g o r i t hm 2 .2## F i nd s a s o l u t i o n to p = g ( p ) g i v e n an# i n i t i a l a p p r o x ima t i o n p_0 :##==============================================================

11

Page 12: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.2 Fixed-point Iteration Solutions by Jon Lo Kim Lin - Fall 2014

# Record o f r e v i s i o n s :## Date Programmer D e s c r i p t i o n o f change# ======== ================= ===================# 11/03/14 Jon T. Lo Kim L i n O r i g i n a l code##==============================================================from math import exp , cos , s i n , \

s q r t , f a b s # Only impo r t what you need

# The f u n c t i o n s o f i n t e r e s tdef g_a( x ) :

r e tu rn ( 2 . 0 − exp ( x ) + ( x ∗∗ 2) ) / 3 .0def g_b( x ) :

r e tu rn 5 .0 / ( x ∗∗ 2) + 2 .0def g_c( x ) :

r e tu rn s q r t ( exp ( x ) / 3 . 0 )def g_d( x ) :

r e tu rn 5 ∗∗ (−x )def g_e( x ) :

r e tu rn 6 ∗∗ (−x )def g_f ( x ) :

r e tu rn 0 .5 ∗ ( s i n ( x ) + cos ( x ) )

# Our l i s t o f f u n c t i o n sg_ l i s t = [ g_a , g_b , g_c , g_d , g_e , g_f ]

# Our l i s t o f l e f t i n t e r v a l end−p o i n t sa = [ 0 . 0 , 2 . 5 , 0 . 25 , 0 . 3 , 0 . 3 , 0 . 0 ]

# Our l i s t o f r i g h t i n t e r v a l end−p o i n t sb = [ 1 . 0 , 3 . 0 , 1 . 0 , 0 . 7 , 0 . 6 , 1 . 0 ]

def f i x e d_po i n t ( p0 , TOL, N, g ) :i = 1 # I n i t i a l i z e the i n d e xwh i l e i <= N:

p = g ( p0 ) # Compute p_ii f f a b s ( p − p0 ) < TOL:

y = [ i , p ]r e tu rn ys y s . e x i t ( )

e l s e :i = i + 1 # Inc r emen t the i n d e xp0 = p # Update p_0

r e tu rn "The␣method␣ f a i l e d ␣ a f t e r ␣%s ␣ i t e r a t i o n s , ␣N␣=␣" % \

12

Page 13: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.3 Newton’s Method and Its Extensions Solutions by Jon Lo Kim Lin - Fall 2014

(N) , N

# I n i t i a l i z e the n e c e s s a r y c o n s t a n t sTOL = 10 ∗∗ (−5) # Set the t o l e r a n c eN = 10 ∗∗ 3 # Set the maximum number o f i t e r a t i o n s

f u n c t i o n s = [ " ( 2 . 0 ␣−␣ exp ( x ) ␣+␣ ( x␣∗∗␣ 2) ) ␣/␣ 3 .0 " , \" 5 .0 ␣/␣ ( x␣∗∗␣ 2) ␣+␣ 2 .0 " , \" s q r t ( exp ( x ) ␣/␣ 3 . 0 ) " , \"5␣∗∗␣(−x ) " , "6␣∗∗␣(−x ) " , \" 0 .5 ␣∗␣ ( s i n ( x ) ␣+␣ cos ( x ) ) " ]

f o r i , g i n enumerate ( g_ l i s t ) :x = f i x e d_po i n t ( a [ i ] , TOL, N, g_ l i s t [ i ] )# P r i n t to the c o n s o l ep r i n t "For ␣g ( x ) ␣=␣" , f u n c t i o n s [ i ] , "␣p%s ␣=␣" % ( x [ 0 ] ) , \

x [ 1 ] , "␣on␣[%s ,%s ] . " % ( a [ i ] , b [ i ] )

2.3 Newton’s Method and Its Extensions

1. Let f (x) = x2 − 6 and p0 = 1. Use Newton’s method to find p2.

5. Use Newton’s method to find solutions accurate to within 10−4 for the following problems.

(a) x3 − 2x2 − 5 = 0, [1, 4] (b) x3 + 3x2 − 1 = 0, [−3,−2]

(c) x − cos x = 0, [0, π/2] (d) x − 0.8− 0.2 sin x = 0, [0, π/2]

SOLUTION.

For fa(x) = x3 − 2x2 − 5 p5 = 2.69064744852 on [1, 4].

For fb(x) = x3 + 3x2 − 1 p3 = −2.87938524484 on [−3,−2].

For fc(x) = x − cos x p4 = 0.739085133385 on [0, π/2].

For fd(x) = x − 0.8− 0.2 sin x p4 = 0.964333887695 on [0, π/2].

The tableau above was generated by the following python code

# Purpose : Imp l ement s Newton ’ s method a l g o r i t hm 2 .3## F i nd s a s o l u t i o n to f ( x ) = 0 g i v e n an# i n i t i a l a p p r o x ima t i o n p_0 :##==============================================================

13

Page 14: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.3 Newton’s Method and Its Extensions Solutions by Jon Lo Kim Lin - Fall 2014

# Record o f r e v i s i o n s :## Date Programmer D e s c r i p t i o n o f change# ======== ================= ===================# 11/03/14 Jon T. Lo Kim L i n O r i g i n a l code##==============================================================from math import p i , cos , s i n , f a b s # Only impo r t what you need

# The f u n c t i o n s o f i n t e r e s tdef f_a ( x ) :

r e tu rn ( x ∗∗ 3) − 2 .0 ∗ ( x ∗∗ 2) − 5 .0def f_b ( x ) :

r e tu rn ( x ∗∗ 3) + 3 .0 ∗ ( x ∗∗ 2) − 1def f_c ( x ) :

r e tu rn x − cos ( x )def f_d ( x ) :

r e tu rn x − 0 .8 − 0 .2 ∗ s i n ( x )

# The c o r r e s p o n d i n g d e r i v a t i v e sdef df_a ( x ) :

r e tu rn 3 .0 ∗ ( x ∗∗ 2) − 4 .0 ∗ xdef df_b ( x ) :

r e tu rn 3 .0 ∗ ( x ∗∗ 2) + 6 .0 ∗ xdef df_c ( x ) :

r e tu rn 1 + s i n ( x )def df_d ( x ) :

r e tu rn 1 .0 − 0 .2 ∗ cos ( x )

# Our l i s t o f f u n c t i o n sf _ l i s t = [ f_a , f_b , f_c , f_d ]

# Our l i s t o f c o r r e s p o n d i n g d e r i v a t i v e sd f _ l i s t = [ df_a , df_b , df_c , df_d ]

# Our l i s t o f l e f t i n t e r v a l end−p o i n t sa = [ 1 . 0 , −3.0 , 0 . 0 , 0 . 0 ]

# Our l i s t o f r i g h t i n t e r v a l end−p o i n t sb = [ 4 . 0 , −2.0 , p i / 2 . 0 , p i / 2 . 0 ]

# Our l i s t o f i n i t i a l a p p r o x ima t i o n sp_int = [ 2 . 0 , −3.0 , 0 . 0 , 0 . 0 ]

def newton ( p0 , TOL, N, f , d f ) :

14

Page 15: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.3 Newton’s Method and Its Extensions Solutions by Jon Lo Kim Lin - Fall 2014

i = 1 # I n i t i a l i z e the i n d e xwh i l e i <= N:

p = p0 − f ( p0 ) / d f ( p0 ) # Compute p_ii f f a b s ( p − p0 ) < TOL:

y = [ i , p ]r e tu rn ys y s . e x i t ( )

e l s e :i = i + 1 # Inc r emen t the i n d e xp0 = p # Update p_0

r e tu rn "The␣method␣ f a i l e d ␣ a f t e r ␣%s ␣ i t e r a t i o n s , ␣N␣=␣" % \(N) , N

# I n i t i a l i z e the n e c e s s a r y c o n s t a n t sTOL = 10 ∗∗ (−4) # Set the t o l e r a n c eN = 10 ∗∗ 2 # Set the maximum number o f i t e r a t i o n s

f u n c t i o n s = [ " ( x␣∗∗␣ 3) ␣−␣ 2 .0 ␣∗␣ ( x␣∗∗␣ 2) ␣−␣ 5 .0 " , \" ( x␣∗∗␣ 3) ␣+␣ 3 .0 ␣∗␣ ( x␣∗∗␣ 2) ␣−␣1" , \"x␣−␣ cos ( x ) " , \"x␣−␣ 0 .8 ␣−␣ 0 .2 ␣∗␣ s i n ( x ) " ]

f o r i , f i n enumerate ( f _ l i s t ) :x = newton ( p_int [ i ] , TOL, N, f _ l i s t [ i ] , d f _ l i s t [ i ] )# P r i n t to the c o n s o l ep r i n t "For ␣ f ( x ) ␣=␣" , f u n c t i o n s [ i ] , "␣p%s ␣=␣" % ( x [ 0 ] ) , \

x [ 1 ] , "␣on␣[%s ,%s ] . " % ( a [ i ] , b [ i ] )

11. Use all three methods in this section to find solutions to within 10−5 for the following problems.

(a) 3x − ex = 0 for 1 ≤ x ≤ 2(b) 2x + 3cos x − ex = 0 for 0 ≤ x ≤ 1

SOLUTION.

(a) Newton’s method f (x) = 3x − ex with p0 = 1.5 gives p3 = 1.51213455166 on [1, 2].

The Secant method f (x) = 3x−ex with p0 = 1.0 and p1 = 2.0 gives p10 = 1.51213455176on [1, 2].

The Method of False Position f (x) = 3x − ex with p0 = 1.0 and p1 = 2.0 givesp17 = 1.51212953952 on [1, 2].

15

Page 16: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.3 Newton’s Method and Its Extensions Solutions by Jon Lo Kim Lin - Fall 2014

(b) Newton’s method f (x) = 2x + 3cos x − ex with p0 = 0.5 gives p6 = 1.23971469798 on[0, 1].

The Secant method f (x) = 2x + 3cos x − ex with p0 = 0.0 and p1 = 1.0 givesp7 = 1.23971469689 on [0, 1].

The Method of False Position f (x) = 2x + 3cos x − ex with p0 = 0.0 and p1 = 1.0gives p10 = 1.23971177328 on [0, 1].

The corresponding python code is given by

# Purpose : Imp l ement s a l l t h r e e a l g o r i t h m s i n s e c t i o n 2 .3## More s p e c i f i c a l l y , f i n d s a s o l u t i o n to## f ( p)=0## i f g i v e n an i n i t i a l a p p r o x ima t i o n by u s i n g# Newton ’ s method , the Secant method# and the Method o f f a l s e p o s i t i o n :##==============================================================# Record o f r e v i s i o n s :## Date Programmer D e s c r i p t i o n o f change# ======== ================= ===================# 11/03/14 Jon T. Lo Kim L i n O r i g i n a l code##==============================================================from math import exp , cos , s i n , \

f a b s # Only impo r t what you need

# The f u n c t i o n s o f i n t e r e s tdef f_a ( x ) :

r e tu rn 3 .0 ∗ x − exp ( x )def f_b ( x ) :

r e tu rn 2 .0 ∗ x + 3 .0 ∗ cos ( x ) − exp ( x )

# The c o r r e s p o n d i n g d e r i v a t i v e sdef df_a ( x ) :

r e tu rn 3 .0 − exp ( x )def df_b ( x ) :

r e tu rn 2 .0 − 3 .0 ∗ s i n ( x ) − exp ( x )

# Our l i s t o f f u n c t i o n sf _ l i s t = [ f_a , f_b ]

16

Page 17: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.3 Newton’s Method and Its Extensions Solutions by Jon Lo Kim Lin - Fall 2014

# Our l i s t o f c o r r e s p o n d i n g d e r i v a t i v e sd f _ l i s t = [ df_a , df_b ]

# Our l i s t o f l e f t i n t e r v a l end−p o i n t sa = [ 1 . 0 , 0 . 0 ]

# Our l i s t o f r i g h t i n t e r v a l end−p o i n t sb = [ 2 . 0 , 1 . 0 ]

# Our l i s t s o f i n i t i a l a p p r o x ima t i o n sp0_N = [ 1 . 5 , 0 . 5 ]p0 = [ [ 1 . 0 , 2 . 0 ] , [ 0 . 0 , 1 . 0 ] ]

def sgn ( x ) : # The s ignum f u n c t i o ni f x < 0 . 0 :

r e tu rn −1.0e l i f x == 0 . 0 :

r e tu rn 0 .0e l s e :

r e tu rn 1 .0

def newton ( p0 , TOL, N, f , d f ) :i = 1 # I n i t i a l i z e the i n d e xwh i l e i <= N:

p = p0 − f ( p0 ) / d f ( p0 ) # Compute p_ii f f a b s ( p − p0 ) < TOL:

y = [ i , p ]r e tu rn ys y s . e x i t ( )

e l s e :i = i + 1 # Inc r emen t the i n d e xp0 = p # Update p_0

r e tu rn "The␣method␣ f a i l e d ␣ a f t e r ␣%s ␣ i t e r a t i o n s , ␣N␣=␣" % \(N) , N

def s e c a n t ( p0 , p1 , TOL, N, f ) :i = 2 # I n i t i a l i z e the i n d e xq0 = f ( p0 ) # I n i t i a l i z e q0 and q1q1 = f ( p1 )wh i l e i <= N:

p = p1 − q1 ∗ ( p1 − p0 ) / ( q1 − q0 ) # Compute p_ii f f a b s ( p − p1 ) < TOL:

y = [ i , p ]r e tu rn ys y s . e x i t ( )

17

Page 18: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.3 Newton’s Method and Its Extensions Solutions by Jon Lo Kim Lin - Fall 2014

e l s e :i = i + 1 # Inc r emen t the i n d e xp0 = p1 # Update p_0 , q0 , p1 , q1q0 = q1p1 = pq1 = f ( p )

r e tu rn "The␣method␣ f a i l e d ␣ a f t e r ␣%s ␣ i t e r a t i o n s , ␣N␣=␣" % \(N) , N

def f a l s e _ p o s i t i o n ( p0 , p1 , TOL, N, f ) :i = 2 # I n i t i a l i z e the i n d e xq0 = f ( p0 ) # I n i t i a l i z e q0 and q1q1 = f ( p1 )wh i l e i <= N:

p = p1 − q1 ∗ ( ( p1 − p0 ) / ( q1 − q0 ) ) # Compute p_ii f f a b s ( p − p1 ) < TOL:

y = [ i , p ]r e tu rn ys y s . e x i t ( )

e l s e :i = i + 1 # Inc r emen t the i n d e xq = f ( p )

i f sgn ( q ) ∗ sgn ( q1 ) < 0 . 0 :p0 = p1q0 = q1

p1 = p # Make s u r e t h a t t h i s o u t s i d e o f the i f b l o c kq1 = q

r e tu rn "The␣method␣ f a i l e d ␣ a f t e r ␣%s ␣ i t e r a t i o n s , ␣N␣=␣" % \(N) , N

# I n i t i a l i z e the n e c e s s a r y c o n s t a n t s

TOL = 10 ∗∗ (−5) # Set the t o l e r a n c eN = 10 ∗∗ 2 # Set the maximum number o f i t e r a t i o n s

f u n c t i o n s = [ " 3 .0 ␣∗␣x␣−␣ exp ( x ) " , \" 2 .0 ␣∗␣x␣+␣ 3 .0 ␣∗␣ cos ( x ) ␣−␣ exp ( x ) " ]

f o r i , f i n enumerate ( f _ l i s t ) :# Newton ’ s methodx = newton (p0_N [ i ] , TOL, N, f _ l i s t [ i ] , d f _ l i s t [ i ] )# Secant methody = s e c a n t ( p0 [ i ] [ 0 ] , p0 [ i ] [ 1 ] , TOL, N, f _ l i s t [ i ] )# Method o f F a l s e P o s i t i o nz = f a l s e_ p o s i t i o n ( p0 [ i ] [ 0 ] , p0 [ i ] [ 1 ] , TOL, N, f _ l i s t [ i ] )

18

Page 19: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.3 Newton’s Method and Its Extensions Solutions by Jon Lo Kim Lin - Fall 2014

# P r i n t to the c o n s o l ep r i n t "Newton ’ s ␣method␣ f ( x ) ␣=␣" , f u n c t i o n s [ i ] , \"␣ w i t h ␣p0␣=␣" , p0_N [ i ] , "␣ g i v e s ␣p%s ␣=␣" % ( x [ 0 ] ) , \

x [ 1 ] , "␣on␣[%s ,%s ] . " % ( a [ i ] , b [ i ] )p r i n t " Secant ␣method␣ f ( x ) ␣=␣" , f u n c t i o n s [ i ] , \"␣ w i t h ␣p0␣=␣" , p0 [ i ] [ 0 ] , "␣and␣p1␣=␣" , p0 [ i ] [ 1 ] , \"␣ g i v e s ␣p%s ␣=␣" % ( y [ 0 ] ) , \

y [ 1 ] , "␣on␣[%s ,%s ] . " % ( a [ i ] , b [ i ] )p r i n t "Method␣ o f ␣ F a l s e ␣ P o s i t i o n ␣ f ( x ) ␣=␣" , f u n c t i o n s [ i ] , \"␣ w i t h ␣p0␣=␣" , p0 [ i ] [ 0 ] , "␣and␣p1␣=␣" , p0 [ i ] [ 1 ] , \"␣ g i v e s ␣p%s ␣=␣" % ( z [ 0 ] ) , \

z [ 1 ] , "␣on␣[%s ,%s ] . " % ( a [ i ] , b [ i ] )

19. The iteration equation for the secant method can be written in the simpler form

pn =f (pn−1)pn−2 − f (pn−2)pn−1

f (pn−1)− f (pn−2).

Explain, why, in general, this iteration is likely to be less accurate than the one given inAlgorithm 2.4.

SOLUTION. This formula involves the subtraction of nearly equal numbers in both the nu-meration and denominator if pn−1 and pn−2 are nearly equal, hence; less numerically robust. �

24. Find an approximation for λ, accurate to within 10−4, for the population equation

1, 564, 000 = 1, 000, 000eλ +435, 000

λ(eλ − 1),

discussed in the introduction to this chapter. Use this value to predict the population at theend of the second year, assuming that the immigration rate during this year remains at 435,000individuals per year.

SOLUTION. We proceed by means of Newton’s method on the function

f (λ) = 106λeλ + 435 · 103(eλ − 1

)− 1564 · 103λ

with corresponding derivative

f ′(λ) = 103(5eλ(2 · 102λ+ 287)− 1564

),

error tolerance 10−4 and initial approximation p0 = 1. Indeed, the initial approximation p0 = 1.0gives p8 = 0.100997932932, and the population at the end of the 2nd year is 2187938.74539.The implementation in python is listed below

19

Page 20: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.3 Newton’s Method and Its Extensions Solutions by Jon Lo Kim Lin - Fall 2014

# Purpose : Imp l ement s Newton ’ s method to s o l v e e x e r c i s e 2 . 3 . 2 4##==============================================================# Record o f r e v i s i o n s :## Date Programmer D e s c r i p t i o n o f change# ======== ================= ===================# 11/03/14 Jon T. Lo Kim L i n O r i g i n a l code##==============================================================from math import exp , f a b s # Only impo r t what you need

# The f u n c t i o n o f i n t e r e s tdef f ( l ) :

f 1 = (10 ∗∗ 6) ∗ l ∗ exp ( l )f 2 = 435 .0 ∗ (10 ∗∗ 3) ∗ ( exp ( l ) − 1 . 0 )f 3 = −1564.0 ∗ (10 ∗∗ 3) ∗ lr e tu rn f 1 + f2 + f3

# The c o r r e s p o n d i n g d e r i v a t i v edef d f ( l ) :

d f1 = 2 .0 ∗ (10 ∗∗ 2) ∗ l + 287r e tu rn (10 ∗∗ 3) ∗ ( 5 . 0 ∗ exp ( l ) ∗ d f1 − 1564 .0 )

# The p o p u l a t i o n f u n c t i o ndef pop ( l ) :

pop1 = (10 ∗∗ 6) ∗ exp ( 2 . 0 ∗ l )pop2 = 435 .0 ∗ (10 ∗∗ 3) / lpop3 = exp ( 2 . 0 ∗ l ) − 1 .0r e tu rn pop1 + pop2 ∗ pop3

def newton ( p0 , TOL, N, f , d f ) :i = 1 # I n i t i a l i z e the i n d e xwh i l e i <= N:

p = p0 − f ( p0 ) / d f ( p0 ) # Compute p_ii f f a b s ( p − p0 ) < TOL:

y = [ i , p ]r e tu rn ys y s . e x i t ( )

e l s e :i = i + 1 # Inc r emen t the i n d e xp0 = p # Update p_0

r e tu rn "The␣method␣ f a i l e d ␣ a f t e r ␣%s ␣ i t e r a t i o n s , ␣N␣=␣" % \(N) , N

20

Page 21: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.3 Newton’s Method and Its Extensions Solutions by Jon Lo Kim Lin - Fall 2014

# I n i t i a l i z e the n e c e s s a r y c o n s t a n t sTOL = 10 ∗∗ (−4) # Set the t o l e r a n c eN = 10 ∗∗ 3 # Set the maximum number o f i t e r a t i o n sp0 = 1 .0 # Set the i n i t i a l a p p r o x ima t i o n

x = newton ( p0 , TOL, N, f , d f )

p r i n t " I n i t i a l ␣ a p p r o x ima t i o n ␣" , "p0␣=␣" , p0 \, "␣ g i v e s ␣p%s ␣=␣" % x [ 0 ] , x [ 1 ]

p r i n t "The␣ p o p u l a t i o n ␣ at ␣ the ␣ end␣ o f ␣ the ␣2nd␣ y e a r ␣ i s ␣" , \pop ( x [ 1 ] )

26. The accumulated value of a savings account based on regular periodic payments can be deter-mined from the annuity due equation,

A =P

i[(1 + i)n − 1] .

In this equation, A is the amount in the account, P is the amount regularly deposited, and iis the rate of interest per period for the n deposit periods. An engineer would like to have asavings account valued at $750,000 upon retirement in 20 years and can afford to put $1500per month toward this goal. What is the minimal interest rate at which this amount can beinvested, assuming that the interest is compounded monthly?

SOLUTION. We proceed by means of Newton’s method on the function

f (i) = 15 · 102((1 + i)20 − 1

)− 75 · 104i

with the corresponding derivative

f ′(i) = 3 · 104(1 + i)19 − 75 · 104,

error tolerance 10−3 and initial approximation p0 = 0.5. Indeed, the initial approximationp0 = 0.5 gives p7 = 0.280956890647 and the minimal monthly interest rate is i = 2.085%.The implementation in python is listed below

# Purpose : Imp l ement s Newton ’ s method to s o l v e e x e r c i s e 2 . 3 . 2 6##==============================================================# Record o f r e v i s i o n s :## Date Programmer D e s c r i p t i o n o f change# ======== ================= ===================# 11/03/14 Jon T. Lo Kim L i n O r i g i n a l code##==============================================================

21

Page 22: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.3 Newton’s Method and Its Extensions Solutions by Jon Lo Kim Lin - Fall 2014

from math import exp , f a b s # Only impo r t what you need

# The f u n c t i o n o f i n t e r e s tdef f ( i ) :

f 1 = 15 .0 ∗ (10 ∗∗ 2) ∗ ( ( ( 1 + i ) ∗∗ ( 20 ) ) − 1 . 0 )f 2 = −75.0 ∗ (10 ∗∗ 4) ∗ ir e tu rn f 1 + f2

# The c o r r e s p o n d i n g d e r i v a t i v edef d f ( i ) :

d f1 = 3 .0 ∗ (10 ∗∗ 4) ∗ ( ( 1 . 0 + i ) ∗∗ ( 19 ) )d f2 = −75.0 ∗ (10 ∗∗ 4)r e tu rn d f1 + d f2

# The monthy i n t e r e s t f u n c t i o ndef i n t e r e s t ( i ) :

i 1 = 1 .0 / 240 .0i 2 = (1 + i ) ∗∗ (20)r e tu rn (10 ∗∗ 2) ∗ ( i 2 ∗∗ ( i 1 ) − 1 . 0 )

def newton ( p0 , TOL, N, f , d f ) :i = 1 # I n i t i a l i z e the i n d e xwh i l e i <= N:

p = p0 − f ( p0 ) / d f ( p0 ) # Compute p_ii f f a b s ( p − p0 ) < TOL:

y = [ i , p ]r e tu rn ys y s . e x i t ( )

e l s e :i = i + 1 # Inc r emen t the i n d e xp0 = p # Update p_0

r e tu rn "The␣method␣ f a i l e d ␣ a f t e r ␣%s ␣ i t e r a t i o n s , ␣N␣=␣" % \(N) , N

# I n i t i a l i z e the n e c e s s a r y c o n s t a n t sTOL = 10 ∗∗ (−3) # Set the t o l e r a n c eN = 10 ∗∗ 3 # Set the maximum number o f i t e r a t i o n sp0 = 0 .5 # Set the i n i t i a l a p p r o x ima t i o n

x = newton ( p0 , TOL, N, f , d f )

p r i n t " I n i t i a l ␣ a p p r o x ima t i o n ␣" , "p0␣=␣" , p0 \, "␣ g i v e s ␣p%s ␣=␣" % x [ 0 ] , x [ 1 ]

p r i n t "The␣m in ima l ␣month ly ␣ i n t e r e s t ␣ r a t e ␣ i s ␣ i =␣%.3E" % \

22

Page 23: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.3 Newton’s Method and Its Extensions Solutions by Jon Lo Kim Lin - Fall 2014

i n t e r e s t ( x [ 1 ] ) , " p e r c e n t "

34. In the design of all-terrain vehicles, it is necessary to consider the failure of the vehicle whenattempting to negotiate two types of obstacles. One type of failure is called hang-up failureand occurs when the vehicle attempts to cross an obstacle that causes the bottom of thevehicle to touch the ground. The other type of failure is called nose-in failure and occurswhen the vehicle descends into a ditch and its nose touches the ground.

The accompanying figure, adapted from [Bek], shows the components associated with thenose-in failure of a vehicle. In that reference it is shown that the maximum angle α that canbe negotiated by a vehicle when β is the maximum angle at which hang-up failure does notoccur satisfies the equation

A sinα cosα+ B sin2 α− C cosα− E sinα = 0,

whereA = ` sinβ1, B = ` cosβ1, C = (h + 0.5D) sinβ1 − 0.5D tanβ1,

and E = (h + 0.5D) cosβ1 − 0.5D.

(a) It is stated that when ` = 89in., h = 49in., D = 55in., and β1 = 11.5◦, angle α isapproximately 33◦. Verify this result.

(b) Find α for the situation when `, h, and β1 are the same as in part (a) but D = 30in.

SOLUTION.

(a) To verify, α = 33◦ and D = 55 gives f (α) = 0.0254113058116. For even furthervalidation the Secant method with p0 = 0.0 and p1 = 1.0 gives α = 32.9721779929◦.

(b) For D = 30 the Secant method with p0 = 0.0 and p1 = 1.0 gives α = 33.1689065057◦.

The corresponding python code is listed below

23

Page 24: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.3 Newton’s Method and Its Extensions Solutions by Jon Lo Kim Lin - Fall 2014

# Purpose : Use the s e c a n t method to s o l v e e x e r c i s e 2 . 3 . 3 4##==============================================================# Record o f r e v i s i o n s :## Date Programmer D e s c r i p t i o n o f change# ======== ================= ===================# 11/03/14 Jon T. Lo Kim L i n O r i g i n a l code##==============================================================from math import exp , cos , s i n , tan , r a d i a n s , \

deg r ee s , f a b s # Only impo r t what you need

# The f u n c t i o n o f i n t e r e s tdef f ( a , D) :

l = 89 .0h = 49 .0b1 = r a d i a n s ( 1 1 . 5 )A = l ∗ s i n ( b1 )B = l ∗ cos ( b1 )C = ( h + 0 .5 ∗ D) ∗ s i n ( b1 ) − 0 .5 ∗ D ∗ tan ( b1 )E = ( h + 0 .5 ∗ D) ∗ cos ( b1 ) − 0 .5 ∗ Df1 = A ∗ s i n ( a ) ∗ cos ( a )f 2 = B ∗ ( s i n ( a ) ∗∗ 2)f 3 = −C ∗ cos ( a )f 4 = −E ∗ s i n ( a )r e tu rn f 1 + f2 + f3 + f4

def f_a ( a ) :D = 55 .0r e tu rn f ( a , D)

def f_b ( a ) :D = 30 .0r e tu rn f ( a , D)

def s e c a n t ( p0 , p1 , TOL, N, f ) :i = 2 # I n i t i a l i z e the i n d e xq0 = f ( p0 ) # I n i t i a l i z e q0 and q1q1 = f ( p1 )wh i l e i <= N:

p = p1 − q1 ∗ ( p1 − p0 ) / ( q1 − q0 ) # Compute p_ii f f a b s ( p − p1 ) < TOL:

y = [ i , p ]r e tu rn y

24

Page 25: MATH 104A HW 03 SOLUTION KEY - UCSBweb.math.ucsb.edu › ... › Teaching2014Fall104A › hw03_sol_2014.pdf · 2014-11-04 · MATH 104A HW 03 SOLUTION KEY ... # 11/03/14 Jon T. Lo

2.3 Newton’s Method and Its Extensions Solutions by Jon Lo Kim Lin - Fall 2014

s y s . e x i t ( )e l s e :

i = i + 1 # Inc r emen t the i n d e xp0 = p1 # Update p_0 , q0 , p1 , q1q0 = q1p1 = pq1 = f ( p )

r e tu rn "The␣method␣ f a i l e d ␣ a f t e r ␣%s ␣ i t e r a t i o n s , ␣N␣=␣" % \(N) , N

# I n i t i a l i z e the n e c e s s a r y c o n s t a n t s

TOL = 10 ∗∗ (−4) # Set the t o l e r a n c eN = 10 ∗∗ 2 # Set the maximum number o f i t e r a t i o n s

# Par t ( a )p r i n t " Par t ␣ ( a ) "

# V e r i f y the r e s u l ta = r a d i a n s (33)p r i n t "To␣ v e r i f y , ␣ a l p h a ␣=␣33␣ d e g r e e s ␣and␣D=55␣ g i v e s ␣ f ( a l p h a ) ␣=␣" , \

f_a ( a )

# For f u r t h e r v a l i d a t i o n we imp lement the s e c a n t methodp0 = 0 .0p1 = 1 .0ya = s e c a n t ( p0 , p1 , TOL, N, f_a )p r i n t "For ␣ f u r t h e r ␣ v a l i d a t i o n ␣ the ␣ Secant ␣method␣ w i t h ␣" , "p0␣=" , p0 , \

"and␣p1␣=␣" , p1 , " g i v e s ␣ a l p h a ␣=␣" , d e g r e e s ( ya [ 1 ] ) , \"␣ d e g r e e s "

# Part ( b )p r i n t " Par t ␣ ( b ) "

yb = s e c a n t ( p0 , p1 , TOL, N, f_b )p r i n t "For ␣D␣=␣30␣ the ␣ Secant ␣method␣ w i t h ␣" , "p0␣=" , p0 , \

"and␣p1␣=␣" , p1 , " g i v e s ␣ a l p h a ␣=␣" , d e g r e e s ( yb [ 1 ] ) , \"␣ d e g r e e s "

25


Recommended