File: rootsearch.py

package info (click to toggle)
pycode-browser 1%3A1.03-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,860 kB
  • sloc: python: 2,919; xml: 152; makefile: 76
file content (20 lines) | stat: -rwxr-xr-x 415 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import math
def func(x):
        return x**3 - 10.0* x*x + 5

def rootsearch(f,a,b,dx):
    x = a
    while True:
        f1 = f(x)
        f2 = f(x+dx)
        if f1*f2 < 0:
                return x, x + dx
        x = x + dx
        if x >=  b: 
                print( 'Failed to find root')
                return

x,y = rootsearch(func, 0.0,1.0,.1)
print( x,y)
x,y = rootsearch(math.cos, 0.0, 4,.1)
print( x,y)