Created
April 13, 2019 01:34
-
-
Save tomcha/45cec33495815560e76552aac191c102 to your computer and use it in GitHub Desktop.
台形法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use feature 'say'; | |
use DDP { deparse => 1 }; | |
print "a b(区間)を入力してください(スペース区切り) >"; | |
chomp(my $input = <STDIN>); | |
my ($a, $b) = split(/ /, $input); | |
my $n = 10000; | |
my $scale = ($b - $a) / $n; | |
my $result = 0; | |
for my $i (0..($n - 1)){ | |
my $a_val = $a + $i * $scale; | |
my $b_val = $a_val + $scale; | |
$result += (function_x($a_val) + function_x($b_val)) * $scale / 2 ; | |
say "re:$result"; | |
} | |
say $result; | |
sub function_x{ | |
my $x =shift; | |
return $x ** 2 + 5 * $x + 6; | |
} | |
__END__ | |
f(x) = x^2 + 5x + 6 | |
a-b : 入力値による |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment