lagrange, aitken, neville - interpolation
DESCRIPTION
some well known interpolation algorithms, written in C/C++ (numerical methods, numerical analysis)TRANSCRIPT
![Page 1: Lagrange, Aitken, Neville - Interpolation](https://reader036.vdocuments.net/reader036/viewer/2022081717/553dbe4e4a7959c6368b4771/html5/thumbnails/1.jpg)
void lagrange_interpolation(double x[], double y[], int nr, double where) {
cout<<"Intermediate steps: ";
double sum = 0;
for (int i=0; i<nr; i++) {
double product = y[i];
for (int j=0; j<nr; j++) {
if (i!=j) product *= ((where - x[j])/(x[i] - x[j]));
}
sum += product;
cout.precision(10);
cout<<sum<<", ";
}
cout<<"\b\b;\n";
cout.precision(10);
cout<<"Aproximation: "<<sum<<endl;
}
void aitken_interpolation(double x[], double y[], int nr, double where) {
// i -> columns, j -> rows
double **matrix = matrix_alloc(nr, nr);
// create the table
for (int j=0; j<nr; j++) matrix[0][j] = y[j];
for (int i=1; i<nr; i++) {
for (int j=i; j<nr; j++) {
matrix[i][j] = (matrix[i-1][i-1] * (x[j] - where) -
matrix[i-1][j] * (x[i-1] - where)) /
(x[j] - x[i-1]);
}
}
cout<<"Here's Aitken's table:\n";
matrix_print(matrix, nr, nr);
cout<<"Aproximation: "<<matrix[nr-1][nr-1]<<endl;
matrix_delete(matrix, nr, nr);
}
void neville_interpolation(double x[], double y[], int nr, double where) {
// i -> columns, j -> rows
double** matrix = matrix_alloc(nr, nr);
// create the table
for (int j=0; j<nr; j++) matrix[0][j] = y[j];
for (int i=1; i<nr; i++) {
for (int j=i; j<nr; j++) {
double denominator = x[j] - x[j-i];
double determinant = matrix[i-1][j-1] * (x[j] - where) -
matrix[i-1][j] * (x[j-i] - where);
matrix[i][j] = determinant/denominator;
}
}
cout<<"Here's Neville's table:\n";
matrix_print(matrix, nr, nr);
cout<<"Aproximation: "<<matrix[nr-1][nr-1]<<endl;
matrix_delete(matrix, nr, nr);
}