{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec18_roots

# lec18_roots - Root search methods Supplied functions 1...

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

# Root search methods ''' Supplied functions: 1. bisect() Usage: root=bisect(f,xlow,xhigh,switch=0,tol=1.0e-8) Returns: the root in the bracket (float) Parameters: f - function f(x) to find the root of xlow,xhigh - lower and upper bounds of root switch - whether to check whether |f(x)| is getting smaller with each step tol - accuracy of root 2. newtonraphson() Usage: root = newtonraphson(f,xlow,xhigh,xStart,maxIter=10,tol=1.0e-8,plotit=True) Returns: the closest root to xStart (float) Parameters: f - function f(x) to find the root of xlow,xhigh - lower and upper bounds of root xStart = starting guess for root maxIter - upper limit to number of iterations tol - accuracy of root plotit - flag whether to plot ouput ''' __all__=('bisect()','newtonraphson()') from pylab import * def bisect(f,xlow,xhigh,switch=0,tol=1.0e-8): from math import ceil,log #for single numbers, these are more efficient than those supplied by numpy f1=f(xlow) if f1 == 0. : return xlow f2=f(xhigh) if f2 == 0. : return xhigh if f1*f2 > 0. : print "An even number of roots, or no roots, lie inside the bracket." return None n= int(ceil(log(abs(xhigh - xlow)/tol)/log(2.0)))

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 3

lec18_roots - Root search methods Supplied functions 1...

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

View Full Document
Ask a homework question - tutors are online