def problem11():
    a,b,dx = (-6, 6, 0.01)
    print("The roots for problem 11 are:")
    while True:
        x1,x2 = rootsearch(f,a,b,dx)
        if x1 != None:
            a = x2
            root = newtonRaphson(f,df11,x1,x2)
            if root != None: print(root)
        else:
            print("Done\n")
            break
def problem23():
    a,b,dx = (-2, 2, 0.001)
    print("Problem 23: The coordinates where the two points of the circle intersect are:")
    while True:
        x1,x2 = rootsearch(f23,a,b,dx)
        if x1 != None:
            a = x2
            root = newtonRaphson(f23,df23,x1,x2)
            if root != None: print(root)
        else:
            print("Done\n")
            break
def problem10():
    a,b,dx = (-6, 6, 0.01)
    print("The roots for problem 10 are:")
    while True:
        x1,x2 = rootsearch(f,a,b,dx)
        if x1 != None:
            a = x2
            root = ridder(f,x1,x2)
            if root != None: print(root)
        else:
            print("Done\n")
            break
from math import tan
from bisect import *
from rootsearch import *


f = lambda x: x - tan(x)

a, b, dx = (0, 20, 0.01)

print('The roots are:')

while True:
    x1, x2 = rootsearch(f, a, b, dx)

    if x1 != None:
        a = x2
        root = bisect(f, x1, x2, 1)
        if root != None: print(root)
    else:
        print('\ndone!')
        break
input('Press return to exit')
Example #5
0
#!/usr/bin/python
## example4_3
from math import tan
from rootsearch import *
from bisect import *


def f(x):
    return x - tan(x)


a, b, dx = (0.0, 20.0, 0.01)
print "The roots are:"
while 1:
    x1, x2 = rootsearch(f, a, b, dx)
    if x1 != None:
        a = x2
        root = bisect(f, x1, x2, 1)
        if root != None: print root
    else:
        print "\nDone"
        break
raw_input("Press return to exit")
Example #6
0
##
## example4_3 (pp. 150-151, Kiusalaas 3rd Ed)
##

from math import tan
from numpy import arange,zeros
from rootsearch import *
from bisection import *
import pylab

def f(x): return x - tan(x)

a,b,dx = (0.0, 20.0, 0.01)
print ("The roots of f(x)= x-tan x on (0,20) using delta x = " + str(dx) + " :")
while 1:
    x1,x2 = rootsearch(f,a,b,dx)      # What is rootsearch doing here?
    if x1 != None:
        a = x2
        root = bisection(f,x1,x2,1)    # switch is set to 1 to avoid
        if root != None: print(root)   # singularities in f(x)
    else:                              # What epsilon is bisection using?
        print ("\nDone")
        n=20
        xData = arange(0,n,.1,dtype=float)
        n2=xData.size
        yData = zeros((n2),dtype=float)
        for j in range(0,n2):
          yData[j]=f(xData[j])

        pylab.xlabel("x")
        my_title= 'Plot of f(x)=x-tan x'
Example #7
0
def main():
    low = float(raw_input("low = "))
    high = float(raw_input("high = "))
    step = float(raw_input("step = "))

    root = rootsearch(f, low, high, step)
    bisect = bisection(f, low, high)
    newton = newtonRaphson(f, df, low, high)
    ridders = ridder(f, low, high)

    correct = math.pi

    print "rootsearch =", root
    print "rootsearch error =", max(abs(correct - root[0]), abs(correct - root[1]))
    print ""
    print "bisection =", bisect
    print "bisection error =", abs(correct - bisect)
    print ""
    print "newtonRaphson =", newton
    print "newton error =", abs(correct - newton)
    print ""
    print "ridder =", ridders
    print "ridder error =", abs(correct - ridders)
    print ""
    print ""
    print "Average root =", (((root[0] + root[1]) / 2.0) + bisect + newton + ridders) / 4.0
    print "Average root error =", abs(((((root[0] + root[1]) / 2.0) + bisect + newton + ridders) / 4.0) - correct)
    print ""
    print "Most Accurate root =", min(
        min(abs(correct - root[0]), abs(correct - root[1])),
        abs(correct - bisect),
        abs(correct - newton),
        abs(correct - ridders),
    )
    print "Least Accurate root =", max(
        max(abs(correct - root[0]), abs(correct - root[1])),
        abs(correct - bisect),
        abs(correct - newton),
        abs(correct - ridders),
    )

    gdisplay(title="Root Search")
    f1 = gcurve()
    for x in linspace(low - 1, high + 1, 200):
        f1.plot(pos=(x, f(x)), color=color.red)
    f2 = gdots(pos=[low, f(low)], color=color.yellow)
    gdots(pos=[high, f(high)], color=color.yellow)
    gdots(pos=[(root[0], f(root[0])), (root[1], f(root[1]))], color=color.green)

    gdisplay(title="Bisection")
    f1 = gcurve()
    for x in linspace(low - 1, high + 1, 200):
        f1.plot(pos=(x, f(x)), color=color.red)
    gdots(pos=[low, f(low)], color=color.yellow)
    gdots(pos=[high, f(high)], color=color.yellow)
    gdots(pos=[bisect, 0], color=color.green)

    gdisplay(title="Newton Raphson")
    f1 = gcurve()
    for x in linspace(low - 1, high + 1, 200):
        f1.plot(pos=(x, f(x)), color=color.red)
    gdots(pos=[low, f(low)], color=color.yellow)
    gdots(pos=[high, f(high)], color=color.yellow)
    gdots(pos=[newton, 0], color=color.green)

    gdisplay(title="Ridder")
    f1 = gcurve()
    for x in linspace(low - 1, high + 1, 200):
        f1.plot(pos=(x, f(x)), color=color.red)
    gdots(pos=[low, f(low)], color=color.yellow)
    gdots(pos=[high, f(high)], color=color.yellow)
    gdots(pos=[ridders, 0], color=color.green)

    gdisplay(title="Combination: RootSearch(white), Bisection(yellow), NewtonRaphson(blue), Ridders(green)")
    f1 = gcurve()
    for x in linspace(low - 1, high + 1, 200):
        f1.plot(pos=(x, f(x)), color=color.red)

    gdots(pos=[low, f(low)], color=color.yellow)
    gdots(pos=[high, f(high)], color=color.yellow)
    gdots(pos=[(root[0], f(root[0])), (root[1], f(root[1]))], color=color.white)
    gdots(pos=[bisect, 0], color=color.yellow)
    gdots(pos=[newton, 0], color=color.blue)
    gdots(pos=[ridders, 0], color=color.green)
