Docsity

Implementation of Newton's Method to Find a Root of a Function - Prof. David M. Rocke, Assignments of Engineering

Toggle sidebar
bg1
Write a program in a language of your choice to implement Newtons method. Assume that you
have functions f(x) and fp(x) to return the function value and its derivative. Try out the Newtons
method program to find a root of
(
)
xexf x−= −)2/( 2.
SOLUTION:
Option Explicit
Function Newton(xi, es, imax)
Dim xr As Double, xrold As Double, ea As Double, xinc As Double
Dim iter As Integer
iter = 0
xr = xi
Do
xrold = xr
iter = iter + 1
xinc = -f(xr) / fp(xr)
xr = xrold + xinc
If xr <> 0 Then
ea = Abs((xr - xrold) / xr)
End If
If (ea < es) Or (iter >= imax) Then
Exit Do
End If
Loop
Newton = xr
MsgBox ("ea = " & ea & " iter = " & iter)
End Function
Function f(x)
f = Exp(-(x ^ 2) / 2) - x
End Function
Function fp(x)
fp = -x * Exp(-(x ^ 2) / 2) - 1
End Function
INPUT:
Initial guess: xi=1
Percent tolerance: es=0.001
Iterations: imax=100
OUTPUT:
Root: xr=0.753089165
Relative percent error: ea=5.447936E-7
Interation: I=3
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Implementation of Newton's Method to Find a Root of a Function - Prof. David M. Rocke and more Assignments Engineering in PDF only on Docsity!

Write a program in a language of your choice to implement Newtons method. Assume that you

have functions f(x) and fp(x) to return the function value and its derivative. Try out the Newtons

method program to find a root of f ( x ) e x

x

− ( 2 / 2 ) .

SOLUTION:

Option Explicit

Function Newton(xi, es, imax)

Dim xr As Double, xrold As Double, ea As Double, xinc As Double

Dim iter As Integer

iter = 0

xr = xi

Do

xrold = xr

iter = iter + 1

xinc = -f(xr) / fp(xr)

xr = xrold + xinc

If xr <> 0 Then

ea = Abs((xr - xrold) / xr)

End If

If (ea < es) Or (iter >= imax) Then

Exit Do

End If

Loop

Newton = xr

MsgBox ("ea = " & ea & " iter = " & iter)

End Function

Function f(x)

f = Exp(-(x ^ 2) / 2) - x

End Function

Function fp(x)

fp = -x * Exp(-(x ^ 2) / 2) - 1

End Function

INPUT:

Initial guess: xi=

Percent tolerance: es=0.

Iterations: imax=

OUTPUT:

Root: xr=0.

Relative percent error: ea=5.447936E-

Interation: I=

5.2 (a) A plot indicates that a single real root occurs at about x = 0.42.

- -

0

4

8

-1 0 1

(b) First iteration:

xr =

× =

ε a =

f ( 0 ) f ( 0. 5 )=− 2 ( 0. 375 )=− 0. 75

Therefore, the new bracket is xl = 0 and xu = 0.5.

The process can be repeated until the approximate error falls below 10%. As summarized

below, this occurs after 5 iterations yielding a root estimate of 0.40625.

iteration xl xu xr f ( xl ) f ( xr ) f ( xl ) × f ( xr ) ε a

1 0 1 0.5 -2 0.375 -0.

2 0 0.5 0.25 -2 -0.73438 1.46875 100.00%

3 0.25 0.5 0.375 -0.73438 -0.18945 0.13913 33.33%

4 0.375 0.5 0.4375 -0.18945 0.08667 -0.01642 14.29%

5 0.375 0.4375 0.40625 -0.18945 -0.05246 0.009939 7.69%

5.3 (a) A plot indicates that a single real root occurs at about x = 0.58.

(b) Bisection:

First iteration:

1 0.5 1.00000 -1.47813 3.70000 0.64273 0.91879 -1.

2 0.5 0.64273 -1.47813 0.91879 0.58802 0.13729 -0.20293 9.304%

3 0.5 0.58802 -1.47813 0.13729 0.58054 0.01822 -0.02693 1.289%

4 0.5 0.58054 -1.47813 0.01822 0.57956 0.00238 -0.00351 0.169%

5.5 A graph indicates that a nontrivial root (i.e., nonzero) is located at about 0.93.

-1.

-

-0.

0

0.

0 0.5 1 1.

Using bisection, the first iteration is

xr =

f ( 0. 5 ) f ( 0. 75 )= 0. 354426 ( 0. 2597638 )= 0. 092067

Therefore, the root is in the second interval and the lower guess is redefined as xl = 0.75. The

second iteration is

xr =

ε a =

f ( 0. 75 ) f ( 0. 875 )= 0. 259764 ( 0. 0976216 )= 0. 025359

Because the product is positive, the root is in the second interval and the lower guess is

redefined as xl = 0.875. All the iterations are displayed in the following table:

i xl f ( xl )^ xu f ( xu )^ xr f ( xr )^ ε a

1 0.5 0.354426 1 -0.158529 0.75 0.

2 0.75 0.259764 1 -0.158529 0.875 0.0976216 14.29%

3 0.875 0.097622 1 -0.158529 0.9375 -0.0178935 6.67%

4 0.875 0.097622 0.9375 -0.0178935 0.90625 0.0429034 3.45%

5 0.90625 0.042903 0.9375 -0.0178935 0.921875 0.0132774 1.69%

Consequently, after five iterations we obtain a root estimate of 0.921875 with an approximate

error of 1.69%, which is below the stopping criterion of 2%. The result can be checked by

substituting it into the original equation to verify that it is close to zero.

( ) sin( ) sin( 0. 921875 ) 0. 921875 0. 0132774

3 3 f x = x − x = − =

5.9 A graph of the function indicates a positive real root at approximately x = 3.7.

Using false position, the first iteration is

xr = −

f ( 0 ) f ( 1. 62003 )=− 5 (− 4. 22944 )= 21. 147

Therefore, the root is in the second interval and the lower guess is redefined as xl = 1.62003.

The remaining iterations are summarized below

i xl xu f ( xl ) f ( xu ) xr f ( xr ) f ( xl ) × f ( xr ) ε a

1 0 5.00000 -5 10.43182 1.62003 -4.22944 21.

2 1.62003 5.00000 -4.2294 10.43182 2.59507 -4.72984 20.00459 37.573%

3 2.59507 5.00000 -4.7298 10.43182 3.34532 -2.14219 10.13219 22.427%

4 3.34532 5.00000 -2.1422 10.43182 3.62722 -0.69027 1.47869 7.772%

5 3.62722 5.00000 -0.6903 10.43182 3.71242 -0.19700 0.13598 2.295%

6 3.71242 5.00000 -0.197 10.43182 3.73628 -0.05424 0.01069 0.639%

The final result, xr = 3.73628, can be checked by substituting it into the original function to

yield a near-zero result,

( 3. 73628 ) ( 3. 73628 ) cos 3. 73628 5 0. 05424

2 f = − =−

6.1 The function can be formulated as a fixed-point iteration as

xi + 1 = 2 sin ( xi )

Using an initial guess of x 0 = 0.5, the first iteration is

x 1 = 2 sin ( 0. 5 ) = 1. 299274

× =

ε a =

The equation can be solved in numerous ways. A simple way that converges is to solve for

the x that is not raised to a power to yield

3 2 x x x

The resulting iterations are

i xi^ ε a

0 3

(^1) 3.180791 5.68%

2 3.333959 4.59%

3 3.442543 3.15%

(c) Newton-Raphson

i xi f ( x ) f '( x ) ε a

0 3 -3.2 1.

1 5.133333 48.09007 55.68667 41.56%

2 4.26975 12.95624 27.17244 20.23%

3 3.792934 12.57%

(d) Secant

i xi − 1 f ( xi − 1 ) xi f ( xi ) ε a

0 3 -3.2 4 6.

1 4 6.6 3.326531 -1.9688531 20.25%

2 3.326531 -1.96885 3.481273 -0.7959153 4.44%

3 3.481273 -0.79592 3.586275 0.2478695 2.93%

(e) Modified secant ( δ = 0.01)

i x f ( x ) dx x + dx f ( x + dx ) f '( x ) ε a

0 3 -3.2 0.03 3.03 -3.14928 1.

1 4.892595 35.7632 0.048926 4.9415212 38.09731 47.7068 38.68%

2 4.142949 9.73047 0.041429 4.1843789 10.7367 24.28771 18.09%

3 3.742316 2.203063 0.037423 3.7797391 2.748117 14.56462 10.71%

6.3 (a) Fixed point. The equation can be solved in two ways. The way that converges is

xi + 1 = 1. 8 xi + 2. 5

The resulting iterations are

i xi^ ε a

0 5

1 3.391165 47.44%

(^2) 2.933274 15.61%

3 2.789246 5.16%

4 2.742379 1.71%

5 2.726955 0.57%

6 2.721859 0.19%

7 2.720174 0.06%

8 2.719616 0.02%

(b) Newton-Raphson

2

1 − +

i

i i i i x

x x x x

i xi f ( x ) f '( x ) ε a

0 5 -13.5 -8.

1 3.353659 -2.71044 -4.90732 49.09%

2 2.801332 -0.30506 -3.80266 19.72%

3 2.721108 -0.00644 -3.64222 2.95%

4 2.719341 -3.1E-06 -3.63868 0.06%

5 2.719341 -7.4E-13 -3.63868 0.00%

6.4 (a) A graph of the function indicates that there are 3 real roots at approximately 0.2, 1.5 and

6.3.

(b) The Newton-Raphson method can be set up as

2

2 3

1

  1. 5 8 1. 5

i i

i i i i i x x

x x x x x − +

This formula can be solved iteratively to determine the three roots as summarized in the

following tables:

i xi f ( x ) f '( x ) ε a

0 0 -1 5.

1 0.181818 -0.12923 4.095041100.000000%

2 0.213375 -0.0037 3.861294 14.789338%

3 0.214332 -3.4E-06 3.85425 0.446594%

4 0.214333 -2.8E-12 3.854244 0.000408%