#define PI 3.14159265
float xarr[6]={0.15,2.3,3.15,4.85,6.25,7.95};
float yarr[6]={4.79867,4.49013,4.2243,3.47313,2.66674,1.51909};
float want_x[14]={0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7};
float want_y[14]={0,};
float d[6][6]={0,};
float funtion(float x);
double CSpline(int n, double data);
int main(void)
{
for (int i = 0; i < 14; i++)
{
printf("x = %f y = %f f(x) = %f f(x)-y = %f \n",want_x[i],CSpline(6,want_x[i]),funtion(want_x[i]),funtion(want_x[i])-CSpline(6,want_x[i]));
}
return 0;
}
double CSpline(int n, double data)
{
int k;
double e[100],f[100],g[100],r[100],w[100];
double A[100], B[100], C[100], D[100];
double intPol;
//form the tridiagonal system
e[0]=0.;
e[1]=0.;
f[0]=0.;
g[0]=0.;
r[0]=0.;
g[n-2]=0.;
for (k = 1; k <= n-2; k++)
{
e[k+1] = xarr[k+1] - xarr[k];
f[k] = 2.*(xarr[k+1] - xarr[k-1]);
g[k] = xarr[k+1] - xarr[k];
r[k] = 6.*((yarr[k+1] - yarr[k]) / (xarr[k+1] - xarr[k])
+ (yarr[k-1] - yarr[k]) / (xarr[k] - xarr[k-1]));
}
w[0] = 0.;
w[n-1] = 0.;
for (k = 2; k <= n-2; k++)
{
e[k] = e[k] / f[k-1];
f[k] = f[k] - e[k]*g[k-1];
}
for (k = 2; k <= n-2; k++)
{
r[k] = r[k] - e[k]*r[k-1];
}
w[n-2] = r[n-2] / f[n-2];
for (k = n-3; k >= 1; k--)
{
w[k] = (r[k] - g[k]*w[k+1]) / f[k];
}
for (k = 1; k <= n-1; k++)
{
A[k] = w[k-1] / (6.*(xarr[k] - xarr[k-1]));
B[k] = w[k] / (6.*(xarr[k] - xarr[k-1]));
C[k] = yarr[k-1] / (xarr[k] - xarr[k-1])
- w[k-1] / 6.*(xarr[k] - xarr[k-1]);
D[k] = yarr[k] / (xarr[k] - xarr[k-1])
- w[k] / 6.*(xarr[k] - xarr[k-1]);
}
//보¬¢¬간Æ¡Ì값ƨ£ 계Æe산íe
for (k = 1; k <= n-1; k++)
{
if(data >= xarr[k-1] && data <= xarr[k])
{
intPol = A[k] * pow(xarr[k] - data, 3)
+ B[k] * pow(data - xarr[k-1], 3)
+ C[k] * (xarr[k] - data)
+ D[k] * (data - xarr[k-1]);
}
}
return(intPol);
}
float funtion(float x)
{
return 4.8*cos(PI/20*x);
}