default
[ class tree: default ] [ index: default ] [ 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.  
  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.  
  36.       $rhs[$i] = $y[$i];
  37.     }
  38.  
  39.     // Solve m * s = b
  40.     
  41.     $m = new Matrix($data);
  42.     $b = new Matrix($rhs, $n);
  43.  
  44.     $s = $m->solve($b);
  45.  
  46.     return $s->getRowPackedCopy();
  47.  
  48.   }
  49.  
  50. }
  51.  
  52. $x = array(2.0, 1.0, 3.0);
  53. $y = array(3.0, 4.0, 7.0);
  54.  
  55. $li = new LagrangeInterpolation;
  56. $f = $li->findPolynomialFactors($x, $y);
  57.  
  58. for ($i = 0; $i < 3; $i++)
  59.   echo $f[$i]."<br />";
  60.  
  61. ?>

Documentation generated on Mon, 05 Jan 2009 20:38:00 +0100 by phpDocumentor 1.4.1