Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
37
Task/Euler-method/Raku/euler-method.raku
Normal file
37
Task/Euler-method/Raku/euler-method.raku
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
sub euler ( &f, $y0, $a, $b, $h ) {
|
||||
my $y = $y0;
|
||||
my @t_y;
|
||||
for $a, * + $h ... * > $b -> $t {
|
||||
@t_y[$t] = $y;
|
||||
$y += $h × f( $t, $y );
|
||||
}
|
||||
@t_y
|
||||
}
|
||||
|
||||
constant COOLING_RATE = 0.07;
|
||||
constant AMBIENT_TEMP = 20;
|
||||
constant INITIAL_TEMP = 100;
|
||||
constant INITIAL_TIME = 0;
|
||||
constant FINAL_TIME = 100;
|
||||
|
||||
sub f ( $time, $temp ) {
|
||||
-COOLING_RATE × ( $temp - AMBIENT_TEMP )
|
||||
}
|
||||
|
||||
my @e;
|
||||
@e[$_] = euler( &f, INITIAL_TEMP, INITIAL_TIME, FINAL_TIME, $_ ) for 2, 5, 10;
|
||||
|
||||
say 'Time Analytic Step2 Step5 Step10 Err2 Err5 Err10';
|
||||
|
||||
for INITIAL_TIME, * + 10 ... * >= FINAL_TIME -> $t {
|
||||
|
||||
my $exact = AMBIENT_TEMP + (INITIAL_TEMP - AMBIENT_TEMP)
|
||||
× (-COOLING_RATE × $t).exp;
|
||||
|
||||
my $err = sub { @^a.map: { 100 × ($_ - $exact).abs / $exact } }
|
||||
|
||||
my ( $a, $b, $c ) = map { @e[$_][$t] }, 2, 5, 10;
|
||||
|
||||
say $t.fmt('%4d '), ( $exact, $a, $b, $c )».fmt(' %7.3f'),
|
||||
$err([$a, $b, $c])».fmt(' %7.3f%%');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue