Skip to content

Instantly share code, notes, and snippets.

@jberger
Created June 2, 2012 14:48
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 jberger/2858700 to your computer and use it in GitHub Desktop.
Save jberger/2858700 to your computer and use it in GitHub Desktop.
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