Solution HW2
Solution HW2
Solution HW2
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
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)
Interation xr Εa %
1 0.5 100
2 0.25 100
3 0.375 33
4 0.3125 22
5 0.34375 9.1
Interation xr Εa %
1 0.75 33
2 0.625 20
3 0.5625 11.1
4 0.59375 5.2
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:
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:
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:
Iteration xi+1
1 11.76
2 71.80
3 2728.1
It diverges.
SOLUTION:
(a)
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.
SOLUTION:
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
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.