Euler method: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (→‎{{header|Raku}}: stray '.' removed, use '×' for clarity)
Line 2,748: Line 2,748:
for $a, * + $h ... * > $b -> $t {
for $a, * + $h ... * > $b -> $t {
@t_y[$t] = $y;
@t_y[$t] = $y;
$y += $h * f( $t, $y );
$y += $h × f( $t, $y );
}
}
return @t_y;
@t_y
}
}


Line 2,760: Line 2,760:


sub f ( $time, $temp ) {
sub f ( $time, $temp ) {
return -COOLING_RATE * ( $temp - AMBIENT_TEMP );
-COOLING_RATE × ( $temp - AMBIENT_TEMP )
}
}


Line 2,771: Line 2,771:


my $exact = AMBIENT_TEMP + (INITIAL_TEMP - AMBIENT_TEMP)
my $exact = AMBIENT_TEMP + (INITIAL_TEMP - AMBIENT_TEMP)
* (-COOLING_RATE * $t).exp;
× (-COOLING_RATE × $t).exp;


my $err = sub { @^a.map: { 100 * abs( $_ - $exact ) / $exact } }
my $err = sub { @^a.map: { 100 × ($_ - $exact).abs / $exact } }


my ( $a, $b, $c ) = map { @e[$_][$t] }, 2, 5, 10;
my ( $a, $b, $c ) = map { @e[$_][$t] }, 2, 5, 10;


say $t.fmt('%4d '), ( $exact, $a, $b, $c )».fmt(' %7.3f'),
say $t.fmt('%4d '), ( $exact, $a, $b, $c )».fmt(' %7.3f'),
$err.([$a, $b, $c])».fmt(' %7.3f%%');
$err([$a, $b, $c])».fmt(' %7.3f%%');
}</syntaxhighlight>
}</syntaxhighlight>