#include<stdio.h>
#include<math.h>
#define true 1
#define false 0
double F(double);
void INPUT(int *, double *, double [][26], int *);
void OUTPUT(FILE **);
void OUTPUT2(FILE **, int, int, double *, double [][26]);
main()
{
double Q[26][26],X[13],Z[26];
double XX,S;
int I,J,K,N,OK;
char A;
FILE *OUP[1];
INPUT(&OK, X, Q, &N);
if (OK) {
/* STEP 1 */
for (I=0; I<=N; I++) {
/* STEP 2 */
Z[2*I] = X[I];
Z[2*I+1] = X[I];
Q[2*I+1][0] = Q[2*I][0];
/* STEP 3 */
if (I != 0)
Q[2*I][1] = (Q[2*I][0] - Q[2*I-1][0]) / (Z[2*I] -Z[2*I-1]);
}
/* STEP 4 */
K = 2 * N + 1;
for (I=2; I<=K; I++)
for (J=2; J<=I; J++)
Q[I][J] = ( Q[I][J - 1] - Q[I - 1][J - 1] ) / ( Z[I] - Z[I - J] );
/* STEP 5 */
OUTPUT(OUP);
OUTPUT2(OUP, K, N, X, Q);
printf("Do you wish to evaluate this polynomial?\n");
printf("Enter Y or N\n");
scanf("\n%c", &A);
if ((A == 'Y') || (A == 'y')) {
printf("Enter a point at which to evaluate\n");
scanf("%lf", &XX);
S = Q[K][K] * (XX - Z[K-1]);
for (I=2; I<=K; I++) {
J = K - I + 1;
S = (S + Q[J][J]) * (XX - Z[J-1]);
}
S = S + Q[0][0];
fprintf(*OUP, "x-value and interpolated-value\n");
fprintf(*OUP, " %12.10e %12.10e\n", XX, S);
}
}
fclose(*OUP);
return 0;
}
/* Change functions F and FP if program is to calculate the
first column and part of the second column of Q */
double F(double X)
{
double f;
f = 1.0/X;
return f;
}
/* Note: Function FP is the derivative of F */
double FP(double X)
{
double f;
f = -1.0/(X*X);
return f;
}
printf("Has a text file been created with the data in three ");
printf("columns?\n");
printf("Enter Y or N\n");
scanf("\n%c", &A);
if ((A == 'Y') || (A == 'y')) {
printf("Input the file name in the form - ");
printf("drive:name.ext\n");
printf("for example: A:DATA.DTA\n");
scanf("%s", NAME);
INP = fopen(NAME, "r");
*OK = false;
while (!(*OK)) {
printf("Input the number of data points minus 1\n");
scanf("%d", N);
if (*N > 0) {
for (I=0; I<=*N; I++)
fscanf(INP, "%lf %lf %lf", &X[I], &Q[2*I][0],
&Q[2*I+1][1]);
fclose(INP);
*OK = true;
}
else printf("Number must be a positive integer\n");
}
}
else {
printf("Please create the input file in three column ");
printf("form with the X values, F(X), and\n");
printf("F'(X) values in the corresponding columns.\n");
printf("The program will end so the input file can ");
printf("be created.\n");
*OK = false;
}
}