Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created April 13, 2019 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomcha/45cec33495815560e76552aac191c102 to your computer and use it in GitHub Desktop.
Save tomcha/45cec33495815560e76552aac191c102 to your computer and use it in GitHub Desktop.
台形法
#!/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