-
Notifications
You must be signed in to change notification settings - Fork 2
/
rk2.py
74 lines (51 loc) · 1.38 KB
/
rk2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Implementa la solucion de RK2 para el problema de un
pendulo real con largo 1.
"""
import numpy as np
import matplotlib.pyplot as plt
# constantes
g = 9.8
# condiciones iniciales
phi_t0 = np.pi / 2.
w_t0 = 0.
# Solucion para pequeñas oscilaciones
freq = np.sqrt(g)
t = np.linspace(0, 4 * 2 * np.pi / freq, 400)
phi_po = phi_t0 * np.cos(freq * t)
plt.figure(1)
plt.clf()
plt.plot(t, phi_po, label=u'pequeñas oscilaciones')
# Usando RK2
def f(phi, omega):
output = [omega, -g * np.sin(phi)]
return output
def calc_k1(f, phi_n, omega_n, h):
fn = f(phi_n, omega_n)
output = [h * fn[0], h * fn[1]]
return output
def calc_k2(f, phi_n, omega_n, h):
k1 = calc_k1(f, phi_n, omega_n, h)
f_middle = f(phi_n + k1[0]/2, omega_n + k1[1]/2)
output = [h * f_middle[0], h * f_middle[1]]
return output
def rk2_step(f, phi_n, omega_n, h):
k2 = calc_k2(f, phi_n, omega_n, h)
phi_next = phi_n + k2[0]
omega_next = omega_n + k2[1]
output = [phi_next, omega_next]
return output
Nsteps = 400
h = 4 * 2 * np.pi / freq / Nsteps
phi_arr = np.zeros(Nsteps)
omega_arr = np.zeros(Nsteps)
# Condiciones iniciales
phi_arr[0] = phi_t0
omega_arr[0] = w_t0
for i in range(1, Nsteps):
phi_arr[i], omega_arr[i] = rk2_step(f, phi_arr[i-1], omega_arr[i-1], h)
plt.plot(t, phi_arr, label='rk2')
plt.legend()
plt.show()