PHPExcel
[ class tree: PHPExcel ] [ index: PHPExcel ] [ all elements ]

Source for file LagrangeInterpolation.php

Documentation is available at LagrangeInterpolation.php

  1. <?php
  2.  
  3. require_once "../Matrix.php";
  4.  
  5. /**
  6.  * Given n points (x0,y0)...(xn-1,yn-1), the following methid computes
  7.  * the polynomial factors of the n-1't degree polynomial passing through
  8.  * the n points.
  9.  *
  10.  * Example: Passing in three points (2,3) (1,4) and (3,7) will produce
  11.  * the results [2.5, -8.5, 10] which means that the points are on the
  12.  * curve y = 2.5x² - 8.5x + 10.
  13.  *
  14.  * @see http://geosoft.no/software/lagrange/LagrangeInterpolation.java.html
  15.  * @author Jacob Dreyer
  16.  * @author Paul Meagher (port to PHP and minor changes)
  17.  *
  18.  * @param x[] float
  19.  * @param y[] float
  20.  */
  21.  
  22.     public function findPolynomialFactors($x$y{
  23.         $n count($x);
  24.  
  25.         $data array();  // double[n][n];
  26.         $rhs  array();  // double[n];
  27.  
  28.         for ($i 0$i $n++$i{
  29.             $v 1;
  30.             for ($j 0$j $n++$j{
  31.                 $data[$i][$n-$j-1$v;
  32.                 $v *= $x[$i];
  33.             }
  34.             $rhs[$i$y[$i];
  35.         }
  36.  
  37.         // Solve m * s = b
  38.         $m new Matrix($data);
  39.         $b new Matrix($rhs$n);
  40.  
  41.         $s $m->solve($b);
  42.  
  43.         return $s->getRowPackedCopy();
  44.     }    //    function findPolynomialFactors()
  45.  
  46. }    //    class LagrangeInterpolation
  47.  
  48.  
  49. $x array(2.01.03.0);
  50. $y array(3.04.07.0);
  51.  
  52. $li new LagrangeInterpolation;
  53. $f $li->findPolynomialFactors($x$y);
  54.  
  55.  
  56. for ($i 0$i 3++$i{
  57.     echo $f[$i]."<br />";
  58. }

Documentation generated on Sat, 19 May 2012 14:37:40 +0200 by phpDocumentor 1.4.4