Permalink
Browse files

add new examples

  • Loading branch information...
Firman Wandayandi
Firman Wandayandi committed Nov 10, 2008
1 parent 41f5c0a commit 91db16e402b5ede5ecde1b074466475f14f3f2e6
@@ -0,0 +1,68 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker */
+
+// $Id$
+
+/**
+ * Bisection method usage example.
+ *
+ * @author Firman Wandayandi <firman@php.net>
+ * @package Math_Numerical_RootFinding
+ * @subpackage Examples
+ */
+
+/**
+ * Math_Numerical_RootFinding class
+ */
+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";
+print "<b>True root</b> = 0,56714329...<br />\n";
+
+print "<h3>Result</h3>\n";
+print sprintf("<b>Iteration count</b> = %d<br />\n", $mroot->getIterationCount());
+print sprintf("<b>Root value</b> = %f<br />\n", $mroot->getRoot());
+print sprintf("<b>Estimate error</b> = %f<br />\n", $mroot->getEpsError());
+
+/*
+ * Local variables:
+ * mode: php
+ * tab-width: 4
+ * c-basic-offset: 4
+ * c-hanging-comment-ender-p: nil
+ * End:
+ */
+?>
@@ -0,0 +1,68 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker */
+
+// $Id$
+
+/**
+ * False Position method usage example.
+ *
+ * @author Firman Wandayandi <firman@php.net>
+ * @package Math_Numerical_RootFinding
+ * @subpackage Examples
+ */
+
+/**
+ * Math_Numerical_RootFinding class
+ */
+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";
+print "<b>True root</b> = 0,56714329...<br />\n";
+
+print "<h3>Result</h3>\n";
+print sprintf("<b>Iteration count</b> = %d<br />\n", $mroot->getIterationCount());
+print sprintf("<b>Root value</b> = %f<br />\n", $mroot->getRoot());
+print sprintf("<b>Estimate error</b> = %f<br />\n", $mroot->getEpsError());
+
+/*
+ * Local variables:
+ * mode: php
+ * tab-width: 4
+ * c-basic-offset: 4
+ * c-hanging-comment-ender-p: nil
+ * End:
+ */
+?>
@@ -0,0 +1,67 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker */
+
+// $Id$
+
+/**
+ * Fixed Point method usage example.
+ *
+ * @author Firman Wandayandi <firman@php.net>
+ * @package Math_Numerical_RootFinding
+ * @subpackage Examples
+ */
+
+/**
+ * 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";
+print "<b>True root</b> = 0.56714329<br />\n";
+
+print "<h3>Result</h3>\n";
+print sprintf("<b>Iteration count</b> = %d<br />\n", $mroot->getIterationCount());
+print sprintf("<b>Root value</b> = %f<br />\n", $mroot->getRoot());
+print sprintf("<b>Estimate error</b> = %f<br />\n", $mroot->getEpsError());
+
+/*
+ * Local variables:
+ * mode: php
+ * tab-width: 4
+ * c-basic-offset: 4
+ * c-hanging-comment-ender-p: nil
+ * End:
+ */
+?>
@@ -0,0 +1,81 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker */
+
+// $Id$
+
+/**
+ * Newton-Raphson method usage example.
+ *
+ * @author Firman Wandayandi <firman@php.net>
+ * @package Math_Numerical_RootFinding
+ * @subpackage Examples
+ */
+
+/**
+ * Require Math_Numerical_RootFinding class.
+ */
+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;
+}
+
+/**
+ * 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";
+print "<b>True root</b> = 0,56714329...<br />\n";
+
+print "<h3>Result</h3>\n";
+print sprintf("<b>Iteration count</b> = %d<br />\n", $mroot->getIterationCount());
+print sprintf("<b>Root value</b> = %f<br />\n", $mroot->getRoot());
+print sprintf("<b>Estimate error</b> = %f<br />\n", $mroot->getEpsError());
+
+/*
+ * Local variables:
+ * mode: php
+ * tab-width: 4
+ * c-basic-offset: 4
+ * c-hanging-comment-ender-p: nil
+ * End:
+ */
+?>
@@ -0,0 +1,93 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker */
+
+// $Id$
+
+/**
+ * Newton-Raphson 2 method usage example.
+ *
+ * @author Firman Wandayandi <firman@php.net>
+ * @package Math_Numerical_RootFinding
+ * @subpackage Examples
+ */
+
+/**
+ * Math_Numerical_RootFinding class.
+ */
+require_once 'Math/Numerical/RootFinding.php';
+
+/**
+ * f(x) callback function.
+ *
+ * @param float $x Variable value.
+ *
+ * @return float
+ * @ignore
+ */
+function fx($x)
+{
+ return pow($x, 3) - (5 * pow($x,2)) + (7 * $x) - 3;
+}
+
+/**
+ * f'(x) callback function.
+ *
+ * @param float $x Variable value.
+ *
+ * @return float
+ */
+function d1x($x)
+{
+ return 3 * pow($x, 2) - (10 * $x) + 7;
+}
+
+/**
+ * f''(x) callback function.
+ *
+ * @param float $x Variable value.
+ *
+ * @return float
+ */
+function d2x($x)
+{
+ return 6 * $x - 10;
+}
+
+// Create new instance of Math_Numerical_RootFinding_Newtonraphson2.
+$mroot = Math_Numerical_RootFinding::factory('NewtonRaphson2');
+if (PEAR::isError($mroot)) {
+ die($mroot->toString());
+}
+
+// Calculate the root using Newton-Raphson 2 method.
+$root = $mroot->compute('fx', 'd1x', 'd2x', 0);
+if (PEAR::isError($root)) {
+ die($root->toString());
+}
+
+print "<h1>Root Finding::Newton-Raphson 2 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 "f''(x) = 6x - 10<br />\n";
+
+print "<h3>Parameters</h3>\n";
+print "<b>Initial guest</b> = 0<br />\n";
+print "<b>True root</b> = 1<br />\n";
+
+print "<h3>Result</h3>\n";
+print sprintf("<b>Iteration count</b> = %d<br />\n", $mroot->getIterationCount());
+print sprintf("<b>Root value</b> = %f<br />\n", $mroot->getRoot());
+print sprintf("<b>Estimate error</b> = %f<br />\n", $mroot->getEpsError());
+
+/*
+ * Local variables:
+ * mode: php
+ * tab-width: 4
+ * c-basic-offset: 4
+ * c-hanging-comment-ender-p: nil
+ * End:
+ */
+?>
Oops, something went wrong.

0 comments on commit 91db16e

Please sign in to comment.