Created
June 25, 2013 13:56
-
-
Save shonenada/5858617 to your computer and use it in GitHub Desktop.
lagrange interpolation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define N 10 | |
#define MIN -5.0 | |
#define MAX 5.0 | |
#define F(x) (1 / (1 + x * x)) | |
double x_at (int i); | |
double get_lj (int j, double x); | |
double Ln (double x); | |
int main (){ | |
double x1, x2, x; | |
printf(" ui \t f(ui) \t Ln(x)\n"); | |
for (int i=0;i<N;++i){ | |
x1 = x_at(i); | |
x2 = x_at(i+1); | |
x = (x1 + x2) / 2; | |
printf("%10lf\t%10lf\t%10lf\n", x, F(x), Ln(x)); | |
} | |
system("pause"); | |
return 0; | |
} | |
double x_at (int i){ | |
double step = (MAX - MIN) / N; | |
double x = MIN + i * step; | |
return x; | |
} | |
double get_lj (int j, double x){ | |
double result = 1.0; | |
double xi; | |
double xj = x_at(j); | |
for (int i=0;i<N+1;++i){ | |
if (i == j){ | |
continue; | |
} | |
xi = x_at(i); | |
result = result * ((x - xi) / (xj - xi)); | |
} | |
return result; | |
} | |
double Ln(double x){ | |
double xj, yj, lj; | |
double result = 0; | |
for (int j=0;j<N+1;++j){ | |
xj = x_at(j); | |
yj = F(xj); | |
lj = get_lj(j, x); | |
result = result + yj * lj; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment