public function testSetOptions() { $options = array('max_iteration' => 20, 'divergent_skip' => false); $o = Math_Numerical_RootFinding::factory('Bisection', $options); $this->assertTrue($o instanceof Math_Numerical_RootFinding_Bisection); $this->assertEquals($o->get('max_iteration'), $options['max_iteration']); $this->assertFalse($o->get('divergent_skip')); }
public function testCompute() { $o = Math_Numerical_RootFinding::factory('Secant'); $res = $o->compute(array(get_class($this), 'Fx'), 0, 1); if (PEAR::isError($res)) { $this->fail('Error has returned from compute(): ' . $res->getMessage()); } $exact_root = 0.56714329; $this->assertLessThanOrEqual($o->get('max_iteration'), $o->getIterationCount(), 'Invalid iteration count'); $this->assertLessThanOrEqual($o->get('err_tolerance'), $o->getEpsError()); $this->assertGreaterThanOrEqual($exact_root - $o->get('err_tolerance'), $o->getRoot()); $this->assertLessThanOrEqual($exact_root + $o->get('err_tolerance'), $o->getRoot()); }
* Math_Numerical_RootFinding class. */ require_once 'Math/Numerical/RootFinding.php'; /** * f(x) callback function. * * @param float $x Variable value. * * @return float */ function gx($x) { return pow(M_E, -$x); } // Create new instance of Math_Numerical_RootFinding_Fixedpoint. $mroot = Math_Numerical_RootFinding::factory('fixedpoint'); if (PEAR::isError($mroot)) { die($mroot->toString()); } // Calculate the root using Fixed Point's method. $root = $mroot->compute('gx', 0); if (PEAR::isError($root)) { die($root->toString()); } print "<h1>Root Finding::Fixed Point Method</h1>\n"; $mroot->infoCompute(); print "<h2>Case</h2>\n"; print "f(x) = e<sup>-x</sup> - x<br />\n"; print "g(x) = e<sup>-x</sup><br />\n"; print "<h3>Parameters</h3>\n"; print "<b>Initial guest</b> = 0<br />\n";
*/ require_once 'Math/Numerical/RootFinding.php'; /** * f(x) callback function. * * @param float $x Variable value. * * @return float * @ignore */ function fx($x) { return pow(M_E, -$x) - $x; } // Create new instance of Math_Numerical_RootFinding_Falseposition. $mroot = Math_Numerical_RootFinding::factory('falseposition'); if (PEAR::isError($mroot)) { die($mroot->toString()); } // Calculate the root using False Position's method. $root = $mroot->compute('fx', 0, 1); if (PEAR::isError($root)) { die($root->toString()); } print "<h1>Root Finding::False Position Method</h1>\n"; $mroot->infoCompute(); print "<h2>Case</h2>\n"; print "f(x) = e<sup>-x</sup> - x<br />\n"; print "<h3>Parameters</h3>\n"; print "<b>First initial guest</b> = 0<br />\n"; print "<b>Second initial guest</b> = 1<br />\n";
return pow($x, 3) - 5 * pow($x, 2) + 7 * $x - 3; } /** * f'(x) callback function. * * @param float $x Variable value. * * @return float * @ignore */ function dx($x) { return 3 * pow($x, 2) - 10 * $x + 7; } // Create new instance of Math_Numerical_RootFinding_Ralstonrabinowitz. $mroot = Math_Numerical_RootFinding::factory('RalstonRabinowitz'); if (PEAR::isError($mroot)) { die($mroot->toString()); } // Calculate the root using Ralston and Rabinowitz's method. $root = $mroot->compute('fx', 'dx', 0, 4); if (PEAR::isError($root)) { die($root->toString()); } print "<h1>Root Finding::Ralston and Rabinowitz Method</h1>\n"; $mroot->infoCompute(); print "<h2>Case</h2>\n"; print "f(x) = x<sup>3</sup> - 5x<sup>2</sup> + 7x - 3<br />\n"; print "f'(x) = 3x<sup>2</sup> - 10x + 7<br />\n"; print "<h3>Parameters</h3>\n"; print "<b>First initial guest</b> (<u>\$xR0</u>) = 0<br />\n";
*/ require_once 'Math/Numerical/RootFinding.php'; /** * f(x) callback function. * * @param float $x Variable value. * * @return float * @ignore */ function fx($x) { return pow(M_E, -$x) - $x; } // Create new instance of Math_Numerical_RootFinding_Bisection. $mroot = Math_Numerical_RootFinding::factory('Bisection'); if (PEAR::isError($mroot)) { die($mroot->toString()); } // Calculate the root using Bisection's method. $root = $mroot->compute('fx', 0, 1); if (PEAR::isError($root)) { die($root->toString()); } print "<h1>Root Finding::Bisection Method</h1>\n"; $mroot->infoCompute(); print "<h2>Case</h2>\n"; print "f(x) = e<sup>-x</sup> - x<br />\n"; print "<h3>Parameters</h3>\n"; print "<b>Lower guest</b> = 0<br />\n"; print "<b>Upper guest</b> = 1<br />\n";
*/ require_once 'Math/Numerical/RootFinding.php'; /** * f(x) callback function * * @param float $x Variable value. * * @return float * @ignore */ function fx($x) { return pow(M_E, -$x) - $x; } // Create new instance of Math_Numerical_RootFinding_Secant. $mroot = Math_Numerical_RootFinding::factory('Secant'); if (PEAR::isError($mroot)) { die($mroot->toString()); } // Calculate the root using Secant's method. $root = $mroot->compute('fx', 0, 1); if (PEAR::isError($root)) { die($root->toString()); } print "<h1>Root Finding::Secant Method</h1>\n"; $mroot->infoCompute(); print "<h2>Case</h2>\n"; print "<b>Case:</b><br />\n"; print "f(x) = e<sup>-x</sup> - x<br />\n"; print "<h3>Parameters</h3>\n"; print "<b>First initial guest</b> = 0<br />\n";
return pow(M_E, -$x) - $x; } /** * f'(x) callback function. * * @param float $x Variable value. * * @return float * @ignore */ function dx($x) { return -pow(M_E, -$x) - 1; } // Create new instance of Math_Numerical_RootFinding_Newtonraphson. $mroot = Math_Numerical_RootFinding::factory('NewtonRaphson'); if (PEAR::isError($mroot)) { die($mroot->toString()); } // Calculate the root using Newton-Raphson's method. $root = $mroot->compute('fx', 'dx', 0); if (PEAR::isError($root)) { die($root->toString()); } print "<h1>Root Finding::Newton-Raphson Method</h1>\n"; $mroot->infoCompute(); print "<h2>Case</h2>\n"; print "f(x) = e<sup>-x</sup> - x<br />\n"; print "f'(x) = -e<sup>-x</sup> - x<br />\n"; print "<h3>Parameters</h3>\n"; print "<b>Initial guest</b> = 0<br />\n";
/** * Estimate and return the roots of a high-degree Polynomial ( degree 5 or greater ) * * This function uses Newton's method using the Math_Numerical_RootFinding * PEAR package to estimate the real roots of high-degree Polynomials. If * you already have estimates of where the roots might be, you can pass in * an array of guesses. Otherwise, the method will try to calculate some * good initial guesses for you. * * You must have the Math_Numerical_RootFinding package installed for this * method to work! * * @see Math_PolynomialOp::getRoots(), Math_Numerical_RootFinding * * @access public * * @param object $m * @param array $guesses * @return array */ function getRootsHighDegree($p, $guesses = array()) { require_once 'Math/Numerical/RootFinding.php'; if (!is_a($p, 'Math_Polynomial')) { $p = new Math_Polynomial($p); } $arr = array(); // Array to store the roots $fx = Math_PolynomialOp::createFunction($p); $dx = Math_PolynomialOp::createFunction(Math_PolynomialOp::getDerivative($p)); $newton = Math_Numerical_RootFinding::factory('Newtonraphson'); if (PEAR::isError($newton)) { return PEAR::raiseError('Math_Numeric_RootFinding could not be instantiated, message was: ' . $newton->toString()); } // We need to find some initial guesses for finding the roots if (count($guesses)) { // User has provided guesses for us foreach ($guesses as $guess) { $arr[] = $newton->compute($fx, $dx, $guess); } } else { // We need to find the guesses ourselves... yuck. $criticals = Math_PolynomialOp::getCriticalPoints($p); $arr[] = $newton->compute($fx, $dx, $criticals[0] - 0.1); $count = count($criticals); for ($i = 1; $i < $count; $i++) { $arr[] = $newton->compute($fx, $dx, ($criticals[$i - 1] + $criticals[$i]) / 2); } $arr[] = $newton->compute($fx, $dx, end($criticals) + 0.1); $arr = array_unique($arr); } return Math_PolynomialOp::_round($arr); }