Created
June 25, 2013 13:57
-
-
Save shonenada/5858623 to your computer and use it in GitHub Desktop.
piecewise 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.0 / (1.0 + x * x)) | |
double x_at(int i); | |
double get_L(int k, double x); | |
double Lh(int k, double x); | |
int main(){ | |
double x1, x2, x, y; | |
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; | |
y = F(x); | |
printf("%10lf\t%10lf\t%10lf\n", x, F(x), Lh(i, x)); | |
} | |
system("pause"); | |
return 0; | |
} | |
double x_at(int i){ | |
double step = (MAX - MIN) / N; | |
double x = MIN + i * step; | |
return x; | |
} | |
double get_L(int k, double x){ | |
double xk, xk_plus_one, yk, yk_plus_one, L_value; | |
xk = x_at(k); | |
xk_plus_one = x_at(k + 1); | |
yk = F(xk); | |
yk_plus_one = F(xk_plus_one); | |
L_value = (yk * (x - xk_plus_one) / (xk - xk_plus_one) + | |
yk_plus_one * (x - xk) / (xk_plus_one - xk)); | |
return L_value; | |
} | |
double Lh(int k, double x){ | |
if (x < x_at(k)){ | |
return get_L(k, 0); | |
} | |
else if(x > x_at(N)){ | |
return get_L(k, N); | |
} | |
else{ | |
return get_L(k, x); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment