Runge-Kutta method: Difference between revisions
Content deleted Content added
Added Standard ML implementation |
Added zkl |
||
Line 1,137:
y(9.0) = 451.56245928 Error: 4.07231581e-05
y(10.0) = 675.99994902 Error: 5.09832864e-05</pre>
=={{header|zkl}}==
{{trans|OCaml}}
<lang zkl>fcn yp(t,y) { t * y.sqrt() }
fcn exact(t){ u:=0.25*t*t + 1.0; u*u }
fcn rk4_step([(y,t)],h){
k1:=h * yp(t,y);
k2:=h * yp(t + 0.5*h, y + 0.5*k1);
k3:=h * yp(t + 0.5*h, y + 0.5*k2);
k4:=h * yp(t + h, y + k3);
T(y + (k1+k4)/6.0 + (k2+k3)/3.0, t + h);
}
fcn loop(h,n,[(y,t)]){
if(n % 10 == 1)
print("t = %f,\ty = %f,\terr = %g\n".fmt(t,y,(y - exact(t)).abs()));
if(n < 102) return(loop(h,(n+1),rk4_step(T(y,t),h))) //tail recursion
}</lang>
{{out}}
<pre>
loop(0.1,1,T(1.0, 0.0))
t = 0.000000, y = 1.000000, err = 0
t = 1.000000, y = 1.562500, err = 1.45722e-07
t = 2.000000, y = 3.999999, err = 9.19479e-07
t = 3.000000, y = 10.562497, err = 2.90956e-06
t = 4.000000, y = 24.999994, err = 6.23491e-06
t = 5.000000, y = 52.562489, err = 1.08197e-05
t = 6.000000, y = 99.999983, err = 1.65946e-05
t = 7.000000, y = 175.562476, err = 2.35177e-05
t = 8.000000, y = 288.999968, err = 3.15652e-05
t = 9.000000, y = 451.562459, err = 4.07232e-05
t = 10.000000, y = 675.999949, err = 5.09833e-05
</pre>
{{omit from|Brlcad}}
|