#@title Librerias import numpy as np import matplotlib.pyplot as plt f = lambda x: np.exp(x) g = lambda x: 1/x x = np.linspace(.1, 2, 100) plt.plot(x, f(x), label='$e^x$') plt.plot(x, g(x), label='$1/x$') plt.xlabel('x') plt.ylabel('f(x)') plt.grid(True) plt.legend() plt.show() f = lambda x: x*np.exp(x) - 1 x = np.linspace(-2, 2, 100) plt.plot(x, f(x), label = '$f(x)=x*e^x-1$') plt.axhline(y=0, color='m', linestyle = '--') plt.legend() plt.grid(True) plt.show() import numpy as np from scipy.optimize import newton # Parámetros M = 1.0 e = 0.6 # Definición de la función f(E) = E - e*sin(E) - M def kepler_eq(E): return E - e * np.sin(E) - M E = np.linspace(0, 2*np.pi, 100) plt.plot(E, kepler_eq(E), label = '$E - 0.6 \\sin E = 1$') plt.axhline(y=0, color='m', linestyle = '--') plt.xlabel('E') plt.ylabel('f(E)') plt.legend() plt.grid(True) plt.show() # Resolución usando el método de Brent sol = newton(kepler_eq, x0 = 1.0) print("Anomalía excéntrica E:", sol) from scipy.optimize import bisect, newton, root_scalar, fsolve def f(x): ''' función a la que queremos encontrar la raíz x^3 - 2*x^2 - 5 = 0 ''' return x**3 - 2*x**2 - 5 x = np.linspace(-4, 4, 100) plt.plot(x, f(x), label = '$f(x)=x^3-2x^2-5$') plt.legend() plt.axhline(y=0, color='m', linestyle = '--') plt.xlabel('x') plt.ylabel('f(x)') plt.grid(True) plt.show() raiz_biseccion = bisect(f, 2, 3) print("Raíz (Bisección):", raiz_biseccion) def f(x): return x**3 - 2*x**2 - 5 def df(x): return 3*x**2 - 4*x # Usando derivada explícita raiz_newton = newton(f, x0=2.5, fprime=df) print("Raíz (Newton-Raphson):", raiz_newton) # Usando derivada aproximada (diferencias finitas) raiz_newton_aprox = newton(f, x0=2.5) print("Raíz (Newton sin derivada explícita):", raiz_newton_aprox) # Método de la secante: se especifican dos valores iniciales raiz_secante = newton(f, x0=2, x1=3) print("Raíz (Secante):", raiz_secante) raiz_fsolve = fsolve(f, x0=2.5) print("Raíz (fsolve):", raiz_fsolve[0]) def sistema(vars): x, y = vars return [x**2 + y**2 - 4, x*y - 1] solucion_sistema = fsolve(sistema, [1, 1]) print("Solución del sistema:", solucion_sistema) # Polinomio: x³ - 2x² - 5 coeficientes = [1, -2, 0, -5] raices = np.roots(coeficientes) print("Raíces del polinomio:", raices)