Or login with:

#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
Source code is available when you buy a Commercial licence.
Not a member, then Register with CodeCogs. Already a Member, then Login.
| Lagrange( | int | n | |
| double* | x | ||
| double* | y | )[constructor] |
| n | The number of initial points |
| x | The x-coordinates for the initial points |
| y | The y-coordinates for the initial points |
| doublegetValue( | double | x | |
| int | l | ) |
| x | The abscissa of the interpolation point |
| l | The level of interpolation (2 means quadratic) |
| doubleLagrange_once( | int | N | |
| double* | x | ||
| double* | y | ||
| double | a | ||
| int | l | ) |
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

| N | The number of initial points |
| x | The x-coordinates for the initial points (evenly spaced!) |
| y | The y-coordinates for the initial points |
| a | The x-coordinate for the output point |
| l | The level of interpolation (2 means quadratic) |
Source code is available when you buy a Commercial licence.
Not a member, then Register with CodeCogs. Already a Member, then Login.
You must login to leave a messge