-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRK.rb
More file actions
35 lines (26 loc) · 733 Bytes
/
Copy pathRK.rb
File metadata and controls
35 lines (26 loc) · 733 Bytes
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
#Runge-Kutta ordem 4
def rk4(x_atual, y_atual, h, intervalo)
resultados = {x_atual => y_atual}
while x_atual < intervalo.last
k1 = f(x_atual,y_atual)
k2 = f(x_atual + h/2, y_atual + (h/2)*k1)
k3 = f(x_atual + h/2, y_atual + (h/2)*k2)
k4 = f(x_atual + h, y_atual + h*k3)
y_prox = y_atual + (h/6)*(k1 + 2*k2 + 2*k3 + k4)
x_atual += h
resultados[x_atual] = y_prox
y_atual = y_prox
end
return resultados
end
#Runge-Kutta ordem 2
def rk2(x_atual, y_atual, h, intervalo)
resultados = {x_atual => y_atual}
while x_atual < intervalo.last
y_prox = y_atual + h*f(x_atual + h/2, y_atual + (h/2)*f(x_atual,y_atual))
x_atual += h
resultados[x_atual] = y_prox
y_atual = y_prox
end
return resultados
end