Solution HW2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

1 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
2
/ 2)
−x.

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
5.2 Determine the real roots of f (x ) = −2 + 7 x − 5 x 2 + 6 x 3 :
(a) Graphically.
(b) Using bisection to locate the lowest root. Employ initial guesses of xl=0 and
xu=1 and iterate until the estimated error εa falls below a level of εs=10%.

SOLUTION:

(a)

(b) Bisection method:

Starting with xl=0, xu=1.


x + xu x − xl
We know x r = l , εa = u × 100% . Make sure f ( xl ) f ( xu ) < 0 in every
2 xu + xl
iteration. The results are:

Interation xr Εa %
1 0.5 100
2 0.25 100
3 0.375 33
4 0.3125 22
5 0.34375 9.1

5.3 Determine the real roots of f (x ) = −26 + 82.3x − 88 x 2 + 45.4 x 3 − 9 x 4 + 0.65 x 5 :


(a) Graphically.
(b) Using bisection to determine the highest root to εs=10%. Employ initial
guesses of xl=0.5 and xu=1.0.
(c) Perform the same computation as in (b) but use the false position method and
εs=0.1%.
SOLUTION:
(a)
(b) Bisection method:

Starting with xl=0.5, xu=1.0.


x + xu x − xl
We know x r = l , εa = u × 100% . Make sure f ( xl ) f ( xu ) < 0 in every
2 xu + xl
iteration. The results are:

Interation xr Εa %
1 0.75 33
2 0.625 20
3 0.5625 11.1
4 0.59375 5.2

(c) False position method:

f (xu )(xl − xu )
new
xr − x rold
x r = xu − , εa = × 100% . Also make sure
f ( xl ) − f ( xu ) x rnew
f ( xl ) f ( xu ) < 0 in every iteration. The results are:

Interation xr Εa %
1 0.62149
2 0.583727 6.47
3 0.5797806 0.68
4 0.5793734 0.07

5.5 Locate the first nontrivial root of sin x=x2, where x is in radians. Use a graphical
technique and bisection with the initial interval from 0.5 to 1. Perform the
computation until εa less than εs=2%. Also perform an error check by substituting
your final answer into the original equation.

SOLUTION:

Let f ( x ) = sin x − x 2
Starting with xl=0.5, xu=1.0.
xl + xu x − xl
We know x r = , εa = u × 100% . Make sure f ( xl ) f ( xu ) < 0 in every
2 xu + xl
iteration. The results are:

Interation xr Εa %
1 0.75 33.3
2 0.875 14.3
3 0.9375 6.6
4 0.90625 3.5
5 0.890625 1.7

5.9 Find the smallest positive root of the function (x is in radians) x 2 sin x = 5 using
the false position method. To locate the region in which the root lies, first plot this
function for values of x between 0 and 5. Perform the computation until εa falls below
εs=1%. Check your final answer by substituting it into the original function.

SOLUTION:

Let f ( x ) = x 2 sin x − 5
First, plot the function:

Form the plot, we choose xl=2, xu=3 as the starting point.

f (xu )(xl − xu )
new
xr − x rold
x r = xu − , εa = × 100% . Also make sure
f ( xl ) − f ( xu ) x rnew
f ( xl ) f ( xu ) < 0 in every iteration. The results are:

Interation xr Εa %
1 2.212672
2 2.236474 1.06
3 2.238879 0.11

6.1 Use simple fixed-point iteration to locate the root of f (x ) = sin x − x . Use an
initial guess of x0=0.5 and iterate until εa≤0.01%.

SOLUTION:

Simple fixed-point iteration:


xi +1 − xi
xi +1 = g ( xi ) , ε a = 100%
xi +1
Here
xi +1 = sin xi
Starting at x0=0.5

Iteration xi+1 εa%


1 0.6496369 23
2 0.7215238 9.9
3 0.7509012 3.9
4 0.7620969 1.5
5 0.7662482 0.5
6 0.7677717 0.2
7 0.7683278 0.07
8 0.7685317 0.02
9 0.7686061 0.009

6.2 Use (a) fixed-point iteration and (b) the Newton-Raphson method to determine a
root of f ( x ) = −0.9 x 2 + 1.7 x + 2.5 using x0=5. Perform the computation until εa is less
than εs=0.01%. Also perform an error check of your final answer.

SOLUTION:

(a) Fixed-point iteration:


x − xi
xi +1 = g ( xi ) , ε a = i +1 100%
xi +1
Here
0.9 xi2 − 2.5
xi +1 =
1.7
Starting at
x0=5

Iteration xi+1
1 11.76
2 71.80
3 2728.1
It diverges.

(b) Newton-Raphson method:


f ( xi ) x − xi
xi +1 = xi − , ε a = i +1 100%
f ′( xi ) xi +1
Here
2
− 0.9 xi + 1.7 xi + 2.5
xi +1 = xi −
− 1.8 xi + 1.7
Starting at
x0=5
Iteration xi+1 ε a%
1 3.424658 46
2 2.924357 17.1
3 2.861147 2.2
4 2.860105 0.04
5 2.860105 0

6.3 Determine the real roots of f ( x ) = − 2 . 0 + 6 x − 4 x 2 + 0 . 5 x 3 : (a)


graphically and (b) using the Newton-Raphson method to within εs=0.01%.

SOLUTION:

(a)

(b) Newton-Raphson method:

f ( xi ) x − xi
xi +1 = xi − , ε a = i +1 100%
f ′( xi ) xi +1
Here
2 3
− 2.0 + 6 xi − 4 xi + 0.5 xi
xi +1 = xi − 2
6 − 8 xi + 1.5 xi
From the plot, we can see that the approximate roots are 0.5, 1.5, and 6

For x0=0.5:

Iteration xi+1 ε a%
1 0.4736842 5.5
2 0.4745714 0.2
3 0.4745724 0.0002

For x0=1.5:

Iteration xi+1 ε a%
1 1.380952 8.6
2 1.369227 0.8
3 1.369103 0.009
For x0=6:

Iteration xi+1 ε a%
1 6.166667 2.7
2 6.156366 0.16
3 0.156325 0.0006

Notice: Rapid convergence with accurate initial guesses from the plot.

6.4 Employ the Newton-Raphson method to determine a real root for


f ( x ) = −2.0 + 6 x − 4 x 2 + 0.5 x 3 using initial guesses of (a) 4.2 and (b) 4.43. Discuss
and use graphical and analytical methods to explain any peculiarities in your results.

SOLUTION:

(a) For x0=4.2:

f ( xi ) x − xi
xi +1 = xi − , ε a = i +1 100%
f ′( xi ) xi +1
Here
2 3
− 2.0 + 6 xi − 4 xi + 0.5 xi
xi +1 = xi − 2
6 − 8 xi + 1.5 xi
Iteration xi+1 ε a%
1 -4.849096 186
2 -2.573902 88
3 -1.137515 126
4 -0.2727324 317
5 0.202833 234
6 0.4153548 51
7 0.4705743 11
8 0.4745519 0.8
9 0.4745724 0.004

(b) For x0=4.43:

Iteration xi+1 ε a%
1 -3937 100
2 -2623 50
. . .
. . .
. . .

25 0.4745724 0.00009

Interpretation:
f ′(4.43) = −0.00265
We know the formula of Newtons method is
f ( xi )
xi +1 = xi − .
f ′( xi )
So if we choose xi=4.43, xi+1 will be very large. It is far from the real root, we need
much more iterations to find the real root.

You might also like