def runge_kutta_4(f, t0, t_fin, y0, h): resultados_y = [] resultados_t = [] t = t0 w = y0 resultados_y.append(w) resultados_t.append(t) i = 1 while t < t_fin: k1 = h * f(t, w) k2 = h * f(t + h / 2, w + k1 / 2) k3 = h * f(t + h / 2, w + k2 / 2) k4 = h * f(t + h, w + k3) w = w + (k1 + 2 * k2 + 2 * k3 + k4) / 6 t = t0 + i * h resultados_y.append(w) resultados_t.append(t) i = i + 1 temps_a_celsius(resultados_y) tiempos_a_minutos(resultados_t) return [resultados_y, resultados_t]
def ej2(): print("Generando Ejercicio 2...") os.makedirs('ej2', exist_ok=True) os.chdir('ej2') file = open('ej2.txt', 'w') T0 = 20 + 273 t0 = 0 t_fin = NBOL * CADE # Optamos por usar los valores de RK valores_runge = runge_kutta_4(intercambio_total(TMP1, TMP2), t0, t_fin, T0, CADE) xx = np.linspace(t0, t_fin) yy = temperatura_exacta(T0)(xx) plt.figure(figsize=(10, 7)) tiempos_a_minutos(xx) temps_a_celsius(yy) plt.plot(xx, yy, lw=2, label='Temperatura Exacta') plt.plot(valores_runge[1], valores_runge[0], 'b^', label='Runge-Kutta 4') plt.legend(loc='lower right') plt.xlabel('t [min]') plt.ylabel('T(t) [°C]') plt.grid(True) plt.savefig('ej2.png') buscar_intervalo_temperatura(valores_runge, TMP2 - 10 - 273, file) os.chdir('../') print("")
def resolver_caso_ej5(sk_obj, tsk_obj, caso, file=sys.stdout): j_inv = np.matrix([[0.25, 0.75], [0.75, 0.25]]) err = 0.1 t0 = 0 T0 = 20 + 273 t_fin = NBOL * CADE x, n_iter = punto_fijo_sistema( sistema_funciones(tsk_obj, sk_obj), sistema_funciones(tsk_obj, sk_obj)(np.matrix([[TMP1], [TMP2]])), err, j_inv) print('{0:4}\t{1: .14f}\t{2: .14f}\t{3: .14f}\t{4:4}'.format( sk_obj, tsk_obj, x.item(0) - 273, x.item(1) - 273, n_iter), file=file) valores_runge = runge_kutta_4(intercambio_total(x.item(0), x.item(1)), t0, t_fin, T0, CADE) xx = np.linspace(t0, t_fin) yy = temperatura_exacta(T0)(xx) plt.figure(figsize=(10, 7)) tiempos_a_minutos(xx) temps_a_celsius(yy) plt.plot(xx, yy, lw=2, label='Temperatura Exacta') plt.plot(valores_runge[1], valores_runge[0], 'b^', label='Runge-Kutta 4') plt.legend(loc='lower right') plt.xlabel('t [min]') plt.ylabel('T(t) [°C]') plt.grid(True) plt.savefig('ej5' + caso + '.png')
def ej1(): print("Generando Ejercicio 1...") os.makedirs('ej1', exist_ok=True) os.chdir('ej1') T0 = 20 + 273 t0 = 0 t_fin = NBOL * CADE valores_euler = euler(intercambio_conveccion(TMP1, TMP2), t0, t_fin, T0, CADE) valores_runge = runge_kutta_4(intercambio_conveccion(TMP1, TMP2), t0, t_fin, T0, CADE) valores_exactos = buscar_valores_exactos(t0, t_fin, T0, CADE) xx = np.linspace(t0, t_fin) yy = temperatura_exacta(T0)(xx) plt.figure(figsize=(10, 7)) tiempos_a_minutos(xx) temps_a_celsius(yy) plt.plot(xx, yy, lw=2, label='Temperatura Exacta') plt.plot(valores_euler[1], valores_euler[0], 'gs', label='Euler') plt.plot(valores_runge[1], valores_runge[0], 'b^', label='Runge-Kutta 4') plt.xlabel('t [min]') plt.ylabel('T(t) [°C]') plt.legend(loc='lower right') plt.grid(True) plt.savefig('ej1.png') plt.figure(figsize=(10, 7)) plt.yscale('log') plt.plot(valores_euler[1], buscar_error(valores_exactos[0], valores_euler[0]), 'gs', label='Euler') plt.plot(valores_runge[1], buscar_error(valores_exactos[0], valores_runge[0]), 'b^', label='Runge-Kutta 4') plt.legend(loc='lower right') plt.xlabel('t [min]') plt.ylabel('error') plt.grid(True) plt.savefig('ej1-error.png') os.chdir('../') print("")
def buscar_valores_exactos(t0, t_fin, T0, CADE): arr_t = [] arr_T = [] arr_t.append(t0) arr_T.append(T0) i = 1 t = t0 while t < t_fin: t = t0 + i * CADE T = temperatura_exacta(T0)(t) arr_t.append(t) arr_T.append(T) i = i + 1 temps_a_celsius(arr_T) tiempos_a_minutos(arr_t) return [arr_T, arr_t]
def euler(f, t0, t_fin, y0, h): resultados_y = [] resultados_t = [] w = y0 t = t0 resultados_y.append(w) resultados_t.append(t) i = 1 while t < t_fin: w = w + h * f(t, w) t = t0 + i * h resultados_y.append(w) resultados_t.append(t) i = i + 1 temps_a_celsius(resultados_y) tiempos_a_minutos(resultados_t) return [resultados_y, resultados_t]