function polyfit($X, $Y, $n) { 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) $B [$i] = array ( $Y [$i] ); $matrixA = new Matrix ( $A ); $matrixB = new Matrix ( $B ); $C = $matrixA->solve ( $matrixB ); return $C->getMatrix ( 0, $n, 0, 1 ); }
public function findPolynomialFactors($x, $y) { $n = count($x); $data = array(); // double[n][n]; $rhs = array(); // double[n]; for ($i = 0; $i < $n; ++$i) { $v = 1; for ($j = 0; $j < $n; ++$j) { $data[$i][$n - $j - 1] = $v; $v *= $x[$i]; } $rhs[$i] = $y[$i]; } // Solve m * s = b $m = new Matrix($data); $b = new Matrix($rhs, $n); $s = $m->solve($b); return $s->getRowPackedCopy(); }
private function _polynomial_regression($order, $yValues, $xValues, $const) { // calculate sums $x_sum = array_sum ( $xValues ); $y_sum = array_sum ( $yValues ); $xx_sum = $xy_sum = 0; for($i = 0; $i < $this->_valueCount; ++ $i) { $xy_sum += $xValues [$i] * $yValues [$i]; $xx_sum += $xValues [$i] * $xValues [$i]; $yy_sum += $yValues [$i] * $yValues [$i]; } /* * This routine uses logic from the PHP port of polyfit version 0.1 * written by Michael Bommarito and Paul Meagher The function fits a * polynomial function of order $order through a series of x-y data * points using least squares. */ for($i = 0; $i < $this->_valueCount; ++ $i) { for($j = 0; $j <= $order; ++ $j) { $A [$i] [$j] = pow ( $xValues [$i], $j ); } } for($i = 0; $i < $this->_valueCount; ++ $i) { $B [$i] = array ( $yValues [$i] ); } $matrixA = new Matrix ( $A ); $matrixB = new Matrix ( $B ); $C = $matrixA->solve ( $matrixB ); $coefficients = array (); for($i = 0; $i < $C->m; ++ $i) { $r = $C->get ( $i, 0 ); if (abs ( $r ) <= pow ( 10, - 9 )) { $r = 0; } $coefficients [] = $r; } $this->_intersect = array_shift ( $coefficients ); $this->_slope = $coefficients; $this->_calculateGoodnessOfFit ( $x_sum, $y_sum, $xx_sum, $yy_sum, $xy_sum ); foreach ( $this->_xValues as $xKey => $xValue ) { $this->_yBestFitValues [$xKey] = $this->getValueOfYForX ( $xValue ); } } // function _polynomial_regression()
/** * Fit first derivatives to minimise second derivatives. */ function fit_derivs_badly() { $n = $this->size() - 1; // there are 3*$n parameters to fit: polynomial coeffs of orders from 1 to 3 for each spline piece $M = new Matrix(3 * $n, 3 * $n); $b = array_fill(0, 3 * $n, 0); for ($i = 0; $i < $n; ++$i) { $dx = $this->points[$i + 1]->x - $this->points[$i]->x; $dx2 = $dx * $dx; $dx3 = $dx2 * $dx; $j = 3 * $i; $b[$j] = $this->points[$i + 1]->y - $this->points[$i]->y; $M->add($j, $j, $dx); // c_i^1 $M->add($j, $j + 1, $dx2); // c_i^2 $M->add($j, $j + 2, $dx3); // c_i^3 $M->add($j + 1, $j, 1); // c_i^1 $M->add($j + 1, $j + 1, 2 * $dx); // c_i^2 $M->add($j + 1, $j + 2, 3 * $dx2); // c_i^3 if ($i != $n - 1) { $M->add($j + 1, $j + 3, -1); } // c_{i+1}^1 $M->add($j + 2, $j + 1, 1); // c_i^2 $M->add($j + 2, $j + 2, 3 * $dx); // c_i^3 if ($i != $n - 1) { $M->add($j + 2, $j + 4, -1); } // c_{i+1}^2 } //$M->log(); //$M0 = $M->copy(); //echo 'b='.json_encode($b).'<br>'; $x = $M->solve($b); //echo 'b='.json_encode($M0->mulVect($x)).'<br>'; for ($i = 0; $i < $n; ++$i) { $j = 3 * $i; $p = $this->points[$i]; array_push($this->coeffs, array($p->y, $x[$j], $x[$j + 1], $x[$j + 2])); } $p = $this->points[$n]; array_push($this->coeffs, array($p->y, 0, 0, 0)); }
/** * @dataProvider dataProviderForSolveExceptionNotVectorOrArray */ public function testSolveExceptionNotVectorOrArray($b) { $A = new Matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5]]); $this->setExpectedException('MathPHP\\Exception\\IncorrectTypeException'); $A->solve($b); }