Runge-Kutta method: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 85: Line 85:
y(9.0) = 451.56245928 Error: 4.07232E-05
y(9.0) = 451.56245928 Error: 4.07232E-05
y(10.0) = 675.99994902 Error: 5.09833E-05</pre>
y(10.0) = 675.99994902 Error: 5.09833E-05</pre>

=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang ALGOL68>
<lang ALGOL68>
Line 307: Line 308:
</pre>
</pre>


=={{header|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>
<lang csharp>
using System;
using System;
Line 739: Line 740:
y(10.0) = 675.99994902 Error = 5.098329E-05
y(10.0) = 675.99994902 Error = 5.098329E-05
</pre>
</pre>

=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
{{trans|BBC BASIC}}
Line 1,852: Line 1,854:


''Input:'' 1/2 (h/2) - Р5, 1 (y<sub>0</sub>) - Р8 and Р7, 0 (t<sub>0</sub>) - Р6.
''Input:'' 1/2 (h/2) - Р5, 1 (y<sub>0</sub>) - Р8 and Р7, 0 (t<sub>0</sub>) - Р6.



=={{header|Nim}}==
=={{header|Nim}}==
Line 2,176: Line 2,177:
}</lang>
}</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>

=={{header|Perl 6}}==
{{Works with|rakudo|2016.03}}
<lang perl6>sub runge-kutta(&yp) {
return -> \t, \y, \δt {
my $a = δt * yp( t, y );
my $b = δt * yp( t + δt/2, y + $a/2 );
my $c = δt * yp( t + δt/2, y + $b/2 );
my $d = δt * yp( t + δt, y + $c );
($a + 2*($b + $c) + $d) / 6;
}
}
constant δt = .1;
my &δy = runge-kutta { $^t * sqrt($^y) };
loop (
my ($t, $y) = (0, 1);
$t <= 10;
($t, $y) »+=« (δt, δy($t, $y, δt))
) {
printf "y(%2d) = %12f ± %e\n", $t, $y, abs($y - ($t**2 + 4)**2 / 16)
if $t %% 1;
}</lang>
{{out}}
{{out}}
<pre>y( 0) = 1.000000 ± 0.000000e+00
<pre>y( 0) = 1.000000 ± 0.000000e+00
Line 2,361: Line 2,326:
10 675.99994901671 5.09832902935159E-05
10 675.99994901671 5.09832902935159E-05
</pre>
</pre>

=={{header|PureBasic}}==
=={{header|PureBasic}}==
{{trans|BBC Basic}}
{{trans|BBC Basic}}
Line 2,569: Line 2,535:
</lang>
</lang>
[[File:runge-kutta.png]]
[[File:runge-kutta.png]]

=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2016.03}}
<lang perl6>sub runge-kutta(&yp) {
return -> \t, \y, \δt {
my $a = δt * yp( t, y );
my $b = δt * yp( t + δt/2, y + $a/2 );
my $c = δt * yp( t + δt/2, y + $b/2 );
my $d = δt * yp( t + δt, y + $c );
($a + 2*($b + $c) + $d) / 6;
}
}
constant δt = .1;
my &δy = runge-kutta { $^t * sqrt($^y) };
loop (
my ($t, $y) = (0, 1);
$t <= 10;
($t, $y) »+=« (δt, δy($t, $y, δt))
) {
printf "y(%2d) = %12f ± %e\n", $t, $y, abs($y - ($t**2 + 4)**2 / 16)
if $t %% 1;
}</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>


=={{header|REXX}}==
=={{header|REXX}}==