Runge-Kutta method: Difference between revisions
Content deleted Content added
→{{header|Perl}}: this is not a translation of Perl 6 anymore |
|||
Line 496: | Line 496: | ||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
||
{{trans|Perl}} |
|||
⚫ | |||
gather loop { |
|||
⚫ | |||
FIRST take $y; |
|||
return -> \t, \y { |
|||
@ |
my @dy = δt * yp( t , y ); |
||
push @dy, δt * yp( t + δt/2, y + @dy[0]/2 ); |
|||
push @dy, δt * yp( t + δt/2, y + @dy[1]/2 ); |
|||
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; |
|||
printf "y(%2.0f) = %12f ± %e\n", $t, $y, abs($y - ($t**2 + 4)**2 / 16) |
|||
⚫ | |||
} |
|||
for @t Z @y -> $t, $y { |
|||
⚫ | |||
printf "y(%2d) = %12f ± %e\n", $t, $y, abs( $y - ($t**2 + 4)**2 / 16) |
|||
⚫ | |||
⚫ | |||
{{out}} |
{{out}} |
||
<pre>y( 0) = 1.000000 ± 0.000000e+00 |
<pre>y( 0) = 1.000000 ± 0.000000e+00 |