Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

solving an exponential equation in Raku

exponential equation

I'm trying to solve this exponential equation like this:

my ($l,$r);

for (1 .. 100) -> $x {
    $l = $x * e ** $x;
    $r = 5 * (e ** $x - 1);
    say $x if $l == $r;
    }

But it doesn't work. How to solve it in a straightforward and comprehensive fashion?

like image 360
Lars Malmsteen Avatar asked Apr 05 '20 21:04

Lars Malmsteen


1 Answers

For Perl 5 there is Math::GSL::Roots - Find roots of arbitrary 1-D functions

https://metacpan.org/pod/Math::GSL::Roots

Raku has support for using Perl 5 code or can access the GSL C library directly, can't it?

$fspec = sub {
       my ( $x ) = shift;

       # here the function has to be inserted in the format 
       # return leftside - rightside;

       return  ($x + $x**2) - 4;


     };

gsl_root_fsolver_alloc($T); # where T is the solver algorithm, see link for the 6 type constants, e.g. $$gsl_root_fsolver_brent
gsl_root_fsolver_set( $s, $fspec, $x_lower, $x_upper ); # [$x_lower; $x_upper] is search interval
gsl_root_fsolver_iterate($s);
gsl_root_fsolver_iterate($s);
gsl_root_fsolver_iterate($s);
gsl_root_fsolver_iterate($s);
gsl_root_fsolver_iterate($s);
my $result = gsl_root_fsolver_root($s);
gsl_root_fsolver_free (s);

There are enhanced algorithms available (gsl_root_fdfsolver_*), if the derivative of a function is available.

See also https://www.gnu.org/software/gsl/doc/html/roots.html#examples for general usage

like image 66
Sebastian Avatar answered Dec 16 '22 20:12

Sebastian