Solution-HW2

# Solution-HW2 - 1 Write a program in a language of your...

This preview shows pages 1–3. Sign up to view the full content.

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 () 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 5.2 Determine the real roots of () 3 2 6 5 7 2 x x x x f + + = :

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

View Full Document
(a) Graphically. (b) Using bisection to locate the lowest root. Employ initial guesses of x l =0 and x u =1 and iterate until the estimated error ε a falls below a level of ε s =10%. SOLUTION: (a) (b) Bisection method: Starting with x l =0, x u =1 . We know 2 u l r x x x + = , % 100 × + = l u l u a x x x x ε . Make sure ()() 0 < u l x f x f in every iteration. The results are: Interation x r Ε 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 () 5 4 3 2 65 . 0 9 4 . 45 88 3 . 82 26 x x x x x x f + + + = : (a)
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/20/2011 for the course ENG 115 taught by Professor Rocke during the Spring '10 term at UC Davis.

### Page1 / 8

Solution-HW2 - 1 Write a program in a language of your...

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

View Full Document
Ask a homework question - tutors are online