CodeCogs - An iteractive open source Numerical library Welcome... Login
CodeCogs
shopping cart
OSXWindowsLinux
Search CodeCogs
Numerical Components

Valid RSS

MathsInterpolation

Lagrange

Only available under a commercial licence
COST (GBP)
this unit 6.24
sub units 0.00
add a commercial licence to your cart
0
viewed 10287 times and licensed 14 times

Interpolates a given set of points using the Lagrange polynomial.

Controller: CodeCogs  Contact Controller
+View version details
Contents hide toc
buy now     add cart

Interface

#include <codecogs/maths/interpolation/lagrange.h>

using namespace Maths::Interpolation;

class Lagrange
Interpolates a given set of points using the Lagrange polynomial.
[constructor]Lagrange (int n, double *x, double *y)
Class constructor
[destructor]~Lagrange ()
Class destructor
doublegetValue (double x, int l)
Returns an interpolated value.
intm_n
double*m_x
double Lagrange_once (int N, double *x, double *y, double a, int l)
A static function implementing the Lagrange Class for one off calculations
ExcelReal cc_Lagrange_once (Integer N, Range x, Range y, Real a, Integer l)
This function is available as a Microsoft Excel add-in.

Class Documentation

 
Lagrange
The Lagrange interpolating polynomial is the polynomial of degree n - 1 that passes through the n points

y_1 = f(x_1), y_2 = f(x_2), \ldots, y_n = f(x_n)

It is given by

P(x) = \sum_{j = 1} ^ n P_j(x)

where

P_j(x) = y_j \prod_{k=1 \\ k \neq j} ^ n \frac{x - x_k} {x_j - x_k}

The formula was first published by Waring (1779), rediscovered by Euler in 1783, and published by Lagrange in 1795 (Jeffreys and Jeffreys 1988). An important detail when using this class is that the abscissas array given as argument to the constructor needs to be sorted in ascending order.

Below you will find the interpolation graphs for a set of points obtained by evaluating the function f(x) = \sin(2x) / x, displayed in light blue, at particular abscissas. The Lagrange polynomial, displayed in red, has been calculated using this class. In the first graph there had been chosen a number of 12 points, while in the second 36 points were considered. The level of interpolation in both graphs is 3. The root mean squared error is also displayd in each of the cases.

1/lagrange-378.png
+

References:

Example 1:
The following example displays 20 interpolated values (you may change this amount through the N_out variable) for the given function f(x) with abscissas equally spaced in the [ \pi, 3\pi] interval. The X and Y coordinate arrays are initialized by evaluating this function for N = 12 points equally spaced in the domain from \pi to 5 \pi.
#include <codecogs/maths/interpolation/lagrange.h>
 
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
 
#define PI  3.1415
#define N   12
 
int main() 
{
    // Declare and initialize two arrays to hold the coordinates of the initial data points
    double x[N], y[N];
 
    // Generate the points
    double xx = PI, step = 4 * PI / (N - 1);
    for (int i = 0; i < N; ++i, xx += step) {
        x[i] = xx;
        y[i] = sin(2 * xx) / xx;
    }
 
    // Initialize the Lagrange interpolation routine with known data points
    Maths::Interpolation::Lagrange A(N, x, y);
 
    // Interrogate Lagrange polynomial to find interpolated values
    int N_out = 20;
    xx = PI, step = (3 * PI) / (N_out - 1);
    for (int i = 0; i < N_out; ++i, xx += step) {
        cout << "x = " << setw(7) << xx << "  y = ";
        cout << setw(13) << A.getValue(xx, 3) << endl;
  }
    return 0;
}
Output:
x =  3.1415  y = -5.89868e-005
x = 3.63753  y =      0.216649
x = 4.13355  y =      0.208793
x = 4.62958  y =    -0.0536974
x = 5.12561  y =     -0.186543
x = 5.62163  y =      -0.10577
x = 6.11766  y =     0.0268879
x = 6.61368  y =     0.0875189
x = 7.10971  y =     0.0993752
x = 7.60574  y =     0.0512131
x = 8.10176  y =    -0.0885626
x = 8.59779  y =     -0.123293
x = 9.09382  y =    -0.0160297
x = 9.58984  y =     0.0787203
x = 10.0859  y =     0.0791771
x = 10.5819  y =     0.0216086
x = 11.0779  y =    -0.0212055
x = 11.5739  y =    -0.0727429
x =   12.07  y =    -0.0621462
x =  12.566  y =     0.0312161

See Also

Also consider the regression methods: Regression/Discrete, Regression/Forsythe, Regression/Orthogonal, Regression/Stiefel
Authors:
Lucian Bentea (August 2005)
Source Code:

To view or download source code you need to buy a Commercial licence.

buy now     add cart

Not a member, then Register with CodeCogs. Already a Member, then Login.

Members of Lagrange

 
Lagrangeintn
double*x
double*y )[constructor]
Initializes the necessary data for following evaluations of the polynomial.
Parameters:
nThe number of initial points
xThe x-coordinates for the initial points
yThe y-coordinates for the initial points

 
doublegetValuedoublex
intl )
Returns the approximated ordinate at the given abscissa.
Note:
The value of the x parameter needs to be in the X[0]...X[N - L + 1] interval (including endpoints), where l > 1 is the level of interpolation. For example a level 3 interpolation would have the maximum working interval between X[0] and X[N - 2].
Parameters:
xThe abscissa of the interpolation point
lThe level of interpolation (2 means quadratic)


Function Documentation

Lagrange Once Calculator
  
Add calculator to website or email
 
doubleLagrange_onceintN
double*x
double*y
doublea
intl )
This function implements the Lagrange class for one off calculations, thereby avoid the need to instantiate the Lagrange class yourself.
Example 2:
The following graph is constructed from interpolating the following values:
x = 1  y = 0.22
x = 2  y = 0.04
x = 3  y = -0.13
x = 4  y = -0.17
x = 5  y = -0.04
x = 6  y = 0.09
x = 7  y = 0.11
\graph  N=7, x="1 2 3 4 5 6 7", y="0.22 0.04 -0.13 -0.17 -0.04 0.09 0.11", a=1:5, l=2
Parameters:
NThe number of initial points
xThe x-coordinates for the initial points (evenly spaced!)
yThe y-coordinates for the initial points
aThe x-coordinate for the output point
lThe level of interpolation (2 means quadratic)
Returns:
the interpolated y-coordinate that corresponds to a.
Source Code:

To view or download source code you need to buy a Commercial licence.

buy now     add cart

Not a member, then Register with CodeCogs. Already a Member, then Login.


Page Comments

Format Excel Equations

  You must login to leave a messge


Last Modified: 13 Feb 08 @ 23:24     Page Rendered: 2010-03-14 10:58:30

Valid CSS!   Valid XHTML 1.0 Transitional