Runge-Kutta method: Difference between revisions

Content deleted Content added
Grondilu (talk | contribs)
→‎{{header|Perl}}: this is not a translation of Perl 6 anymore
Grondilu (talk | contribs)
Line 496: Line 496:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{trans|Perl}}
<lang perl6>sub runge-kutta (&yp, $t is copy, $y is copy, \δt) {

gather loop {
<lang perl6>sub runge-kutta(&yp, \δt) {
FIRST take $y;
my @δy = δt * yp $t, $y;
return -> \t, \y {
@δy.push: δt * yp $t + δt / 2, $y + @δy[0] / 2;
my @dy = δt * yp( t , y );
@δy.push: δt * yp $t + δt / 2, $y + @δy[1] / 2;
push @dy, δt * yp( t + δt/2, y + @dy[0]/2 );
@δy.push: δt * yp $t += δt, $y + @δy[2];
push @dy, δt * yp( t + δt/2, y + @dy[1]/2 );
take $y += 6 R/ [+] <1 2 2 1> Z* @δy ;
push @dy, δt * yp( t + δt , y + @dy[2] );
t + δt, y + 6 R/ [+] <1 2 2 1> Z* @dy;
}
}
}
}
my &RK = runge-kutta { $^t * sqrt($^y) }, .1;
constant δt = 0.1;
constant n = 10.0;
loop ( my ($t, $y) = (0, 1); $t <= 10; ($t, $y) = RK($t, $y)) {
constant @t = 0, δt ... n;
my @y := runge-kutta -> $t, $y { $t * sqrt $y }, @t[0], 1, δt;
printf "y(%2.0f) = %12f ± %e\n", $t, $y, abs($y - ($t**2 + 4)**2 / 16)
if $t == $t.Int;

}
for @t Z @y -> $t, $y {
</lang>
printf "y(%2d) = %12f ± %e\n", $t, $y, abs( $y - ($t**2 + 4)**2 / 16)
if $t == $t.floor;
}</lang>
{{out}}
{{out}}
<pre>y( 0) = 1.000000 ± 0.000000e+00
<pre>y( 0) = 1.000000 ± 0.000000e+00