Runge-Kutta method: Difference between revisions

Content deleted Content added
Tikkanz (talk | contribs)
Line 211: Line 211:
</pre>
</pre>


=={{header|J}}==
'''Solution:'''
<lang j>NB.*rk4 a Solve function using Runge-Kutta method
NB. y is: y(ta) , ta , tb , tstep
NB. eg: fyp rk4 1 0 10 0.1
rk4=: 1 : 0
'Y0 a b h'=. 4{. y
T=. a + i.@>:&.(%&h) b - a
Y=. Yt=. Y0
for_t. }: T do.
ty=. t,Yt
k1=. h * u ty
k2=. h * u ty + -: h,k1
k3=. h * u ty + -: h,k2
k4=. h * u ty + h,k3
Y=. Y, Yt=. Yt + (%6) * 1 2 2 1 +/@:* k1, k2, k3, k4
end.
T ,. Y
)</lang>
'''Example:'''
<lang j> fy=: (%16) * [: *: 4 + *: NB. f(t,y)
fyp=: (* %:)/ NB. f'(t,y)
report_whole=: (10 * i. >:10)&{ NB. report at whole-numbered t values
report_err=: (, {: - [: fy {.)"1 NB. report errors

report_err report_whole fyp rk4b 1 0 10 0.1
0 1 0
1 1.5625 _1.45722e_7
2 4 _9.19479e_7
3 10.5625 _2.90956e_6
4 25 _6.23491e_6
5 52.5625 _1.08197e_5
6 100 _1.65946e_5
7 175.562 _2.35177e_5
8 289 _3.15652e_5
9 451.562 _4.07232e_5
10 676 _5.09833e_5</lang>
=={{header|Mathematica}}==
=={{header|Mathematica}}==
<lang Mathematica>DSolve[{y'[t] == t * Sqrt[y[t]], y[0] == 1, y'[0] == 0}, y[t], t]//Simplify
<lang Mathematica>DSolve[{y'[t] == t * Sqrt[y[t]], y[0] == 1, y'[0] == 0}, y[t], t]//Simplify
Line 217: Line 254:
y[t] -> 1/16*(4+t^2)^2 /. t -> Range[10]
y[t] -> 1/16*(4+t^2)^2 /. t -> Range[10]
->y[{1,2,3,4,5,6,7,8,9,10}]->{25/16, 4, 169/16, 25, 841/16, 100, 2809/16, 289, 7225/16, 676}</lang>
->y[{1,2,3,4,5,6,7,8,9,10}]->{25/16, 4, 169/16, 25, 841/16, 100, 2809/16, 289, 7225/16, 676}</lang>



=={{header|Python}}==
=={{header|Python}}==