Math::GSL::Roots(3pm) User Contributed Perl Documentation Math::GSL::Roots(3pm)

Math::GSL::Roots - Find roots of arbitrary 1-D functions

    use Math::GSL::Roots qw/:all/;

  • a coderef, e.g.

      $fspec = sub { ... };
        

    or

      sub f { ... };
      $fspec = \&f;
        
  • an arrayref with elements [ $coderef, $params ]

The coderef is called as

      &$coderef( $x, $params );

and should return the function evaluated at "$x, $params". For example, to find the root of a quadratic with run-time specified coefficients "3, 2, 22",

  $f = sub {
             my ( $x, $params ) = @_;
             return $params->[0] + $x * $params->[1] + $x**2 * $params->[2];
           };
  $fspec = [ $f, [ 3, 2, 22 ];
  gsl_root_fsolver_set( $s, $fspec, $x_lower, $x_upper );

If there are no extra parameters, set $fspec to the function to be evaluated:

  $fspec = sub {
             my ( $x ) = shift;
             return  $x + $x**2;
           };
  gsl_root_fsolver_set( $s, $fspec, $x_lower, $x_upper );

Don't apply "gsl_root_fsolver_set" twice to the same fsolver. It will cause a memory leak. Instead of this you should create new fsolver.

  • A hashref with elements "f", "df", "fdf".
  • An arrayref with elements "[ $hashref, $params ]"

    where $hashref has elements "f", "df", "fdf";

The hashref elements are

  • "f"

    A coderef returning the value of the function at a given "x". It is called as "&$f($x, $params)".

  • "df"

    A coderef returning the value of the derivative of the function with respect to "x". It is called as "&$df($x, $params)".

  • "fdf"

    A coderef returning the value of the function and its derivative with respect to "x". It is called as "&$fdf($x, $params)".

For example, to find the root of a quadratic with run-time specified coefficients "3, 2, 22",

  $fdf = {
      f => sub {
          my ( $x, $params ) = @_;
          return $params->[0] + $x * $params->[1] + $x**2 * $params->[2];
      },
      df => sub {
          my ( $x, $params ) = @_;
          $params->[1] + 2 * $x * $params->[2];
      },
      fdf => sub {
          my ( $x, $params ) = @_;
          return
            $params->[0] + $x * $params->[1] + $x**2 * $params->[2],
            $params->[1] + 2 * $x * $params->[2];
      },
  };
  $fspec = [ $fdf, [ 3, 2, 22 ];
  gsl_root_fdsolver_set( $s, $fspec );

If there are no extra parameters, set $fspec to $fdf:

  $fdf = {
      f => sub {
          my $x = shift;
          return $x + $x**2;
      },
      df => sub {
          my $x = shift;
          1 + 2 * $x;
      },
      fdf => sub {
          my $x = shift;
          return
            $x + $x**2,
            1 + 2 * $x;
      },
  };
  gsl_root_fdfsolver_set( $s, $fdf );

Don't apply "gsl_root_fdffsolver_set" twice to the same fdfsolver. It will cause a memory leak. Instead of this you should create new fdfsolver.

This module also includes the following constants :

For more information about these functions, we refer you to the official GSL documentation: <http://www.gnu.org/software/gsl/manual/html_node/>

Jonathan "Duke" Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>

Copyright (C) 2008-2024 Jonathan "Duke" Leto and Thierry Moisan

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

2024-10-15 perl v5.40.0