Runge-Kutta method: Difference between revisions

m
Added the Sidef language
m (→‎{{header|REXX}}: added/changed whitespace and comments, changed format of some comments.)
m (Added the Sidef language)
Line 1,772:
y( 9) = 451.5624593 Error =4.07231581e-5
y(10) = 675.9999490 Error =5.09832864e-5
</pre>
 
=={{header|Sidef}}==
{{trans|Perl 6}}
<lang ruby>__USE_BIGNUM__
 
func runge_kutta(yp) {
func (t, y, δt) {
var a = (δt * yp(t, y));
var b = (δt * yp(t + δt/2, y + a/2));
var c = (δt * yp(t + δt/2, y + b/2));
var d = (δt * yp(t + δt, y + c));
(a + 2*(b + c) + d) / 6;
}.copy;
}
 
define δt = 0.1;
var δy = runge_kutta(func(t, y) { t * y.sqrt });
 
var(t, y) = (0, 1);
loop {
t.is_int &&
printf("y(%2d) = %12f ± %e\n", t, y, abs(y - ((t**2 + 4)**2 / 16)));
t <= 10 || break;
y += δy(t, y, δt);
t += δt;
}</lang>
{{out}}
<pre>
y( 0) = 1.000000 ± 0.000000e+00
y( 1) = 1.562500 ± 1.457219e-07
y( 2) = 3.999999 ± 9.194792e-07
y( 3) = 10.562497 ± 2.909562e-06
y( 4) = 24.999994 ± 6.234909e-06
y( 5) = 52.562489 ± 1.081970e-05
y( 6) = 99.999983 ± 1.659460e-05
y( 7) = 175.562476 ± 2.351773e-05
y( 8) = 288.999968 ± 3.156520e-05
y( 9) = 451.562459 ± 4.072316e-05
y(10) = 675.999949 ± 5.098329e-05
</pre>
 
2,747

edits