30 if (!defined(
'PHPEXCEL_ROOT')) {
34 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../../');
37 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/trend/bestFitClass.php';
38 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/JAMA/Matrix.php';
63 foreach($slope as
$key => $value) {
65 $retVal += $value * pow($xValue,
$key + 1);
81 $equation =
'Y = '.$intersect;
82 foreach($slope as
$key => $value) {
84 $equation .=
' + '.$value.
' * X';
86 $equation .=
'^'.($key + 1);
96 $coefficients = array();
97 foreach($this->_slope as $coefficient) {
98 $coefficients[] = round($coefficient,$dp);
100 return $coefficients;
113 $x_sum = array_sum($xValues);
114 $y_sum = array_sum($yValues);
115 $xx_sum = $xy_sum = 0;
117 $xy_sum += $xValues[$i] * $yValues[$i];
118 $xx_sum += $xValues[$i] * $xValues[$i];
119 $yy_sum += $yValues[$i] * $yValues[$i];
130 for ($j = 0; $j <= $order; ++$j) {
131 $A[$i][$j] = pow($xValues[$i], $j);
135 $B[$i] = array($yValues[$i]);
137 $matrixA =
new Matrix($A);
138 $matrixB =
new Matrix($B);
139 $C = $matrixA->solve($matrixB);
141 $coefficients = array();
142 for($i = 0; $i < $C->m; ++$i) {
144 if (abs($r) <= pow(10, -9)) {
147 $coefficients[] = $r;
150 $this->_intersect = array_shift($coefficients);
151 $this->_slope = $coefficients;
154 foreach($this->_xValues as $xKey => $xValue) {
160 function __construct($order, $yValues, $xValues=array(), $const=True) {
161 if (parent::__construct($yValues, $xValues) !== False) {
162 if ($order < $this->_valueCount) {
163 $this->_bestFitType .=
'_'.$order;
164 $this->_order = $order;
167 $this->_error = True;
170 $this->_error = True;