File: example.quadraticequation.php Example script text/plain An usage example Quadratic Equation Solver Get the roots of a quadratic equation By Alexandre Cisneiros Typo correction. 11 years ago 2,213 bytes

Contents

Let's do some math!

3x^2 - 4x + 2 = 0 (roots: ' . new QuadraticEquation(3, -4, 2) . ')'); print ('

'); // Step-by-step generation: 4x^2 + 2x - 10 = 0 (precision = 3) \$eq = new QuadraticEquation; \$eq->setA(4); \$eq->setB(2); \$eq->setC(-10); \$eq->setPrecision(3); print ('Step-by-step generation with precision = 3:
4x^2 + 2x - 10 = 0 (roots: '); foreach (\$eq->getRoots() as \$root) // Please do a try/catch as done below. This is only an example! {     print (\$root . ';'); } print (')

'); // It is always a good practice to check for errors! // Error checking: 4x^2 + -4x + 100 = 0 (default precision = 2) \$eq = new QuadraticEquation(4, -4, 100); print ('Error checking:
4x^2 + -4x + 100 = 0 (roots: '); try {     foreach (\$eq->getRoots() as \$root)     {         print (\$root . ';');     } } catch (Exception \$e) {     print (\$e->getMessage()); } print (')

'); // You don't have to create another instance of the object to solve another equation, // if you don't want to. You can just set new values and getRoots() once more. I'll use the // same \$eq instance we just made, change the values and solve another equation. \$eq->setC(0); \$eq->setPrecision(4); print ('Using the same object with new values (precision = 4):
4x^2 + 2x = 0 (roots: '); try {     foreach (\$eq->getRoots() as \$root)     {         print (\$root . ';');     } } catch (Exception \$e) {     print (\$e->getMessage()); } print (')

'); ?>

Thanks for using my class! I'd like to know if you use it, so please notify me on alexandre [at] cisneiros.com! ;-)
Alexandre Cisneiros Filho - http://cisneiros.com