HW2 - method program to find a root of f ( x ) = e - ( x...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
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 ( ) x e x f 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: x i =1 Percent tolerance: es=0.001 Iterations: imax=100 OUTPUT: Root: x r =0.753089165 Relative percent error: ea=5.447936E-7 Interation: I=3
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5.2 (a) A plot indicates that a single real root occurs at about x = 0.42. -8 -4 0 4 8 -1 0 1 (b) First iteration: 5 . 0 2 1 0 = + = r x % 100 % 100 0 1 0 1 = × + = a ε 75 . 0 ) 375 . 0 ( 2 ) 5 . 0 ( ) 0 ( = = f f Therefore, the new bracket is x l = 0 and x u = 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 x l x u x r f ( x l ) f ( x r ) f ( x l ) × f ( x r ) a 1 0 1 0.5 -2 0.375 -0.75 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. -8 -4 0 4 8 00 . 511 . 5 (b) Bisection: First iteration:
Background image of page 2
75 . 0 2 1 5 . 0 = + = r x % 33 . 33 % 100 5 . 0 1 5 . 0 1 = × + = a ε 06321 . 3 ) 07236 . 2 ( 47813 . 1 ) 75 . 0 ( ) 5 . 0 ( = = f f Therefore, the new bracket is x l = 0.5 and x u = 0.75.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

HW2 - method program to find a root of f ( x ) = e - ( x...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online