29 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/trend/bestFitClass.php';
30 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/JAMA/Matrix.php';
55 foreach($slope as $key => $value) {
57 $retVal += $value * pow($xValue, $key + 1);
73 $equation =
'Y = '.$intersect;
74 foreach($slope as $key => $value) {
76 $equation .=
' + '.$value.
' * X';
78 $equation .=
'^'.($key + 1);
88 $coefficients = array();
89 foreach($this->_slope as $coefficient) {
90 $coefficients[] = round($coefficient,$dp);
105 $x_sum = array_sum($xValues);
106 $y_sum = array_sum($yValues);
107 $xx_sum = $xy_sum = 0;
109 $xy_sum += $xValues[$i] * $yValues[$i];
110 $xx_sum += $xValues[$i] * $xValues[$i];
111 $yy_sum += $yValues[$i] * $yValues[$i];
122 for ($j = 0; $j <= $order; ++$j) {
123 $A[$i][$j] = pow($xValues[$i], $j);
127 $B[$i] = array($yValues[$i]);
129 $matrixA =
new Matrix($A);
130 $matrixB =
new Matrix($B);
131 $C = $matrixA->solve($matrixB);
133 $coefficients = array();
134 for($i = 0; $i < $C->m; ++$i) {
136 if (abs($r) <= pow(10, -9)) {
139 $coefficients[] = $r;
142 $this->_intersect = array_shift($coefficients);
143 $this->_slope = $coefficients;
146 foreach($this->_xValues as $xKey => $xValue) {
152 function __construct($order, $yValues, $xValues=array(), $const=True) {
153 if (parent::__construct($yValues, $xValues) !== False) {
154 if ($order < $this->_valueCount) {
155 $this->_bestFitType .=
'_'.$order;
156 $this->_order = $order;
159 $this->_error = True;
162 $this->_error = True;