CALCULO DE LA DERIVADA DE UNA FUNCION Y SU GRAFICA
import numpy as np
import matplotlib.pyplot as plt
from sympy import symbols, diff
# Definir la variable y la función, por ejemplo, f(x) = x^2
x = symbols('x')
func = x**2
# Calcular la derivada
derivative_func = diff(func, x)
# Funciones para evaluación numérica
f_lambdified = np.vectorize(lambda x_val: func.subs(x, x_val).evalf())
derivative_lambdified = np.vectorize(lambda x_val: derivative_func.subs(x, x_val).evalf())
# Generar valores x
x_vals = np.linspace(-10, 10, 400)
y_vals = f_lambdified(x_vals)
dy_vals = derivative_lambdified(x_vals)
# Crear trazados
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.plot(x_vals, y_vals, label='f(x) = x^2')
plt.title('Función Original')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(x_vals, dy_vals, label="f'(x) = 2x", color='red')
plt.title('Derivada de la Función')
plt.xlabel('x')
plt.ylabel("f'(x)")
plt.legend()
# Ajustar el diseño y mostrar las gráficas
plt.tight_layout()
plt.show()
No hay comentarios.:
Publicar un comentario