+ Reply to Thread
Results 1 to 6 of 6
Hybrid View
-
09-06-2007, 01:58 PM #1
Registered User
- Join Date
- Aug 2007
- Location
- New York State, USA
- Posts
- 122
Lagrangian Interpolation For Your Inner Geek
For anyone into high-level math, this function may come in handy.
I use it frequently in astronomy computations.
It performs LaGrangian interpolation for any general Y value given any number of matching XY-data pairs.
This LaGrangian method does not require that the data be linear, ie. constant intervals between data points as do forward or central difference methods.
The LaGrange interpolator function simply computes values in between the tabulated values of Y corresponding to any given value of X within the tabular range.
USAGE EXAMPLE:
You have the following table of sines of angles in degrees:
Code:TEST DATA INPUT TABLE X Data Y Data 29.43 0.4913598528 30.97 0.5145891926 27.69 0.4646875083 28.11 0.4711658342 31.58 0.5236885653 33.05 0.5453707057 Where: X = Angle in degrees Y = Sine of angle X
For each X-data value, there is a corresponding Y-value.
For example, using the above data to interpolate the sine of 30 degrees:
The result of the above code example is:Code:$xDataVector = "29.43 30.97 27.69 28.11 31.58 33.05"; $yDataVector = "0.4913598528 0.5145891926 0.4646875083 0.4711658342 0.5236885653 0.5453707057"; $x = 30; // degrees print Lagrange_Interp ($xDataVector, $yDataVector, $x);
x = 30
which gives
y = 0.5000000000180556
The exact value of sine(30 degrees) is 0.5, so the LaGrangian interpolator gives a result accurate to 10 significant figures in this case.
Here is the code for this function:
Code:/* LaGrangian Interpolation for any number of XY-data pairs. Author: Jay Tanner ©2007 PHP v4.4.4 Released under provisions of GPL v3 http://www.gnu.org/licenses ===== NOTES Both X and Y must represent continuous functions and may be linear or non-linear. No two values of X may be identical. */ function Lagrange_Interp ($xDataVectorArg, $yDataVectorArg, $xArg) { // ---------------------------------------------------- // Read XY data vector strings, stripping all redundant // white spaces from within the strings. $Xdv = preg_replace('/\s+/', ' ', trim($xDataVectorArg)); $Ydv = preg_replace('/\s+/', ' ', trim($yDataVectorArg)); // ---------------------------- // Split XY data vector strings // into matching indexed arrays. $xData = split("[ ]", $Xdv); $yData = split("[ ]", $Ydv); // --------------------------------- // Count number of XY data elements. // For every X value there must be a // matching Y value, but no two X // values can be the same. $xDataCount = count($xData); $yDataCount = count($yData); // ----------------------------------------------------------- // Return error message if data vector element count mismatch. // For every X value there must be a corresponding Y value // or an XY data count mismatch error occurs. if ($xDataCount != $yDataCount) {return "ERROR: XY Data Count Mismatch";} // ------------------------------ // Number of matching data pairs. $n = ($xDataCount + $yDataCount) / 2; // ----------------------------- // Read X argument for which to // interpolate the Y value based // on the given XY data. $x = trim($xArg); // GIVEN X, INTERPOLATE THE CORRESPONDING Y VALUE // -------------------------------- // Initialise y series accumulator. $y=0; // ----------------------- // Compute product series. for ($i=0; $i < $n; $i++) { $c = "1"; for ($j=0; $j < $n; $j++) { if ($j != $i) { $P = bcMul($c, bcSub($x, $xData[$j], 24), 24); $Q = bcSub($xData[$i], $xData[$j], 24); $c = bcDiv($P, $Q, 24); } } // Next j // ------------------------------------- // Accumulate sum of product terms. $y = bcAdd($y, bcMul($c, $yData[$i], 24), 24); } // Next i // Remember numerical sign of // interpolated y as ±1 $sign = ($y < 0)? -1 : +1; // ---------------------------------------------------------- // Round off interpolated y value to 16-decimal output limit. $y = bcMul($sign, bcAdd(bcMul($sign, $y, 24), "0.00000000000000005", 16), 16); // -------------------------------------------------------------- // Cut off any redundant zeros and/or decimal point from y value. if (StrPos($y, ".") !== FALSE) {$y = RTrim(RTrim($y, "0"), ".");} // Done. return $y; } // End of Lagrange_Interp()
Since I can't post equation graphics on this forum, the following URL, from where I shamelessly stole the formulas used in the program, better explains what the program does.
[url]http://mathworld.wolfram.com/LagrangeInterpolatingPolynomial.html[/url]Last edited by JayT; 09-07-2007 at 06:11 AM.
Oh to be free, so blissfully free, of the ravages of intelligence, there is no greater joy! - The Cweationist's Cweed
All that is necessary for evil to triumph is a good PR firm.
Very funny, Scotty. Now beam down my clothes!
-
09-07-2007, 05:49 AM #2
Tip: add your name or alias into the top comment of your code. It prevents at least the most idiotic people copying it and saying "lolol look at my awesome code!".
-
09-07-2007, 06:06 AM #3
Registered User
- Join Date
- Aug 2007
- Location
- New York State, USA
- Posts
- 122
ThanX
Oh to be free, so blissfully free, of the ravages of intelligence, there is no greater joy! - The Cweationist's Cweed
All that is necessary for evil to triumph is a good PR firm.
Very funny, Scotty. Now beam down my clothes!
-
09-08-2007, 01:13 PM #4
Registered User
- Join Date
- Aug 2007
- Location
- New York State, USA
- Posts
- 122
O Home On Lagrange
LAGRANGE REVISITED
Here's another practical use for the Lagrange interpolation function posted here.
Suppose you wanted to make a program to convert between temperature scales?
The Lagrange interpolator function is ideal for this purpose, since temperature scale conversions are linear.
In the case of a linear equation passing through both sets of data points, the interpolator is not restricted to only within the given table but can extrapolate beyond the given table values.
All the data you need to do this is two known values on each scale.
For example, we want to convert 177.1 degrees F (Fahrenheit) into its equivalent on the C (Celsius) scale, but we do not remember the formula.
What we do remember is the freezing and boiling points of water on each scale.
To use the Lagrange interpolator to convert 177.1 F into its Celsius equivalent, simply perform the function call:Code:F Degrees C Degrees 32 0 = Freezing on each scale 212 100 = Boiling on each scale
Code:$F = 177.1; // Fahrenheit input argument print Lagrange_Interp ("32 212", "0 100", $F);
The printed output is 80.61111111111111 Celsius degrees, which is the equivalent of 177.1 degrees Fahrenheit.
To reverse the process, for converting from Celsius into Fahrenheit, simply reverse the data input data strings.
Code:C Degrees F Degrees 0 32 = Freezing on each scale 100 212 = Boiling on each scale
To convert 37 degrees Celsius to Fahrenheit:
The printed output is 98.6 degrees Fahrenheit, equivalent to 37 degrees Celsius.Code:$C = 37;// Celsius input argument print Lagrange_Interp ("0 100", "32 212", $C);
All we need to know is any two known temperatures on any temperature scales to use the Lagrange interpolator function as a general purpose temperature scale inter-converter.
.Last edited by JayT; 09-08-2007 at 01:16 PM.
Oh to be free, so blissfully free, of the ravages of intelligence, there is no greater joy! - The Cweationist's Cweed
All that is necessary for evil to triumph is a good PR firm.
Very funny, Scotty. Now beam down my clothes!
-
09-08-2007, 01:38 PM #5
Well, that actually is useful, but I prefer the regular formulas more than this LaGrange stuff.
"Workers of the world unite; you have nothing to lose but your chains." -Karl Marx
-
09-08-2007, 02:39 PM #6
Registered User
- Join Date
- Aug 2007
- Location
- New York State, USA
- Posts
- 122
Lagrange Is Good For What Ails You
The Lagrange method is for when you don't remember or even know the formula but have some known values with which to start.
Given the known data, the function creates the necessary formula internally. Then all you have to do is enter any unknown value to be converted.
The primary use of the Lagrange function is for generating a formula for fitting a curve to given data or interpolating for an unknown value between known values within a table, but it does have many practical uses, especially when the law behind the data is unknown.
I use it a lot, but I'm weird anyway - or is it wired?
But, don't fret!
The function does all the work for you.
Oh to be free, so blissfully free, of the ravages of intelligence, there is no greater joy! - The Cweationist's Cweed
All that is necessary for evil to triumph is a good PR firm.
Very funny, Scotty. Now beam down my clothes!


Reply With Quote

