Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
台形法
#!/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
You can’t perform that action at this time.