PHP code example of gamajo / quadratic

1. Go to this page and download the library: Download gamajo/quadratic library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gamajo / quadratic example snippets


use Gamajo\Quadratic;

// Represents x^2 + 5x + 6 = 0.
$equation = new BasicQuadraticEquation(1, 5, 6);

$solver = new Solver($equation);
$solver->solve();

echo $solver->get(); // '2 and 3'
echo $solver->get('root1'); // '2'
echo $solver->get('root2'); // '3'

use Gamajo\Quadratic;

// Represents 3x^2 + 4x + 5 = 0.
$equation = new BasicQuadraticEquation(3, 4, 5);

$solver = new Solver($equation);
$solver->solve();

echo $solver->get(); // '-0.667 + 1.106i and -0.667 - 1.106i'

use Gamajo\Quadratic;

// Represents x^2 + 5x + 6 = 0.
$equation = new BasicQuadraticEquation(1, 5, 6);

echo $equation->getA(); // 1
echo $equation->getB(); // 5
echo $equation->getC(); // 6
print_r( $equation->getArgsAsArray() ); // [1, 5, 6]

use Gamajo\Quadratic;

// Represents 8x^2 + 5x - 2 = 0.
$equation = new BasicQuadraticEquation(8, 5, -2);

$solver = new Solver($equation);
$solve->setPrecision(4); // Default precision is 3

echo $solve->getPrecision(); // 4

$solver->solve();

echo $solver->get(); // '-0.9021 and 0.2771' instead of '-0.902 and 0.277'