def tridi101(dpsv, nv, epsv): print ">>>>>> tridi(1,0,1)" print "{0:>4s} {1:>4s} {2:>10s} {3:>15s} {4:>5s} {5:>10s}".\ format("n", "dps", "eps", "errmaxnorm", "z", "sec") for dpsi,ni,epsi in product(dpsv,nv,epsv): mp.dps = dpsi lk = lambda k: 2*cos((ni-k+1)*pi/(ni+1)) se = matrix([map(lk, linspace(1,ni,ni))]) t1 = time.time() s, z = bisection(zeros(ni,1), ones(ni-1,1), epsi, 0, ni-1) t2 = time.time() err = norm(s-se, inf) print "{0:4d} {1:4d} {2:>10s} {3:>15s} {4:5d} {5:10f}".format(ni, dpsi, \ nstr(epsi), \ nstr(err), z, \ (t2-t1))
from bisection import * from newton import * from secant import * print("-------- START ---------") print("Bisection") f = "x*sin(x)-1" a = 0 b = 2 tol = 0.001 print(bisection(f, a, b, tol)) print("\nNewton-Raphson") f = "exp(-x)-x" x0 = 0 tol = 0.0001 maxIter = 20 print(newton(f, x0, tol, maxIter)) print("\nSecant") f = "x^3-3*x+2" x0 = -2.6 x1 = -2.4 tol = 0.0001 maxIter = 20 print(secant(f, x0, x1, tol, maxIter)) print("-------- END ----------")
import numpy as np from bisection import* def f(x): return x**3 - 7.0*x + 1.0 x = bisection(f, 2.0, 4.0, tol = 1.0e-4) print('\nx =', '{:6.4f}'.format(x))
# ------------------------------------------------------------ # use esta funcion de prueba def f(x): return x * x * x - 2 * x - 10 def g(x): return (x * 2 + 10) / x / x # ------------------------------------------------------------ # buscar raices a, b = rootSearch(f, -10, 10, 0.5, printText=1) # bisection print "\nMetodo Bisection" print bisection(f, a, b, switch=1) # brent print "\nMetodo Brent" print brent(f, a, b, printText=0) # secant print "\nMetodo Secant" print secant(f, a, b, printText=0) # newton print "\nMetodo de Newton" print newton(f, d1f, a, printText=0) # newton raphson print "\nMetodo de Newton-Raphson" print newtonRaphson(f, d1f, a, b, printText=0) # posicion falsa print "\nMetodo Posicion Falsa o Falsa Regla" print falseRule(f, a, b, printText=0)
from bisection import * from newtonraphson import * if len(argv) != 2: print("Usage: ", str(argv[0]), "mode-number") print("1 : Bisection\n2 : Newton-Raphson\n3 : Secant") exit(1) def fPrime(t, v=335.0, u=2510.0, M0=2.8 * 1e6, m=13.3 * 1e3, g=9.81): return float((u * m) / (M0 - m * t) - g) def func(t, v=335.0, u=2510.0, M0=2.8 * 1e6, m=13.3 * 1e3, g=9.81): return float(u * log((M0) / (M0 - m * t)) - g * t - v) mode = int(argv[1]) if (mode == 1): print("Using Bisection Method: ") print(bisection(func, 0, 200)) elif (mode == 2): print("Using Newton-Raphson Method: ") print(newtonraphson(func, fPrime, 60)) elif (mode == 3): print("Using Secant Method: ") print(secant(func, [0, 200], 10)) else: print("Undefined Mode NUmber")
import numpy as np from newton_raphson import * from ridder import * from bisection import * def f(x): return x**10 - 1 def df(x): return 10 * x**9 x0 = 0.5 xroot, _ = newton_raphson(f, df, x0, verbose=True) xroot, _ = bisection(f, 0.5, 1.25, verbose=True) xroot, _ = ridder(f, 0.5, 1.25, verbose=True)
import math from bisection import * def f(x): return x * math.exp(-x) - 0.06064 alpha = 0.0646926359947960 a0 = 0 b0 = 1 _range = (a0, b0) bisection(f, _range, alpha, 15)
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)
def answer(self): I1 = bisection(self.X.get(), int(self.nIter.get()), float(self.tolerance.get()), float(self.x0.get()), float(self.x1.get())) self.label["text"] = I1.biseccion()
import math from bisection import * def f(x): return pow(x, 2) - 2 * x - math.atan(7 * x - 2) alpha1 = 0.225333477 _range = (0, 1) print(bisection(f, _range, alpha1)) alpha2 = 2.58389241 _range = (2, 3) print(bisection(f, _range, alpha2))
#!/usr/bin/python ## example4_2 from bisection import * def f(x): return x**3 - 10.0 * x**2 + 5.0 x = bisection(f, 0.0, 1.0, tol=1.0e-4) print('x =', '{:6.4f}'.format(x)) input("Press return to exit")
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' pylab.text(2,100,r'Example 4.3, pp. 150-151') pylab.title(my_title) pylab.ylabel("f(x)")
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)
#Imports from bisection import * #Constants LowerLimit = 0.0 UpperLimit = 10.0 def f(x): return (x)**2 Root = bisection(f,LowerLimit,UpperLimit) print Root
#!/usr/bin/python ## example4_3 import math from rootsearch import * from bisection import * def f(x): return x - math.tan(x) a, b, dx = (0.0, 20.0, 0.01) print("The roots are:") while True: x1, x2 = rootsearch(f, a, b, dx) if x1 != None: a = x2 root = bisection(f, x1, x2, 1) if root != None: print(root) else: print("Done") break input("Press return to exit")
from bisection import * from regula_falsi import * def f(x): return x**3 - 75 x1 = 3.0 x2 = 5.0 x, err = bisection(f, x1, x2, TOL=1e-10, verbose=True) print("Final root = %18.10f" % x) print("err = %18.10e" % err) x, err = regula_falsi(f, x1, x2, TOL=1e-10, verbose=True) print("Final root = %18.10f" % x) print("err = %18.10e" % err)
#Imports from bisection import * #Constants LowerLimit = 0.0 UpperLimit = 10.0 def f(x): return (x)**2 Root = bisection(f, LowerLimit, UpperLimit) print Root