Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from scipy.integrate import solve_ivp | |
| import gradio as gr | |
| # Sistema no lineal: competencia entre dos empresas | |
| def sistema(t, z, a, b): | |
| x, y = z | |
| dxdt = x * (1 - x) - a * x * y | |
| dydt = y * (1 - y) - b * x * y | |
| return [dxdt, dydt] | |
| # Jacobiano del sistema para análisis cualitativo | |
| def jacobiano(x, y, a, b): | |
| j11 = 1 - 2*x - a*y | |
| j12 = -a*x | |
| j21 = -b*y | |
| j22 = 1 - 2*y - b*x | |
| return np.array([[j11, j12], [j21, j22]]) | |
| # Clasificación del punto crítico | |
| def clasificar_critico(jac): | |
| tr = np.trace(jac) | |
| det = np.linalg.det(jac) | |
| disc = tr**2 - 4*det | |
| if det < 0: | |
| return "Punto silla" | |
| elif disc < 0: | |
| return "Foco" + (" estable" if tr < 0 else " inestable") | |
| elif disc == 0: | |
| return "Nodo degenerado" | |
| else: | |
| return "Nodo" + (" estable" if tr < 0 else " inestable") | |
| # Simulación y graficación del plano fase | |
| def simular(x0, y0, a, b): | |
| t_span = (0, 20) | |
| z0 = [x0, y0] | |
| sol = solve_ivp(sistema, t_span, z0, args=(a, b), t_eval=np.linspace(*t_span, 1000)) | |
| fig, ax = plt.subplots(figsize=(6, 6)) | |
| # Traza de la solución | |
| ax.plot(sol.y[0], sol.y[1], label="Trayectoria", color="blue") | |
| ax.plot([sol.y[0][0]], [sol.y[1][0]], 'go', label="Inicio") | |
| ax.plot([sol.y[0][-1]], [sol.y[1][-1]], 'ro', label="Fin") | |
| # Puntos críticos conocidos | |
| puntos = [(0,0), (1,0), (0,1), (1,1)] | |
| for px, py in puntos: | |
| if 0 <= px <= 1 and 0 <= py <= 1: | |
| jac = jacobiano(px, py, a, b) | |
| tipo = clasificar_critico(jac) | |
| ax.plot(px, py, 'ko') | |
| ax.text(px + 0.02, py + 0.02, tipo, fontsize=8) | |
| ax.set_xlim(0, 1.1) | |
| ax.set_ylim(0, 1.1) | |
| ax.set_xlabel("x: Cuota de mercado empresa A") | |
| ax.set_ylabel("y: Cuota de mercado empresa B") | |
| ax.set_title("Plano fase de competencia entre dos empresas") | |
| ax.legend() | |
| ax.grid(True) | |
| plt.tight_layout() | |
| return fig | |
| # Interfaz Gradio | |
| interfaz = gr.Interface( | |
| fn=simular, | |
| inputs=[ | |
| gr.Slider(0, 1, value=0.5, label="x₀: Cuota inicial empresa A"), | |
| gr.Slider(0, 1, value=0.5, label="y₀: Cuota inicial empresa B"), | |
| gr.Slider(0, 1, value=0.5, label="a: Impacto de B sobre A"), | |
| gr.Slider(0, 1, value=0.3, label="b: Impacto de A sobre B") | |
| ], | |
| outputs=gr.Plot(), | |
| title="Competencia entre dos empresas con EDO no lineales", | |
| description="Simulador cualitativo con plano fase, puntos críticos y análisis de estabilidad.\nProfesor E. Cristian J. Trujillo - Curso: Ecuaciones Diferenciales para Ingeniería" | |
| ) | |
| interfaz.launch() | |