Skip to content

Instantly share code, notes, and snippets.

@edo0xff
Created October 22, 2019 16:19
Show Gist options
  • Save edo0xff/f9915f073bf7f3910910dd37b0798dfd to your computer and use it in GitHub Desktop.
Save edo0xff/f9915f073bf7f3910910dd37b0798dfd to your computer and use it in GitHub Desktop.
from math import cos
from math import sin as sen
from math import tan
from math import e
def bisec(fx, a, b, iteraciones):
for i in range(iteraciones):
print "----------------------------------"
x = a
fa = eval(fx)
x = b
fb = eval(fx)
m = ((a+b)/2.0)
x = m
fm = eval(fx)
print "a= %s fa= %s" %(a, fa)
print "b= %s fb= %s" %(b, fb)
print "fm= %s" %(fm)
print "m%s = (%s - %s) / 2 = %s" %(i+1, a, b, m)
if fm < 0:
if fa>0:
# [a, m]
b = m
else:
# [m, b]
a = m
else:
if fa<0:
# [a, m]
b = m
else:
# [m, b]
a = m
funciones = [
["(x**3)-x-1", 1, 2],
["(x**4)+(x**2)-4", 1, 2],
["(x**3)+(2*x)+8", -2, -1],
["(2*(x**2))+(cos(x))-x-2", 0, 3],
["((x**2)/(cos(x)))-x-10", -1.5, 0],
["(sen(x))/((x**2)+1)", -4, -2],
["((tan(x))/x+1)-(2*x)-1", 0.1, 1],
["(e**((x**2)+1))-5", -1, 0],
["cos((x**2)+1)", 2, 3],
["(2*(x**3))-(sen((x**2)+1))", 0, 1]
]
c = 1
for f in funciones:
fx = f[0]
a = f[1]
b = f[2]
print "\n##################################"
print "%s) f(x) = %s from %s to %s" %(c, fx, a, b)
bisec(f[0], f[1], f[2], 5)
c += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment