Runge-Kutta method: Difference between revisions

Content deleted Content added
added Haskell
Tikkanz (talk | contribs)
→‎{{header|J}}: edit alternative version for clarity
Line 338: Line 338:
10 676 _5.09833e_5</lang>
10 676 _5.09833e_5</lang>


Alternative version:
'''Alternative solution:'''

<lang j>p4=: 1 :0
The following solution replaces the for loop as well as the calculation of the increments (ks) with an accumulating suffix.
<lang j>nextY=: adverb define
:
:
({:y)+6%~+/1 2 2 1*(x*[: u y+(*x&,))/\.1 0.5 0.5,x*u y
tableau=. 1 0.5 0.5, x * u y
ks=. (x * [: u y + (* x&,))/\. tableau
({:y) + 6 %~ +/ 1 2 2 1 * ks
)
)


rk4=: 1 :0
rk4=: adverb define
'Y0 a b h'=. 4{. y
'Y0 a b h'=. 4{. y
(,.[:h&(u p4)@,/\.Y0,~}.)&.|. a+i.@>:&.(%&h) b-a
T=. a + i.@>:&.(%&h) b-a
(,. [: h&(u nextY)@,/\. Y0 ,~ }.)&.|. T
)</lang>
)</lang>