import math
from matplotlib import pyplot as plt
import numpy as np
def f(x):
return (x ** 3) - x - 1
def biseccion(f, a, b, tol = 10**4, n = 50):
i=1
delta = 0.1
if f(a)*f(b) >= 0: # el intevalo escogido no sirve
print('El intervalo [{}.{}] no tiene raíces'.format(a,b))
else:
while i <= n:
c = (a+b)/2
print('ite {:<2}: a_{:<2}={:.7f} , b_{:<2}={:.7f}, c_{:<2}={:.7f}'.format(i,i-1,a,i-1,b,i,c))
if f(c + delta) * f(c-delta) < 0:
return c
if f(a) * f(c) < 0:
b - c
elif f(b) * f(c) < 0:
a - c
i = i + 1
print("Solución no encontrada, iteraciones agotadas {}".format(i-1))
return
def graficar(f, ini, fin, ite = 1000):
x = np.linspace(ini, fin, ite)
fig, ax = plt.subplots(figsize = (15, 5))
ax.plot(x, f(x))
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()
ax.annotate("", xy=(xmax, 0), xytext=(xmin, 0),arrowprops=dict(color='gray', width=1.5, headwidth=8, headlength=10))
ax.annotate("", xy=(0, ymax), xytext=(0, ymin),arrowprops=dict(color='gray', width=1.5, headwidth=8, headlength=10))
plt.title('Funcion de Biseccion:Grafica de $x^{3}-x-1$')
plt.show()
return None
graficar(f, 0, 2)
biseccion(f, 0, 2)
ite 1 : a_0 =0.0000000 , b_0 =2.0000000, c_1 =1.0000000
ite 2 : a_1 =0.0000000 , b_1 =2.0000000, c_2 =1.0000000
ite 3 : a_2 =0.0000000 , b_2 =2.0000000, c_3 =1.0000000
ite 4 : a_3 =0.0000000 , b_3 =2.0000000, c_4 =1.0000000
ite 5 : a_4 =0.0000000 , b_4 =2.0000000, c_5 =1.0000000
ite 6 : a_5 =0.0000000 , b_5 =2.0000000, c_6 =1.0000000
ite 7 : a_6 =0.0000000 , b_6 =2.0000000, c_7 =1.0000000
ite 8 : a_7 =0.0000000 , b_7 =2.0000000, c_8 =1.0000000
ite 9 : a_8 =0.0000000 , b_8 =2.0000000, c_9 =1.0000000
ite 10: a_9 =0.0000000 , b_9 =2.0000000, c_10=1.0000000
ite 11: a_10=0.0000000 , b_10=2.0000000, c_11=1.0000000
ite 12: a_11=0.0000000 , b_11=2.0000000, c_12=1.0000000
ite 13: a_12=0.0000000 , b_12=2.0000000, c_13=1.0000000
ite 14: a_13=0.0000000 , b_13=2.0000000, c_14=1.0000000
ite 15: a_14=0.0000000 , b_14=2.0000000, c_15=1.0000000
ite 16: a_15=0.0000000 , b_15=2.0000000, c_16=1.0000000
ite 17: a_16=0.0000000 , b_16=2.0000000, c_17=1.0000000
ite 18: a_17=0.0000000 , b_17=2.0000000, c_18=1.0000000
ite 19: a_18=0.0000000 , b_18=2.0000000, c_19=1.0000000
ite 20: a_19=0.0000000 , b_19=2.0000000, c_20=1.0000000
ite 21: a_20=0.0000000 , b_20=2.0000000, c_21=1.0000000
ite 22: a_21=0.0000000 , b_21=2.0000000, c_22=1.0000000
ite 23: a_22=0.0000000 , b_22=2.0000000, c_23=1.0000000
ite 24: a_23=0.0000000 , b_23=2.0000000, c_24=1.0000000
ite 25: a_24=0.0000000 , b_24=2.0000000, c_25=1.0000000
ite 26: a_25=0.0000000 , b_25=2.0000000, c_26=1.0000000
ite 27: a_26=0.0000000 , b_26=2.0000000, c_27=1.0000000
ite 28: a_27=0.0000000 , b_27=2.0000000, c_28=1.0000000
ite 29: a_28=0.0000000 , b_28=2.0000000, c_29=1.0000000
ite 30: a_29=0.0000000 , b_29=2.0000000, c_30=1.0000000
ite 31: a_30=0.0000000 , b_30=2.0000000, c_31=1.0000000
ite 32: a_31=0.0000000 , b_31=2.0000000, c_32=1.0000000
ite 33: a_32=0.0000000 , b_32=2.0000000, c_33=1.0000000
ite 34: a_33=0.0000000 , b_33=2.0000000, c_34=1.0000000
ite 35: a_34=0.0000000 , b_34=2.0000000, c_35=1.0000000
ite 36: a_35=0.0000000 , b_35=2.0000000, c_36=1.0000000
ite 37: a_36=0.0000000 , b_36=2.0000000, c_37=1.0000000
ite 38: a_37=0.0000000 , b_37=2.0000000, c_38=1.0000000
ite 39: a_38=0.0000000 , b_38=2.0000000, c_39=1.0000000
ite 40: a_39=0.0000000 , b_39=2.0000000, c_40=1.0000000
ite 41: a_40=0.0000000 , b_40=2.0000000, c_41=1.0000000
ite 42: a_41=0.0000000 , b_41=2.0000000, c_42=1.0000000
ite 43: a_42=0.0000000 , b_42=2.0000000, c_43=1.0000000
ite 44: a_43=0.0000000 , b_43=2.0000000, c_44=1.0000000
ite 45: a_44=0.0000000 , b_44=2.0000000, c_45=1.0000000
ite 46: a_45=0.0000000 , b_45=2.0000000, c_46=1.0000000
ite 47: a_46=0.0000000 , b_46=2.0000000, c_47=1.0000000
ite 48: a_47=0.0000000 , b_47=2.0000000, c_48=1.0000000
ite 49: a_48=0.0000000 , b_48=2.0000000, c_49=1.0000000
ite 50: a_49=0.0000000 , b_49=2.0000000, c_50=1.0000000
Solución no encontrada, iteraciones agotadas 50
import numpy as np
import matplotlib.pyplot as plt
def f(x):
try:
return np.sin(x)
except ZeroDivisionError:
return infi
def f1(x):
try:
return np.sin(x)+ np.sin(5*x)
except ZeroDivisionError:
return infi
def f2(x):
try:
return np.sin(x)*np.exp(-x/10)
except ZeroDivisionError:
return infi
plt.figure(figsize=(60, 10))
plt.subplot(2,6,1)
x = np.linspace(0,10*np.pi,800)
plt.plot(x , f(x))
plt.plot(x, f(x),label=r'Funcion 1',color="royalblue")
plt.title('Grafica de cos(x) usando linspace de 10')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.subplot(2,6,1)
x = np.linspace(0,10*np.pi,800)
plt.title('Grafica de cos(x) usando linspace de 100')
plt.xlabel('x')
plt.ylabel('y')
plt.plot(x , f1(x))
plt.plot(x, f1(x),label=r'cos(x)',color="orange")
plt.legend()
plt.subplot(2,6,1)
x = np.linspace(0,10*np.pi,800)
plt.title('Grafica de cos(x) usando linspace de 100')
plt.xlabel('x')
plt.ylabel('y')
plt.plot(x , f2(x))
plt.plot(x, f2(x),label=r'cos(x)',color="yellowgreen")
plt.legend()
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
figura = plt.figure(figsize=(8,6))
xd3 = plt.axes(projection="3d")
xdata = np.linspace(-20,20,100)
ydata = np.linspace(-20,20,100)
x,y = np.meshgrid(xdata,ydata)
z = np.sin((np.sqrt((x*3)+(y*3))/(x*3)+(y*3)))
xd3 = plt.axes(projection='3d')
xd3.plot_surface(x, y, z,cmap='plasma')
xd3.set_title('GRAFICA DE SUPERFICIE')
xd3.set_xlabel('EJE X')
xd3.set_ylabel('EJE Y')
xd3.set_zlabel('EJE Z')
plt.show()
/shared-libs/python3.7/py-core/lib/python3.7/site-packages/ipykernel_launcher.py:14: RuntimeWarning: invalid value encountered in sqrt