Skip to content
Browse files

Fix the coding standar

Pointing the constructor to Math_Numerical_RootFinding_Common::Math_Numerical_RootFinding_Common
Rename file names to the camel-case style


git-svn-id: http://svn.php.net/repository/pear/packages/Math_Numerical_RootFinding/trunk@206147 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent a3c80f1 commit f8ef687701c984295156a1e2acb363f4e56e897a Firman Wandayandi committed Jan 30, 2006
View
229 Math/Numerical/RootFinding/FalsePosition.php
@@ -0,0 +1,229 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
+
+// {{{ Header
+
+/**
+ * Driver file contains Math_Numerical_RootFinding_Bisection class to provide
+ * False Position/Regula Falsi method root finding calculation.
+ *
+ * PHP versions 4 and 5
+ *
+ * LICENSE:
+ *
+ * BSD License
+ *
+ * Copyright (c) 2004-2006 Firman Wandayandi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * 3. Neither the name of Firman Wandayandi nor the names of
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Math
+ * @package Math_Numerical_RootFinding
+ * @subpackage Methods
+ * @author Firman Wandayandi <firman@php.net>
+ * @copyright Copyright (c) 2004-2006 Firman Wandayandi
+ * @license http://www.opensource.org/licenses/bsd-license.php
+ * BSD License
+ * @version CVS: $Id$
+ */
+
+// }}}
+// {{{ Dependencies
+
+/**
+ * Load Math_Numerical_RootFinding_Common as base class.
+ */
+require_once 'Math/Numerical/RootFinding/Common.php';
+
+// }}}
+// {{{ Class: Math_Numerical_RootFinding_FalsePosition
+
+/**
+ * False Position/Regula Falsi method class.
+ *
+ * @category Math
+ * @package Math_Numerical_RootFinding
+ * @subpackage Methods
+ * @author Firman Wandayandi <firman@php.net>
+ * @copyright Copyright (c) 2004-2006 Firman Wandayandi
+ * @license http://www.opensource.org/licenses/bsd-license.php
+ * BSD License
+ * @version Release: @package_version@
+ */
+class Math_Numerical_RootFinding_FalsePosition
+extends Math_Numerical_RootFinding_Common
+{
+ // {{{ Constructor
+
+ /**
+ * Constructor.
+ *
+ * @param array $options (optional) Options.
+ *
+ * @access public
+ * @see Math_Numerical_RootFinding_Common::Math_Numerical_RootFinding_Common()
+ */
+ function Math_Numerical_RootFinding_FalsePosition($options = null)
+ {
+ parent::Math_Numerical_RootFinding_Common($options);
+ }
+
+ // }}}
+ // {{{ infoCompute()
+
+ /**
+ * Print out parameters description for compute() function.
+ *
+ * @access public
+ */
+ function infoCompute()
+ {
+ print "<h2>False Position::compute()</h2>\n" .
+
+ "<em>float</em> | <em>PEAR_Error</em> ".
+ "<strong>compute</strong>(<u>\$fxFunction</u>, <u>\$xL</u>, ".
+ "<u>\$xU</u>)<br />\n" .
+
+ "<h3>Description</h3>\n".
+
+ "<em>callback</em> <u>\$fxFunction</u> Callback f(x) equation ".
+ "function or object/method tuple.<br>\n" .
+
+ "<em>float</em> <u>\$xL</u> Lower guess.<br>\n" .
+
+ "<em>float</em> <u>\$xU</u> Upper guess.<br>\n";
+ }
+
+ // }}}
+ // {{{ compute()
+
+ /**
+ * False Position/Regula Falsi method.
+ *
+ * @param callback $fxFunction Callback f(x) equation function or
+ * object/method tuple.
+ * @param float $xL Lower guess.
+ * @param float $xU Upper guess.
+ *
+ * @return float|PEAR_Error root value on success or PEAR_Error on failure.
+ * @access public
+ * @see Math_Numerical_RootFinding_Common::validateEqFunction()
+ * @see Math_Numerical_RootFinding_Common::getEqResult()
+ * @see Math_Numerical_RootFinding_Bisection::compute()
+ */
+ function compute($fxFunction, $xL, $xU)
+ {
+ // Validate f(x) equation function.
+ $err = Math_Numerical_RootFinding_Common::validateEqFunction($fxFunction);
+ if (PEAR::isError($err)) {
+ return $err;
+ }
+
+ // Sets maximum iteration and tolerance from options.
+ $maxIteration = $this->options['max_iteration'];
+ $errTolerance = $this->options['err_tolerance'];
+
+ // Calculate first approximation $xR (False Position's formula).
+ $fxL = Math_Numerical_RootFinding_Common::getEqResult($fxFunction, $xL);
+ $fxU = Math_Numerical_RootFinding_Common::getEqResult($fxFunction, $xU);
+ if ($fxL - $fxU == 0) {
+ return PEAR::raiseError('Iteration skipped, division by zero');
+ }
+ $xR = $xU - (($fxU * ($xL - $xU)) / ($fxL - $fxU));
+
+ // Sets variable for saving errors during iteration, for divergent
+ // detection.
+ $epsErrors = array();
+
+ for ($i = 0; $i < $maxIteration; $i++) {
+ // Calculate f(xr), where: xr = $xR
+ $fxR = Math_Numerical_RootFinding_Common::getEqResult($fxFunction, $xR);
+
+ if ($fxL * $fxR < 0) { // Root is at first subinterval.
+ $xU = $xR;
+ } elseif ($fxL * $fxR > 0) { // Root is at second subinterval.
+ $xL = $xR;
+ } elseif ($fxL * $fxR == 0) { // $xR is the exact root.
+ $this->root = $xR;
+ break;
+ }
+
+ // Avoid division by zero.
+ if ($fxL - $fxU == 0) {
+ return PEAR::raiseError('Iteration skipped, division by zero');
+ }
+
+ // Compute new approximation.
+ $xN = $xU - (($fxU * ($xL - $xU)) / ($fxL - $fxU));
+
+ // Compute approximation error.
+ $this->epsError = abs(($xN - $xR) / $xN);
+ $epsErrors[] = $this->epsError;
+
+ // Detect for divergent rows.
+ if ($this->isDivergentRows($epsErrors) &&
+ $this->options['divergent_skip'])
+ {
+ return PEAR::raiseError('Iteration skipped, divergent rows detected');
+ break;
+ }
+
+ // Check for error tolerance, if lower than or equal with
+ // $errTolerance it is the root.
+ if ($this->epsError <= $errTolerance) {
+ $this->root = $xN;
+ break;
+ }
+
+ // Calculate f(xl), where xl = $xL.
+ $fxL = Math_Numerical_RootFinding_Common::getEqResult($fxFunction, $xL);
+ // Calculate f(xu), where xu = $xU.
+ $fxU = Math_Numerical_RootFinding_Common::getEqResult($fxFunction, $xU);
+
+ // Switch xn -> xr, where xn = $xN and xr = $xR.
+ $xR = $xN;
+ }
+ $this->iterationCount = $i;
+ return $this->root;
+ }
+
+ // }}}
+}
+
+// }}}
+
+/*
+ * Local variables:
+ * mode: php
+ * tab-width: 4
+ * c-basic-offset: 4
+ * c-hanging-comment-ender-p: nil
+ * End:
+ */
+?>
View
204 Math/Numerical/RootFinding/FixedPoint.php
@@ -0,0 +1,204 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
+
+// {{{ Header
+
+/**
+ * Driver file contains Math_Numerical_RootFinding_Bisection class to provide
+ * Fixed Point method root finding calculation.
+ *
+ * PHP versions 4 and 5
+ *
+ * LICENSE:
+ *
+ * BSD License
+ *
+ * Copyright (c) 2004-2006 Firman Wandayandi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * 3. Neither the name of Firman Wandayandi nor the names of
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Math
+ * @package Math_Numerical_RootFinding
+ * @subpackage Methods
+ * @author Firman Wandayandi <firman@php.net>
+ * @copyright Copyright (c) 2004-2006 Firman Wandayandi
+ * @license http://www.opensource.org/licenses/bsd-license.php
+ * BSD License
+ * @version CVS: $Id$
+ */
+
+// }}}
+// {{{ Dependencies
+
+/**
+ * Load Math_Numerical_RootFinding_Common as base class.
+ */
+require_once 'Math/Numerical/RootFinding/Common.php';
+
+// }}}
+// {{{ Class: Math_Numerical_RootFinding_FixedPoint
+
+/**
+ * Fixed Point method class.
+ *
+ * @category Math
+ * @package Math_Numerical_RootFinding
+ * @subpackage Methods
+ * @author Firman Wandayandi <firman@php.net>
+ * @copyright Copyright (c) 2004-2006 Firman Wandayandi
+ * @license http://www.opensource.org/licenses/bsd-license.php
+ * BSD License
+ * @version Release: @package_version@
+ */
+class Math_Numerical_RootFinding_FixedPoint
+extends Math_Numerical_RootFinding_Common
+{
+ // {{{ Constructor
+
+ /**
+ * Constructor.
+ *
+ * @param array $options (optional) Options.
+ *
+ * @access public
+ * @see Math_Numerical_RootFinding_Common::Math_Numerical_RootFinding_Common()
+ */
+ function Math_Numerical_RootFinding_FixedPoint($options = null)
+ {
+ parent::Math_Numerical_RootFinding_Common($options);
+ }
+
+ // }}}
+ // {{{ infoCompute()
+
+ /**
+ * Print out parameters description for compute() function.
+ *
+ * @access public
+ */
+ function infoCompute()
+ {
+ print "<h2>Fixed Point::compute()</h2>\n" .
+
+ "<em>float</em> | <em>PEAR_Error</em> ".
+ "<strong>compute</strong>(<u>\$gxFunction</u>, <u>\$xR</u>)<br />\n" .
+
+ "<h3>Description</h3>\n" .
+
+ "<em>callback</em> <u>\$gxFunction</u> Callback g(x) " .
+ "equation (the modification of f(x), which g(x) = x) " .
+ "function or object/method tuple.<br>\n" .
+
+ "<em>float</em> <u>\$xR</u> Initial guess.<br>\n";
+ }
+
+ // }}}
+ // {{{ compute()
+
+ /**
+ * Fixed Point method.
+ *
+ * This method using g(x) (the modification of f(x), which g(x) = x).
+ *
+ * @param callback $gxFunction Callback g(x) equation function or
+ * object/method tuple.
+ * @param float $xR Initial guess.
+ *
+ * @return float|PEAR_Error root value on success or PEAR_Error on failure.
+ * @access public
+ * @see Math_Numerical_RootFinding_Common::validateEqFunction()
+ * @see Math_Numerical_RootFinding_Common::getEqResult()
+ * @see Math_Numerical_RootFinding_Common::isDivergentRow()
+ */
+ function compute($gxFunction, $xR)
+ {
+ // Validate g(x) equation function.
+ $err = Math_Numerical_RootFinding_Common::validateEqFunction($gxFunction);
+ if (PEAR::isError($err)) {
+ return $err;
+ }
+
+ // Sets maximum iteration and tolerance from options.
+ $maxIteration = $this->options['max_iteration'];
+ $errTolerance = $this->options['err_tolerance'];
+
+ // Sets variable for saving errors during iteration, for divergent
+ // detection.
+ $epsErrors = array();
+
+ for($i = 0; $i < $maxIteration; $i++) {
+ // Calculate g(x[i]), where x[i] = $xR (Fixed Point's formula).
+ $xN = Math_Numerical_RootFinding_Common::getEqResult($gxFunction, $xR);
+
+ // xR is the root.
+ if ($xN == 0) {
+ $this->root = $xR;
+ break;
+ }
+
+ // Compute error.
+ $this->epsError = abs(($xN - $xR) / $xN);
+ $epsErrors[] = $this->epsError;
+
+ // Detect for divergent rows.
+ if ($this->isDivergentRows($epsErrors) &&
+ $this->options['divergent_skip'])
+ {
+ return PEAR::raiseError('Iteration skipped, divergent rows detected');
+ break;
+ }
+
+ // Check for error tolerance, if lower than or equal with
+ // $errTolerance it is the root.
+ if ($this->epsError <= $errTolerance) {
+ $this->root = $xR;
+ break;
+ }
+
+ // Switch x[i+1] -> x[i], where: x[i] = $xR and x[i+1] = $xN.
+ $xR = $xN;
+ }
+ $this->iterationCount = $i;
+ return $this->root;
+ }
+
+ // }}}
+}
+
+// }}}
+
+/*
+ * Local variables:
+ * mode: php
+ * tab-width: 4
+ * c-basic-offset: 4
+ * c-hanging-comment-ender-p: nil
+ * End:
+ */
+?>
View
226 Math/Numerical/RootFinding/NewtonRaphson.php
@@ -0,0 +1,226 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
+
+// {{{ Header
+
+/**
+ * Driver file contains Math_Numerical_RootFinding_Bisection class to provide
+ * Newton-Raphson method root finding calculation.
+ *
+ * PHP versions 4 and 5
+ *
+ * LICENSE:
+ *
+ * BSD License
+ *
+ * Copyright (c) 2004-2006 Firman Wandayandi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * 3. Neither the name of Firman Wandayandi nor the names of
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Math
+ * @package Math_Numerical_RootFinding
+ * @subpackage Methods
+ * @author Firman Wandayandi <firman@php.net>
+ * @copyright Copyright (c) 2004-2006 Firman Wandayandi
+ * @license http://www.opensource.org/licenses/bsd-license.php
+ * BSD License
+ * @version CVS: $Id$
+ */
+
+// }}}
+// {{{ Dependencies
+
+/**
+ * Load Math_Numerical_RootFinding_Common as base class.
+ */
+require_once 'Math/Numerical/RootFinding/Common.php';
+
+// }}}
+// {{{ Class: Math_Numerical_RootFinding_NewtonRaphson
+
+/**
+ * Newton-Raphson method class.
+ *
+ * @category Math
+ * @package Math_Numerical_RootFinding
+ * @subpackage Methods
+ * @author Firman Wandayandi <firman@php.net>
+ * @copyright Copyright (c) 2004-2006 Firman Wandayandi
+ * @license http://www.opensource.org/licenses/bsd-license.php
+ * BSD License
+ * @version Release: @package_version@
+ */
+class Math_Numerical_RootFinding_NewtonRaphson
+extends Math_Numerical_RootFinding_Common
+{
+ // {{{ Constructor
+
+ /**
+ * Constructor.
+ *
+ * @param array $options (optional) Options.
+ *
+ * @access public
+ * @see Math_Numerical_RootFinding_Common::Math_Numerical_RootFinding_Common()
+ */
+ function Math_Numerical_RootFinding_NewtonRaphson($options = null)
+ {
+ parent::Math_Numerical_RootFinding_Common($options);
+ }
+
+ // }}}
+ // {{{ infoCompute()
+
+ /**
+ * Print out parameters description for compute() function.
+ *
+ * @access public
+ */
+ function infoCompute()
+ {
+ print "<h2>False Position::compute()</h2>\n" .
+
+ "<em>float</em> | <em>PEAR_Error</em> " .
+ "<strong>compute</strong>(<u>\$fxFunction</u>, " .
+
+ "<u>\$dfxFunction</u>, <u>\$xR</u>)<br />\n" .
+
+ "<h3>Description</h3>\n" .
+
+ "<em>callback</em> <u>\$fxFunction</u> Callback f(x) equation " .
+ "function or object/method tuple.<br>\n" .
+
+ "<em>callback</em> <u>\$dfxFunction</u> Callback f'(x) equation " .
+ "function or object/method tuple.<br>\n" .
+
+ "<em>float</em> <u>\$xR</u> Initial guess.<br>\n";
+ }
+
+ // }}}
+ // {{{ compute()
+
+ /**
+ * Netwon-Raphson method.
+ *
+ * @param callback $fxFunction Callback f(x) equation function or
+ * object/method tuple.
+ * @param callback $dfxFunction Callback f'(x) equation function or
+ * object/method tuple.
+ * @param float $xR Initial guess.
+ *
+ * @return float|PEAR_Error Root value on success or PEAR_Error on
+ * failure.
+ * @access public
+ * @see Math_Numerical_RootFinding_Common::validateEqFunction()
+ * @see Math_Numerical_RootFinding_Common::getEqResult()
+ * @see Math_Numerical_RootFinding_Common::isDivergentRow()
+ * @see Math_Numerical_RootFinding_Newtonraphson2::compute()
+ */
+ function compute($fxFunction, $dfxFunction, $xR) {
+ // evaluate f(x) equation function before begin anything
+ $err = Math_Numerical_RootFinding_Common::validateEqFunction($fxFunction);
+ if (PEAR::isError($err)) {
+ return $err;
+ }
+
+ // evaluate df(x) equation function before begin anything
+ $err = Math_Numerical_RootFinding_Common::validateEqFunction($dfxFunction);
+ if (PEAR::isError($err)) {
+ return $err;
+ }
+
+ // Sets maximum iteration and tolerance from options.
+ $maxIteration = $this->options['max_iteration'];
+ $errTolerance = $this->options['err_tolerance'];
+
+ // Sets variable for saving errors during iteration, for divergent
+ // detection.
+ $epsErrors = array();
+
+ for ($i = 1; $i < $maxIteration; $i++) {
+ // Calculate f(x[i]), where: x[i] = $xR.
+ $fxR = Math_Numerical_RootFinding_Common::getEqResult($fxFunction, $xR);
+
+ // Calculate f'(x[i]), where: x[i] = $xR.
+ $dxR = Math_Numerical_RootFinding_Common::getEqResult($dfxFunction, $xR);
+
+ // Avoid division by zero.
+ if ($dxR == 0) {
+ return PEAR::raiseError('Iteration skipped, division by zero');
+ }
+
+ // Newton-Raphson's formula.
+ $xN = $xR - ($fxR / $dxR);
+
+ // xR is the root.
+ if ($xN == 0) {
+ $this->root = $xR;
+ break;
+ }
+
+ // Compute error.
+ $this->epsError = abs(($xN - $xR) / $xN);
+ $epsErrors[] = $this->epsError;
+
+ // Detect for divergent rows.
+ if ($this->isDivergentRows($epsErrors) &&
+ $this->options['divergent_skip'])
+ {
+ return PEAR::raiseError('Iteration skipped, divergent rows detected');
+ break;
+ }
+
+ // Check for error tolerance, if lower than or equal with
+ // $errTolerance it is the root.
+ if ($this->epsError <= $errTolerance) {
+ $this->root = $xR;
+ break;
+ }
+
+ // Switch x[i+1] -> x[i], where: x[i] = $xR and x[i+1] = $xN.
+ $xR = $xN;
+ }
+ $this->iterationCount = $i;
+ return $this->root;
+ }
+
+ // }}}
+}
+
+// }}}
+
+/*
+ * Local variables:
+ * mode: php
+ * tab-width: 4
+ * c-basic-offset: 4
+ * c-hanging-comment-ender-p: nil
+ * End:
+ */
+?>
View
239 Math/Numerical/RootFinding/NewtonRaphson2.php
@@ -0,0 +1,239 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
+
+// {{{ Header
+
+/**
+ * Driver file contains Math_Numerical_RootFinding_Bisection class to provide
+ * Newton-Raphson 2 method root finding calculation.
+ *
+ * PHP versions 4 and 5
+ *
+ * LICENSE:
+ *
+ * BSD License
+ *
+ * Copyright (c) 2004-2006 Firman Wandayandi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * 3. Neither the name of Firman Wandayandi nor the names of
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Math
+ * @package Math_Numerical_RootFinding
+ * @subpackage Methods
+ * @author Firman Wandayandi <firman@php.net>
+ * @copyright Copyright (c) 2004-2006 Firman Wandayandi
+ * @license http://www.opensource.org/licenses/bsd-license.php
+ * BSD License
+ * @version CVS: $Id$
+ */
+
+// }}}
+// {{{ Dependencies
+
+/**
+ * Load Math_Numerical_RootFinding_Common as base class.
+ */
+require_once 'Math/Numerical/RootFinding/Common.php';
+
+// }}}
+// {{{ Class: Math_Numerical_RootFinding_NewtonRaphson2
+
+/**
+ * Newton-Raphson 2 method class.
+ *
+ * @category Math
+ * @package Math_Numerical_RootFinding
+ * @subpackage Methods
+ * @author Firman Wandayandi <firman@php.net>
+ * @copyright Copyright (c) 2004-2006 Firman Wandayandi
+ * @license http://www.opensource.org/licenses/bsd-license.php
+ * BSD License
+ * @version Release: @package_version@
+ */
+class Math_Numerical_RootFinding_NewtonRaphson2
+extends Math_Numerical_RootFinding_Common
+{
+ // {{{ Constructor
+
+ /**
+ * Constructor.
+ *
+ * @param array $options (optional) Options.
+ *
+ * @access public
+ * @see Math_Numerical_RootFinding_Common::Math_Numerical_RootFinding_Common()
+ */
+ function Math_Numerical_RootFinding_NewtonRaphson2($options = null)
+ {
+ parent::Math_Numerical_RootFinding_Common($options);
+ }
+
+ // }}}
+ // {{{ infoCompute()
+
+ /**
+ * Print out parameters description for compute() function.
+ *
+ * @access public
+ */
+ function infoCompute()
+ {
+ print "<h2>False Position::compute()</h2>\n" .
+
+ "<em>float</em> | <em>PEAR_Error</em> " .
+ "<strong>compute</strong>(<u>\$fxFunction</u>, " .
+ "<u>\$dfx1Function</u>, <u>\$dfx2Function</u>, " .
+ "<u>\$xR</u>)<br />\n" .
+
+ "<h3>Description</h3>\n" .
+ "<em>callback</em> <u>\$fxFunction</u> Callback f(x) equation " .
+ "function or object/method tuple.<br>\n" .
+
+ "<em>callback</em> <u>\$dfx1Function</u> Callback f'(x) " .
+ "equation function or object/method tuple.<br>\n" .
+
+ "<em>callback</em> <u>\$dfx2Function</u> Callback f''(x) " .
+ "equation function or object/method tuple.<br>\n" .
+
+ "<em>float</em> <u>\$xR</u> Initial guess.<br>\n";
+ }
+
+ // }}}
+ // {{{ compute()
+
+ /**
+ * Newton-Raphson method for calculate double root (twin root).
+ *
+ * @param callback $fxFunction Callback f(x) equation function or
+ * object/method tuple.
+ * @param callback $dfx1Function Callback f'(x) equation function or
+ * object/method tuple.
+ * @param callback $dfx2Function Callback f''(x) equation function or
+ * object/method tuple.
+ * @param float $xR Initial guess.
+ *
+ * @return float|PEAR_Error Root value on success or PEAR_Error on failure.
+ * @access public
+ * @see Math_Numerical_RootFinding_Common::validateEqFunction()
+ * @see Math_Numerical_RootFinding_Common::getEqResult()
+ * @see Math_Numerical_RootFinding_Common::isDivergentRow()
+ * @see Math_Numerical_RootFinding_Newtonraphson::compute()
+ */
+ function compute($fxFunction, $dfx1Function, $dfx2Function, $xR)
+ {
+ // Validate f(x) equation function.
+ $err = Math_Numerical_RootFinding_Common::validateEqFunction($fxFunction);
+ if (PEAR::isError($err)) {
+ return $err;
+ }
+
+ // Validate f'(x) equation function.
+ $err = Math_Numerical_RootFinding_Common::validateEqFunction($dfx1Function);
+ if (PEAR::isError($err)) {
+ return $err;
+ }
+
+ // Validate f''(x) equation function.
+ $err = Math_Numerical_RootFinding_Common::validateEqFunction($dfx2Function);
+ if (PEAR::isError($err)) {
+ return $err;
+ }
+
+ // Sets maximum iteration and tolerance from options.
+ $maxIteration = $this->options['max_iteration'];
+ $errTolerance = $this->options['err_tolerance'];
+
+ // Sets variable for saving errors during iteration, for divergent
+ // detection.
+ $epsErrors = array();
+
+ for ($i = 1; $i < $maxIteration; $i++) {
+ // Calculate f(x[i]), where: x[i] = $xR.
+ $fxR = Math_Numerical_RootFinding_Common::getEqResult($fxFunction, $xR);
+
+ // Calculate f'(x[i]), where: x[i] = $xR.
+ $d1xR = Math_Numerical_RootFinding_Common::getEqResult($dfx1Function, $xR);
+
+ // Calculate f''(x[i]), where: x[i] = $xR.
+ $d2xR = Math_Numerical_RootFinding_Common::getEqResult($dfx2Function, $xR);
+
+ // Avoid division by zero.
+ if (pow($d1xR, 2) - ($fxR * $d2xR) == 0) {
+ return PEAR::raiseError('Iteration skipped, division by zero');
+ }
+
+ // Newton-Raphson's formula.
+ $xN = $xR - (($fxR * $d1xR) / (pow($d1xR, 2) - ($fxR * $d2xR)));
+
+ // xR is the root.
+ if ($xN == 0) {
+ $this->root = $xR;
+ break;
+ }
+
+ // Compute error.
+ $this->epsError = abs(($xN - $xR) / $xN);
+ $epsErrors[] = $this->epsError;
+
+ // Detect for divergent rows.
+ if ($this->isDivergentRows($epsErrors) &&
+ $this->options['divergent_skip'])
+ {
+ return PEAR::raiseError('Iteration skipped, divergent rows detected');
+ break;
+ }
+
+ // Check for error tolerance, if lower than or equal with
+ // $errTolerance it is the root.
+ if ($this->epsError <= $errTolerance) {
+ $this->root = $xR;
+ break;
+ }
+
+ // Switch x[i+1] -> x[i], where: x[i] = $xR and x[i+1] = $xN.
+ $xR = $xN;
+ }
+ $this->iterationCount = $i;
+ return $this->root;
+ }
+
+ // }}}
+}
+
+// }}}
+
+/*
+ * Local variables:
+ * mode: php
+ * tab-width: 4
+ * c-basic-offset: 4
+ * c-hanging-comment-ender-p: nil
+ * End:
+ */
+?>
View
239 Math/Numerical/RootFinding/RalstonRabinowitz.php
@@ -0,0 +1,239 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
+
+// {{{ Header
+
+/**
+ * Driver file contains Math_Numerical_RootFinding_Bisection class to provide
+ * Ralston and Rabinowitz method root finding calculation.
+ *
+ * PHP versions 4 and 5
+ *
+ * LICENSE:
+ *
+ * BSD License
+ *
+ * Copyright (c) 2004-2006 Firman Wandayandi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * 3. Neither the name of Firman Wandayandi nor the names of
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Math
+ * @package Math_Numerical_RootFinding
+ * @subpackage Methods
+ * @author Firman Wandayandi <firman@php.net>
+ * @copyright Copyright (c) 2004-2006 Firman Wandayandi
+ * @license http://www.opensource.org/licenses/bsd-license.php
+ * BSD License
+ * @version CVS: $Id$
+ */
+
+// }}}
+// {{{ Dependencies
+
+/**
+ * Load Math_Numerical_RootFinding_Common as base class.
+ */
+require_once 'Math/Numerical/RootFinding/Common.php';
+
+// }}}
+// {{{ Class: Math_Numerical_RootFinding_RalstonRabinowitz
+
+/**
+ * Ralston and Rabinowitz method class.
+ *
+ * @category Math
+ * @package Math_Numerical_RootFinding
+ * @subpackage Methods
+ * @author Firman Wandayandi <firman@php.net>
+ * @copyright Copyright (c) 2004-2006 Firman Wandayandi
+ * @license http://www.opensource.org/licenses/bsd-license.php
+ * BSD License
+ * @version Release: @package_version@
+ */
+class Math_Numerical_RootFinding_RalstonRabinowitz
+extends Math_Numerical_RootFinding_Common
+{
+ // {{{ Constructor
+
+ /**
+ * Constructor.
+ *
+ * @param array $options (optional) Options.
+ *
+ * @access public
+ * @see Math_Numerical_RootFinding_Common::Math_Numerical_RootFinding_Common()
+ */
+ function Math_Numerical_RootFinding_RalstonRabinowitz($options = null)
+ {
+ parent::Math_Numerical_RootFinding_Common($options);
+ }
+
+ // }}}
+ // {{{ infoCompute()
+
+ /**
+ * Print out parameters description for compute() function.
+ *
+ * @access public
+ */
+ function infoCompute()
+ {
+ print "<h2>False Position::compute()</h2>\n" .
+
+ "<em>float</em> | <em>PEAR_Error</em> " .
+ "<strong>compute</strong>(<u>\$fxFunction</u>, " .
+ "<u>\$dfxFunction</u>, <u>\$xR0</u>, " .
+ "<u>\$xR1</u>)<br />\n" .
+
+ "<h3>Description</h3>\n" .
+ "<em>callback</em> <u>\$fxFunction</u> Callback f(x) equation " .
+ "function or object/method tuple.<br>\n" .
+
+ "<em>callback</em> <u>\$dfxFunction</u> Callback f'(x) " .
+ "equation function or object/method tuple.<br>\n" .
+
+ "<em>float</em> <u>\$xR0</u> First initial guess.<br>\n" .
+
+ "<em>float</em> <u>\$xR1</u> Second initial guess.<br>\n";
+ }
+
+ // }}}
+ // {{{ compute()
+
+ /**
+ * Ralston and Rabinowitz method for calculate double root (twin root).
+ *
+ * @param callback $fxFunction Callback f(x) equation function or
+ * object/method tuple.
+ * @param callback $dxFunction Callback f'(x) equation function or
+ * object/method tuple.
+ * @param float $xR0 First initial guess.
+ * @param float $xR1 Second initial guess.
+ *
+ * @return float|PEAR_Error Root value on success or PEAR_Error on failure.
+ * @access public
+ * @see Math_Numerical_RootFinding_Common::validateEqFunction()
+ * @see Math_Numerical_RootFinding_Common::getEqResult()
+ * @see Math_Numerical_RootFinding_Common::isDivergentRow()
+ * @see Math_Numerical_RootFinding_Secant::compute()
+ */
+ function compute($fxFunction, $dxFunction, $xR0, $xR1)
+ {
+ // Validate f(x) equation function.
+ $err = Math_Numerical_RootFinding_Common::validateEqFunction($fxFunction);
+ if (PEAR::isError($err)) {
+ return $err;
+ }
+
+ // Validate f'(x) equation function.
+ $err = Math_Numerical_RootFinding_Common::validateEqFunction($dxFunction);
+ if (PEAR::isError($err)) {
+ return $err;
+ }
+
+ // Sets maximum iteration and tolerance from options.
+ $maxIteration = $this->options['max_iteration'];
+ $errTolerance = $this->options['err_tolerance'];
+
+ // Sets variable for saving errors during iteration, for divergent
+ // detection.
+ $epsErrors = array();
+
+ for($i = 1; $i <= $maxIteration; $i++) {
+ // Calculate f(x[i-1]) and f'(x[1]), where: x[i-1] = $xR0.
+ $fxR0 = Math_Numerical_RootFinding_Common::getEqResult($fxFunction, $xR0);
+ $dxR0 = Math_Numerical_RootFinding_Common::getEqResult($dxFunction, $xR0);
+
+ // Calculate f(x[i]) and f'(x[1]), where: x[i] = $xR1.
+ $fxR1 = Math_Numerical_RootFinding_Common::getEqResult($fxFunction, $xR1);
+ $dxR1 = Math_Numerical_RootFinding_Common::getEqResult($dxFunction, $xR1);
+
+ // Calculate f(x[i-1]) / f'(x[i-1], where x[i-1] = $xR0.
+ $uxR0 = $fxR0 / $dxR0;
+
+ // Calculate f(x[i]) / f'(x[i]), where x[i] = $xR1;
+ $uxR1 = $fxR1 / $dxR1;
+
+ // Avoid division by zero.
+ if ($uxR0 - $uxR1 == 0) {
+ return PEAR::raiseError('Iteration skipped division by zero');
+ }
+
+ // Ralston and Rabinowitz's formula.
+ $xN = $xR1 - ($uxR1 * ($xR0 - $xR1) / ($uxR0 - $uxR1));
+
+ // xR is the root.
+ if ($xN == 0) {
+ $this->root = $xR;
+ break;
+ }
+
+ // Compute error.
+ $this->epsError = abs(($xN - $xR1) / $xN);
+ $epsErrors[] = $this->epsError;
+
+ // Detect for divergent rows.
+ if ($this->isDivergentRows($epsErrors) &&
+ $this->options['divergent_skip'])
+ {
+ return PEAR::raiseError('Iteration skipped, divergent rows detected');
+ break;
+ }
+
+ // Check for error tolerance, if lower than or equal with
+ // $errTolerance it is the root.
+ if ($this->epsError <= $errTolerance) {
+ $this->root = $xR1;
+ break;
+ }
+
+ // Swicth the values for next iteration x[i] -> x[i-1] and
+ // x[i+1] -> x[i], where: x[i-1] = $xR0, x[i] = $xR1, and
+ // x[i+1] = $xN.
+ $xR0 = $xR1;
+ $xR1 = $xN;
+ }
+ $this->iterationCount = $i;
+ return $this->root;
+ }
+
+ // }}}
+}
+
+// }}}
+
+/*
+ * Local variables:
+ * mode: php
+ * tab-width: 4
+ * c-basic-offset: 4
+ * c-hanging-comment-ender-p: nil
+ * End:
+ */
+?>

0 comments on commit f8ef687

Please sign in to comment.
Something went wrong with that request. Please try again.