PHPExcel

# Source for file LagrangeInterpolation2.php

Documentation is available at LagrangeInterpolation2.php

1. <?php
3. require_once "../Matrix.php";
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. class LagrangeInterpolation {
24. public function findPolynomialFactors ($x, $y) {
25. $n = count ($x);
27. $data = array(); // double[n][n];
28. $rhs = array(); // double[n];
30. for ($i = 0; $i < $n; ++$i) {
31. $v = 1;
32. for ($j = 0; $j < $n; ++$j) {
33. $data[$i][$n-$j-1] = $v;
34. $v *= $x[$i];
35. }
36. $rhs[$i] = $y[$i];
37. }
39. // Solve m * s = b
40. $m = new Matrix($data);
41. $b = new Matrix($rhs, $n);
43. $s = $m->solve($b);
45. return $s->getRowPackedCopy();
46. } // function findPolynomialFactors()
48. } // class LagrangeInterpolation
51. $x = array(2.0, 1.0, 3.0);
52. $y = array(3.0, 4.0, 7.0);
54. $li = new LagrangeInterpolation ;
55. $f = $li-> findPolynomialFactors ($x, $y);
57. for ($i = 0; $i < 3; ++$i) {
58. echo $f[$i]."<br />";
59. }

Documentation generated on Fri, 12 Oct 2012 00:16:32 +0200 by phpDocumentor 1.4.4