-
Notifications
You must be signed in to change notification settings - Fork 2
/
integrators.jl
47 lines (39 loc) · 982 Bytes
/
integrators.jl
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
function _forward_euler(f, x_curr::Vector{Float64}, h::Float64)
return x_curr + h * f(x_curr)
end
function _RK4(f, x_curr::Vector{Float64}, h::Float64)
k1 = h * f(x_curr)
k2 = h * f(x_curr + k1 / 2)
k3 = h * f(x_curr + k2 / 2)
k4 = h * f(x_curr + k3)
return x_curr + (k1 + k2 + k3 + k4) / 6
end
"""
forward_euler
-------------
explicit Forward Euler method
Input parameters:
- h::Float64: step size
Returns:
A function that takes as input a derivative function and a vector and returns
the next iterate.
"""
function forward_euler(h::Float64)
ret(f, x_curr::Vector{Float64}) = _forward_euler(f, x_curr, h)
return ret
end
"""
RK4
-------------
Fourth-order explicit Runge-Kutta method
Input parameters:
- h::Float64: step size
Returns:
A function that takes as input a derivative function and a vector and returns
the next iterate.
"""
function RK4(h::Float64)
ret(f, x_curr::Vector{Float64}) = _RK4(f, x_curr, h)
return ret
end
;