Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Perl integral equation
#!/usr/bin/env perl
use strict;
use warnings;
use PerlGSL ':all';
# General concept:
# For a given d solve for c
# _
# / 1
# | f(x,c) dx = d
# _/ 0
#
# in this case use f as a gaussian
sub f {
my ($x, $c) = @_;
return exp( - ($x/$c)**2 )
}
# pick some value of d
my $d = 0.5;
my $found_c = findroot_1d(
sub {
my $c = shift;
my $integrand = sub {
my $x = shift;
return f( $x, $c );
};
return int_1d( $integrand, 0, 1) - $d;
},
# range over which to search for c
1e-12, 5
);
print "Found c = $found_c\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment