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

Source for file LagrangeInterpolation2.php

Documentation is available at LagrangeInterpolation2.php

  1. <?php
  2.  
  3. require_once "../Matrix.php";
  4.  
  5. /**
  6.  * Given n points (x0,y0)...(xn-1,yn-1), the following method 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.  * @see http://source.freehep.org/jcvsweb/ilc/LCSIM/wdview/lcsim/src/org/lcsim/fit/polynomial/PolynomialFitter.java
  16.  * @author Jacob Dreyer
  17.  * @author Paul Meagher (port to PHP and minor changes)
  18.  *
  19.  * @param x[] float
  20.  * @param y[] float
  21.  */
  22.  
  23.     public function findPolynomialFactors($x$y{
  24.         $n count($x);
  25.  
  26.         $data array();  // double[n][n];
  27.         $rhs  array();  // double[n];
  28.  
  29.         for ($i 0$i $n++$i{
  30.             $v 1;
  31.             for ($j 0$j $n++$j{
  32.                 $data[$i][$n-$j-1$v;
  33.                 $v *= $x[$i];
  34.             }
  35.             $rhs[$i$y[$i];
  36.         }
  37.  
  38.         // Solve m * s = b
  39.         $m new Matrix($data);
  40.         $b new Matrix($rhs$n);
  41.  
  42.         $s $m->solve($b);
  43.  
  44.         return $s->getRowPackedCopy();
  45.     }    //    function findPolynomialFactors()
  46.  
  47. }    //    class LagrangeInterpolation
  48.  
  49.  
  50. $x array(2.01.03.0);
  51. $y array(3.04.07.0);
  52.  
  53. $li new LagrangeInterpolation;
  54. $f $li->findPolynomialFactors($x$y);
  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