0% found this document useful (0 votes)
30 views6 pages

Lagranges Interpolation

Uploaded by

SJV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

Lagranges Interpolation

Uploaded by

SJV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Here's a simple program in Turbo C++ to perform Lagrange's Interpolation.

This method is used to


find the polynomial that goes through a given set of points and to estimate the value of the
polynomial at a given point.

```cpp

#include <iostream>

using namespace std;

double lagrangeInterpolation(double x[], double y[], int n, double xp) {

double yp = 0;

for (int i = 0; i < n; i++) {

double li = 1;

for (int j = 0; j < n; j++) {

if (i != j) {

li *= (xp - x[j]) / (x[i] - x[j]);

yp += li * y[i];

return yp;

int main() {

int n;

cout << "Enter the number of data points: ";

cin >> n;

double x[n], y[n];


cout << "Enter the data points (x and y):" << endl;

for (int i = 0; i < n; i++) {

cout << "x[" << i << "]: ";

cin >> x[i];

cout << "y[" << i << "]: ";

cin >> y[i];

double xp;

cout << "Enter the point at which to interpolate: ";

cin >> xp;

double yp = lagrangeInterpolation(x, y, n, xp);

cout << "The interpolated value at x = " << xp << " is y = " << yp << endl;

return 0;

```

### Explanation:

1. **Function Definition (`lagrangeInterpolation`)**:

- `x[]` and `y[]` are arrays containing the data points.

- `n` is the number of data points.

- `xp` is the point at which to interpolate.

- The function calculates the interpolated value `yp` at `xp` using Lagrange's formula.

2. **Main Function**:

- Prompts the user to input the number of data points.

- Reads the data points into arrays `x` and `y`.

- Prompts the user to input the point `xp` at which to interpolate.

- Calls the `lagrangeInterpolation` function to compute the interpolated value and prints the result.
This program will perform Lagrange's Interpolation for a given set of data points and estimate the
value of the polynomial at a specified point.
Here is the program for Lagrange's Interpolation, rewritten for Turbo C++ without using namespaces:

```cpp

#include <iostream.h>

#include <conio.h>

double lagrangeInterpolation(double x[], double y[], int n, double xp) {

double yp = 0;

for (int i = 0; i < n; i++) {

double li = 1;

for (int j = 0; j < n; j++) {

if (i != j) {

li *= (xp - x[j]) / (x[i] - x[j]);

yp += li * y[i];

return yp;

int main() {

int n;

cout << "Enter the number of data points: ";

cin >> n;

double *x = new double[n];

double *y = new double[n];


cout << "Enter the data points (x and y):" << endl;

for (int i = 0; i < n; i++) {

cout << "x[" << i << "]: ";

cin >> x[i];

cout << "y[" << i << "]: ";

cin >> y[i];

double xp;

cout << "Enter the point at which to interpolate: ";

cin >> xp;

double yp = lagrangeInterpolation(x, y, n, xp);

cout << "The interpolated value at x = " << xp << " is y = " << yp << endl;

delete[] x;

delete[] y;

getch(); // To keep the console window open

return 0;

```

### Explanation:

1. **Function Definition (`lagrangeInterpolation`)**:

- `lagrangeInterpolation(double x[], double y[], int n, double xp)`: Computes the interpolated value
at `xp` using the Lagrange Interpolation method.

- `x[]` and `y[]` are arrays of data points, `n` is the number of data points, and `xp` is the point at
which to interpolate.

- Calculates the Lagrange basis polynomial for each data point and sums them to get the
interpolated value.
2. **Main Function**:

- Prompts the user to input the number of data points.

- Dynamically allocates arrays `x` and `y` to store the data points.

- Prompts the user to input the data points.

- Prompts the user to input the point at which to interpolate.

- Calls `lagrangeInterpolation` to compute the interpolated value and prints the result.

- Frees the dynamically allocated memory for `x` and `y`.

This program is compatible with Turbo C++ and performs Lagrange Interpolation. You can modify the
data points and the function to suit different interpolation problems.

You might also like