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