Runge-Kutta method: Difference between revisions
Content deleted Content added
→{{header|Perl 6}}: golfing a bit |
|||
Line 500: | Line 500: | ||
<lang perl6>sub runge-kutta(&yp, \δt) { |
<lang perl6>sub runge-kutta(&yp, \δt) { |
||
return -> \t, \y { |
return -> \t, \y { |
||
t + δt, y + 6 R/ [+] <1 2 2 1> Z* |
|||
map { |
|||
push @dy, δt * yp( t + δt/2, y + @dy[0]/2 ); |
|||
state $yp = 0; |
|||
push @dy, δt * yp( t + δt/2, y + @dy[1]/2 ); |
|||
δt* ($yp = yp(t + $_*δt, y + δt*$_*$yp)) |
|||
}, <0 1/2 1/2 1>; |
|||
} |
} |
||
} |
} |
||
my &RK = runge-kutta { $^t * sqrt($^y) }, .1; |
my &RK = runge-kutta { $^t * sqrt($^y) }, .1; |
||
loop ( my ($t, $y) = (0, 1); $t <= 10; ($t, $y) = RK($t, $y)) { |
loop ( my ($t, $y) = (0, 1); $t <= 10; ($t, $y) = RK($t, $y)) { |
||
printf "y(%2.0f) = |
printf "y(%2.0f) = %12f ± %e\n", $t, $y, abs($y - ($t**2 + 4)**2 / 16) |
||
if $t == $t.Int; |
if $t == $t.Int; |
||
⚫ | |||
} |
|||
⚫ | |||
{{out}} |
{{out}} |
||
<pre>y( 0) = 1.000000 ± 0.000000e+00 |
<pre>y( 0) = 1.000000 ± 0.000000e+00 |