An interpolation function is used to estimate the values that lie between known data points. We might use an interpolation function to generate additional plotting points so that we can plot a smooth and definite curve through our data points. One popular interpolation function is called the “Lagrange Interpolating Polynomial” which uses a polynomial function to generate points on and between the supplied data points. The Math_Interpolation_Lagrange
class below has two main methods:
- A
setCoefficients
method for finding the polynomial coefficients to use for the interpolating polynomial.
- A
setInterpolants
method for evaluating the polynomial formula at intermediate points using the computed polynomial coefficients.
You can peruse the code comments for more details about how the class works. <?php /** * Math_Interpolation_Lagrange.php * * The Lagrange interpolation object accepts three arguments: * * 1. A data vector of x values * 2. A data vector of y values * 3. A vector of ix values, in the same range as your x values, that * you want to find the cooresponding interpolated values iy for. * * It uses the data vector of x and y values to compute the polynomial * coefficients c and then uses these coefficients to find the interpolated * values iy for the supplied ix values. All supplied and computed results * are stored as instance variables obtainable by accessor methods or * direct reference. * * @author Paul Meagher * @license LGPL * @created Feb 25/2007 * @version 1.0 */ class Math_Interpolation_Lagrange { /** * @var array of x values */ var $x = array(); /** * @var array of y values */ var $y = array(); /** * @var array of polynomial coefficients */ var $c = array(); /** * @var array of x values to be interpolated */ var $ix = array(); /** * @var array of interpolated y values */ var $iy = array(); /** * @var number of elements in data arrays x and y */ var $n = 0; /** * @var number of elements in interpolation arrays ix and iy */ var $m = 0; /** * Constructor sets up the internal storage variables, calls a * method to compute the polynomial cooefficients, then finds * the interpolated iy values given the supplied ix values. * * @param $x array of x values * @param $y array of y values * @param $ix array of x values to be interpolated */ function Math_Interpolation_Lagrange($x, $y, $ix) { $this->x = $x; $this->y = $y; $this->ix = $ix; $this->n = count($x); $this->m = count($ix); $this->setCoefficients(); $this->setInterpolants(); } /** * Computes the polynomial coefficients. */ function setCoefficients() { for($i=0; $i<$this->n; $i++) { $d[$i] = 1; for($j=0; $j<$this->n; $j++) { if($i != $j) $d[$i] = $d[$i] * ($this->x[$i] - $this->x[$j]); $this->c[$i] = $this->y[$i] / $d[$i]; } } } /** * Computes the interpolated iy values given the ix values * supplied in the constructor. */ function setInterpolants() { for($i=0; $i<$this->m; $i++) { $this->iy[$i] = 0; for($j=0; $j<$this->n; $j++) { $d[$j] = 1; for($k=0; $k<$this->n; $k++) { if($j != $k) $d[$j] = $d[$j] * ($this->ix[$i] - $this->x[$k]); } $this->iy[$i] = $this->iy[$i] + $this->c[$j] * $d[$j]; } } } /** * Recomputes the interpolated iy values given the new ix values. * * @param $ix array of new x values to be interpolated */ function changeInterpolants($ix) { $this->ix = $ix; $this->iy = array(); $this->m = count($ix); $this->setInterpolants(); } /** * @return the polynomial coefficients */ function getCoefficients() { return $this->c; } /** * @return array of interpolated values */ function getInterpolants() { return $this->iy; } /** * @return interpolated values as comma separated string */ function toString() { return implode(", ", $this->iy); } } ?>
The lagrange_test.php
script demonstrates how to use the class to obtain two sets of interpolant values: <?php include "Math/Interpolation/Lagrange.php"; $x = array(-2, 0, -1, 1, 2); $y = array( 4, 2, -1, 1, 8); // create x values in data range using step size of .5 $ix1 = range(-2, 2, .5); // create x values in data range using step size of .2 $ix2 = range(-2, 2, .2); $MIL = new Math_Interpolation_Lagrange($x, $y, $ix1); echo "<p>Interpolated values using step size of .5 are: ".$MIL->toString()."</p>"; $MIL->changeInterpolants($ix2); echo "<p>Interpolated values using step size of .2 are: ".$MIL->toString()."</p>"; ?>
Here is the output the script generates: Interpolated values using step size of .5 are: 4, -1.1875, -1, 0.8125, 2, 1.8125, 1, 1.8125,8 Interpolated values using step size of .2 are: 4, 0.9776, -0.7264, -1.4384, -1.4464, -1, -0.3104, 0.4496, 1.1456, 1.6816, 2, 2.0816, 1.9456, 1.6496, 1.2896, 1,0.9536, 1.3616, 2.4736, 4.5776, 8 Exercise 1. Use a PHP graphing package like JpGraph or Image::Graph to plot your x and y data points and the interpolated points ix and iy. Draw a line through your original data points and your interpolated data points. 2. Use a formula to generate a few data points and then use the Math_Interpolation_Lagrange
class to find interpolating values. Come up with a method to measure how accurate your Lagrange interpolating polynomial is by comparing interpolated values to formula generated values. Try using different formulas and compare the relative accuracy of the Lagrange method for those formulas. 3. Use the Runge Function to generate some data points and see how good the Lagrange method is at interpolating values. Note: The Runge function is known to give the Lagrange interpolation method some problems. 4. Create a Math_Interpolation_Chebyshev
class to minimize the weaknesses of the Lagrange method for interpolating the Runge function. See this reference for further details on Chebyshev polynomials.