lagrange, aitken, neville - interpolation

1
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); }

Upload: andreibanc

Post on 27-Apr-2015

828 views

Category:

Documents


4 download

DESCRIPTION

some well known interpolation algorithms, written in C/C++ (numerical methods, numerical analysis)

TRANSCRIPT

Page 1: Lagrange, Aitken, Neville - Interpolation

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);

}