Skip to content

Commit 48b543a

Browse files
committed
Update RK-4
Changed the main loop to simplify, moved the update at the end of the loop to the beginning. Makes it a little cleaner though there is an extra scale and add step (the first step it is scaling a zero vector by zero and adding it y_t)
1 parent 92eb811 commit 48b543a

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

math/rk4/rk4.bas

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,21 @@ Function rk4step(fname$,yt(),t,dt) as integer
2525
Static integer n = Bound(yt(),1)
2626

2727
'rk arrays
28-
Static float dts(2+l) = (0.5*dt,0.5*dt,dt)
28+
Static float dts(3+l) = (0.0,0.5*dt,0.5*dt,dt)
2929
Static float rks(3+l) = (dt/6,dt/3,dt/3,dt/6)
3030
Static float s(n),a(n),k(n)
3131
Array Set 0,s()
32-
Array Add yt(),0,k()
32+
Array Set 0,k()
3333

3434
'integration step
3535
Local integer i
3636
For i=l To 3+l
37-
'k_{i} = f(t+dts_i,y_{t}+dts_i*k_i)
37+
'y_{t} + dts_i * k_{i-1}
38+
Math scale k(),dts(i),k()
39+
Math c_add yt(),k(),k()
40+
t = t + dts(i)
41+
42+
'k_{i} = f(t+dts_i,y_{t}+dts_i*k_{i-1})
3843
flag = Call(fname$,t,k(),k())
3944
If flag=0 Then
4045
rk4step=0
@@ -44,12 +49,6 @@ Function rk4step(fname$,yt(),t,dt) as integer
4449
'\sum_{i=1}^4 rks_i*k_i
4550
Math scale k(),rks(i),a()
4651
Math c_add s(),a(),s()
47-
If i<(3+l) Then
48-
'y_{t} + dts_i * k_i
49-
Math scale k(),dts(i),k()
50-
Math c_add yt(),k(),k()
51-
t = t + dts(i)
52-
End If
5352
Next i
5453

5554
'y_{t+dt} = y_{t} + \sum

0 commit comments

Comments
 (0)