import matplotlib.pyplot as plt
import numpy as np

# Datos experimentales para un MRUA
tiempos = np.array([0, 2.90, 4.74, 5.73, 6.70, 7.65, 8.48, 9.17, 9.84])
distancias = np.array([0, 2, 4, 6, 8, 10, 12, 14, 16])

#Esperimento PhyPhox
#tiempos = np.array([0.382, 0.434, 0.488, 0.539, 0.586, 0.629, 0.66, 0.702])
#distancias = np.array([0.70, 0.95, 1.2, 1.45, 1.70, 1.95, 2.2, 2.45])
	
# Calcular velocidades promedio por tramo
velocidades = (distancias[1:] - distancias[:-1]) / (tiempos[1:] - tiempos[:-1])

# Tiempos intermedios para las velocidades
tiempos_intermedios = (tiempos[1:] + tiempos[:-1]) / 2

# Ajuste parabólico para distancia vs. tiempo
coef_parabola, cov_parabola = np.polyfit(tiempos, distancias, 2, cov=True)
a, b, c = coef_parabola
error_a, error_b, error_c = np.sqrt(np.diag(cov_parabola))

# Ajuste lineal para velocidad vs. tiempo
coef_lineal, cov_lineal = np.polyfit(tiempos_intermedios, velocidades, 1, cov=True)
m, b_vel = coef_lineal
error_m, error_b_vel = np.sqrt(np.diag(cov_lineal))

# Crear las curvas de ajuste
tiempo_ajuste = np.linspace(min(tiempos), max(tiempos), 100)
distancia_ajuste = a * tiempo_ajuste**2 + b * tiempo_ajuste + c
velocidad_ajuste = m * tiempo_ajuste + b_vel

# Calcular las velocidades predichas por el modelo lineal
velocidades_predichas = m * tiempos_intermedios + b_vel

# Calcular R² para velocidad vs. tiempo
ss_total = np.sum((velocidades - np.mean(velocidades))**2)
ss_residual = np.sum((velocidades - velocidades_predichas)**2)
r2_velocidad = 1 - (ss_residual / ss_total)

# Gráfico distancia vs. tiempo
plt.figure(figsize=(10, 5))
plt.scatter(tiempos, distancias, color='blue', s=50)
plt.plot(tiempo_ajuste, distancia_ajuste, color='orange', linestyle='--', label=f'Ajuste Cuadrático: $y = {a:.3f}x^2 + {b:.3f}x + {c:.3f}$')
#plt.title('Distancia vs. Tiempo (MRUA)', fontsize=18)
plt.xlabel('Tiempo (s)', fontsize=16)
plt.ylabel('Distancia (m)', fontsize=16)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.legend(fontsize=12)
plt.grid(True)
plt.tight_layout()
plt.show()

# Gráfico velocidad vs. tiempo
plt.figure(figsize=(10, 5))
plt.scatter(tiempos_intermedios, velocidades, color='red', s=50)
plt.plot(tiempo_ajuste, velocidad_ajuste, color='green', linestyle='--', label=f'Ajuste Lineal: $y = {m:.3f}x + {b_vel:.3f}$\n$R^2 = {r2_velocidad:.3f}$')
#plt.title('Velocidad vs. Tiempo (MRUA)', fontsize=18)
plt.xlabel('Tiempo (s)', fontsize=16)
plt.ylabel('Velocidad (m/s)', fontsize=16)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.legend(fontsize=12)
plt.grid(True)
plt.tight_layout()
plt.show()

# Calcular y mostrar las posiciones y velocidades promedio por tramo
print("Tiempos, Posiciones y Velocidades Promedio, por tramo:")
print("="*70)
print(f"{'Tramo':<8}{'t1 (s)':<12}{'t2 (s)':<12}{'x1 (m)':<12}{'x2 (m)':<12}{'v (m/s)':<12}")
print("="*70)
for i, (t1, t2, x1, x2, v) in enumerate(zip(tiempos[:-1], tiempos[1:], distancias[:-1], distancias[1:], velocidades)):
    print(f"{i+1:<8}{t1:<12.6f}{t2:<12.6f}{x1:<12.6f}{x2:<12.6f}{v:<12.6f}")
print("="*70)

# Resultados del ajuste parabólico - Distancia vs Tiempo
print("\nResultados del ajuste parabólico - Distancia vs. Tiempo")
print("="*60)
print(f" Coeficiente cuadrático (a): {a:.3f} ± {error_a:.3f} m/s²")
print(f" Coeficiente lineal (b): {b:.3f} ± {error_b:.3f} m/s")
print(f" Término independiente (c): {c:.3f} ± {error_c:.3f} m")
# Calcular aceleración y su error
aceleracion = 2*a
error_aceleracion = 2*error_a
print(f" Por tanto, la aceleración es: {aceleracion:.3f} ± {error_aceleracion:.3f} m/s²")
print("="*60)

# Resultados del ajuste lineal - Velocidad vs Tiempo
velocidades_predichas = m * tiempos_intermedios + b_vel
print("\nResultados del ajuste lineal - Velocidad vs. Tiempo")
print("="*60)
print(f" Pendiente (aceleración): {m:.3f} ± {error_m:.3f} m/s²")
print(f" Ordenada al origen: {b_vel:.3f} ± {error_b_vel:.3f} m/s")
print(f" Coeficiente de determinación (R²): {r2_velocidad:.3f}")
print("="*60)

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: