Runge-Kutta method: Difference between revisions
Content deleted Content added
m →{{header|J}}: typo |
→{{header|Perl 6}}: add entry |
||
Line 255: | Line 255: | ||
y[t] -> 1/16*(4+t^2)^2 /. t -> Range[10] |
y[t] -> 1/16*(4+t^2)^2 /. t -> Range[10] |
||
->y[{1,2,3,4,5,6,7,8,9,10}]->{25/16, 4, 169/16, 25, 841/16, 100, 2809/16, 289, 7225/16, 676}</lang> |
->y[{1,2,3,4,5,6,7,8,9,10}]->{25/16, 4, 169/16, 25, 841/16, 100, 2809/16, 289, 7225/16, 676}</lang> |
||
=={{header|Perl 6}}== |
|||
{{works with|niecza|2012-04-3}} |
|||
<lang perl6>sub runge-kutta (&yp, @t, $y is copy, \𝛿t) { |
|||
$y, gather for @t -> \t { |
|||
my \𝛿y1 = 𝛿t * yp t, $y; |
|||
my \𝛿y2 = 𝛿t * yp t + 𝛿t / 2, $y + 𝛿y1 / 2; |
|||
my \𝛿y3 = 𝛿t * yp t + 𝛿t / 2, $y + 𝛿y2 / 2; |
|||
my \𝛿y4 = 𝛿t * yp t + 𝛿t, $y + 𝛿y3; |
|||
take $y = $y + (𝛿y1 + 2 * (𝛿y2 + 𝛿y3) + 𝛿y4) / 6; |
|||
} |
|||
} |
|||
sub yprime ($t, $y) { $t * sqrt $y } |
|||
constant 𝛿t = 0.10; |
|||
constant n = 10.0; |
|||
constant @t = 0, 𝛿t ... n; |
|||
my @y = runge-kutta(&yprime, @t, 1, 𝛿t); |
|||
for @t Z @y -> $t, $y { |
|||
say "y($t) = $y".fmt("%-30s"), "±", abs($y - (($t ** 2 + 4) ** 2 / 16)) |
|||
if $t == $t.floor; |
|||
}</lang> |
|||
{{out}} |
|||
<pre>y(0) = 1 ±0 |
|||
y(1) = 1.5624998542781079 ±1.4572189210859676E-07 |
|||
y(2) = 3.9999990805207992 ±9.1947920077828371E-07 |
|||
y(3) = 10.562497090437551 ±2.9095624487496252E-06 |
|||
y(4) = 24.999993765090636 ±6.2349093639113562E-06 |
|||
y(5) = 52.562489180302585 ±1.0819697415342944E-05 |
|||
y(6) = 99.999983405403583 ±1.6594596417007779E-05 |
|||
y(7) = 175.56247648227125 ±2.3517728749311573E-05 |
|||
y(8) = 288.99996843479857 ±3.156520142510999E-05 |
|||
y(9) = 451.56245927683966 ±4.07231603389846E-05 |
|||
y(10) = 675.99994901670971 ±5.0983290293515893E-05</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |