Source for file polyfit.php
Documentation is available at polyfit.php
require_once "../Matrix.php";
* @author Michael Bommarito
* Function to fit an order n polynomial function through
* a series of x-y data points using least squares.
* @param $X array x values
* @param $Y array y values
* @param $n int order of polynomial to be used for fitting
* @returns array $coeffs of polynomial coefficients
* Pre-Conditions: the system is not underdetermined: sizeof($X) > $n+1
for ($i = 0; $i < sizeof($X); ++ $i)
for ($j = 0; $j <= $n; ++ $j)
$A[$i][$j] = pow($X[$i], $j);
for ($i= 0; $i < sizeof($Y); ++ $i)
$matrixA = new Matrix($A);
$matrixB = new Matrix($B);
$C = $matrixA->solve($matrixB);
return $C->getMatrix(0, $n, 0, 1);
for($i = $C->m - 1; $i >= 0; -- $i) {
echo $r . "x<sup>$i</sup>";
echo " + " . $r . "x<sup>$i</sup>";
$Y = array(4,3,12,67,228, 579);
$points = new Matrix(array($X, $Y));
$Y = array(1,2,5,10,17, 26);
$points = new Matrix(array($X, $Y));
$X = array(0,1,2,3,4,5,6);
$Y = array(- 90,- 104,- 178,- 252,- 26, 1160, 4446);
$points = new Matrix(array($X, $Y));
$points = new Matrix(array($X, $Y));
|