Example #8
0
#!/usr/bin/python
## example4_1
from rootsearch import *


def f(x):
    return x**3 - 10.0 * x**2 + 5.0


x1 = 0.0
x2 = 1.0
for i in range(4):
    dx = (x2 - x1) / 10.0
    x1, x2 = rootsearch(f, x1, x2, dx)
x = (x1 + x2) / 2.0
print('x =', '{:6.4f}'.format(x))
input("Press return to exit")
Example #9
0
def main():
    low = float(raw_input("low = "))
    high = float(raw_input("high = "))
    step = float(raw_input("step = "))

    root = rootsearch(f, low, high, step)
    bisect = bisection(f, low, high)
    newton = newtonRaphson(f, df, low, high)
    ridders = ridder(f, low, high)

    correct = math.pi

    print "rootsearch =", root
    print "rootsearch error =", max(abs(correct - root[0]),
                                    abs(correct - root[1]))
    print ""
    print "bisection =", bisect
    print "bisection error =", abs(correct - bisect)
    print ""
    print "newtonRaphson =", newton
    print "newton error =", abs(correct - newton)
    print ""
    print "ridder =", ridders
    print "ridder error =", abs(correct - ridders)
    print ""
    print ""
    print "Average root =", ((
        (root[0] + root[1]) / 2.0) + bisect + newton + ridders) / 4.0
    print "Average root error =", abs((((
        (root[0] + root[1]) / 2.0) + bisect + newton + ridders) / 4.0) -
                                      correct)
    print ""
    print "Most Accurate root =", min(min(abs(correct - root[0]), abs(correct - root[1])), abs(correct - bisect), \
                                      abs(correct - newton), abs(correct - ridders))
    print "Least Accurate root =", max(max(abs(correct - root[0]), abs(correct - root[1])), abs(correct - bisect), \
                                      abs(correct - newton), abs(correct - ridders))

    gdisplay(title="Root Search")
    f1 = gcurve()
    for x in linspace(low - 1, high + 1, 200):
        f1.plot(pos=(x, f(x)), color=color.red)
    f2 = gdots(pos=[low, f(low)], color=color.yellow)
    gdots(pos=[high, f(high)], color=color.yellow)
    gdots(pos=[(root[0], f(root[0])), (root[1], f(root[1]))],
          color=color.green)

    gdisplay(title="Bisection")
    f1 = gcurve()
    for x in linspace(low - 1, high + 1, 200):
        f1.plot(pos=(x, f(x)), color=color.red)
    gdots(pos=[low, f(low)], color=color.yellow)
    gdots(pos=[high, f(high)], color=color.yellow)
    gdots(pos=[bisect, 0], color=color.green)

    gdisplay(title="Newton Raphson")
    f1 = gcurve()
    for x in linspace(low - 1, high + 1, 200):
        f1.plot(pos=(x, f(x)), color=color.red)
    gdots(pos=[low, f(low)], color=color.yellow)
    gdots(pos=[high, f(high)], color=color.yellow)
    gdots(pos=[newton, 0], color=color.green)

    gdisplay(title="Ridder")
    f1 = gcurve()
    for x in linspace(low - 1, high + 1, 200):
        f1.plot(pos=(x, f(x)), color=color.red)
    gdots(pos=[low, f(low)], color=color.yellow)
    gdots(pos=[high, f(high)], color=color.yellow)
    gdots(pos=[ridders, 0], color=color.green)

    gdisplay(
        title=
        "Combination: RootSearch(white), Bisection(yellow), NewtonRaphson(blue), Ridders(green)"
    )
    f1 = gcurve()
    for x in linspace(low - 1, high + 1, 200):
        f1.plot(pos=(x, f(x)), color=color.red)

    gdots(pos=[low, f(low)], color=color.yellow)
    gdots(pos=[high, f(high)], color=color.yellow)
    gdots(pos=[(root[0], f(root[0])), (root[1], f(root[1]))],
          color=color.white)
    gdots(pos=[bisect, 0], color=color.yellow)
    gdots(pos=[newton, 0], color=color.blue)
    gdots(pos=[ridders, 0], color=color.green)