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)
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()