相关的理论请参考相关的数值算法的书籍,我这里只给出关键的函数及主程序段,其余相关的细节就不再一一罗列了.
Hermite插值法结合了函数的导数值,使得插值的精度更为提高:
void hermite3(Type* xList,Type* yList,Type* yPList,Type x,FILE* outputFile)
{
Type h;/*The tween value*/
Type hAns;/*The return answer*/
assertF(xList!=NULL,"in Hermite Insert xList passed in is null\n");
assertF(yList!=NULL,"in Hermite Insert yList passed in is null\n");
assertF(yPList!=NULL,"in Hermite Insert yPList passed in is null\n");
assertF(outputFile!=NULL,"in jieXianMethod outputFile is NULL");
/*Init data*/
hAns=0;
/*Core Program*/
h=xList[1]-xList[0];
hAns+=(x-xList[1])*(x-xList[1])*(h+2*(x-xList[0]))*yList[0]/(h*h*h);
hAns+=(x-xList[0])*(x-xList[0])*(h+2*(xList[1]-x))*yList[1]/(h*h*h);
hAns+=(x-xList[1])*(x-xList[1])*(x-xList[0])*yPList[0]/(h*h);
hAns+=(x-xList[0])*(x-xList[0])*(x-xList[1])*yPList[1]/(h*h);
/*End of Core Program*/
/*Output Answer*/
printf("the answer of hermit 3 poly is:%f\n",hAns);
fprintf(outputFile,"the answer of hermit 3 poly is:%f\r\n",hAns);
}
#include "MyAssert.h"
#include "InsertAlgorithm.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *inFileName="inputData.txt";
/*
input data specification
len,
x0,x1,
y0,y1,
y0',y1',
x.
*/
char *outFileName="outputData.txt";
#define DEBUG 1
void main(int argc,char* argv[])
{
FILE *inputFile;/*input file*/
FILE *outputFile;/*output file*/
double startTime,endTime,tweenTime;/*time callopsed info*/
Type* xList,* yList,* yPList;
Type x;
int len;
/*input file open*/
if(argc>1)strcpy(inFileName,argv[1]);
assertF((inputFile=fopen(inFileName,"rb"))!=NULL,"input file error");
printf("input file open success\n");
/*outpout file open*/
if(argc>2)strcpy(outFileName,argv[2]);
assertF((outputFile=fopen(outFileName,"wb"))!=NULL,"output file error");
printf("output file open success\n");
fscanf(inputFile,"%d,",&len);
/*Memory apply*/
xList=(Type*)malloc(sizeof(Type)*len);
yList=(Type*)malloc(sizeof(Type)*len);
yPList=(Type*)malloc(sizeof(Type)*len);
/*Read info data*/
fscanf(inputFile,"%f,%f,",&xList[0],&xList[1]);
fscanf(inputFile,"%f,%f,",&yList[0],&yList[1]);
fscanf(inputFile,"%f,%f,",&yPList[0],&yPList[1]);
fscanf(inputFile,"%f,",&x);
printf("read in data info:%f,%f,%f,%f,%f,%f,%f\n",xList[0],xList[1],yList[0],yList[1],yPList[0],yPList[1],x);
#if DEBUG
printf("\n*******start of test program******\n");
printf("now is runnig,please wait...\n");
startTime=(double)clock()/(double)CLOCKS_PER_SEC;
/******************Core program code*************/
hermite3(xList,yList,yPList,x,outputFile);
/******************End of Core program**********/
endTime=(double)clock()/(double)CLOCKS_PER_SEC;
tweenTime=endTime-startTime;/*Get the time collapsed*/
/*Time collapsed output*/
printf("the collapsed time in this algorithm implement is:%f\n",tweenTime);
fprintf(outputFile,"the collapsed time in this algorithm implement is:%f\r\n",tweenTime);
printf("\n*******end of test program******\n");
#endif
free(yPList);
free(yList);
free(xList);
printf("program end successfully,\n you have to preess any key to clean the buffer area to output,otherwise,you wiil not get the total answer.\n");
getchar();/*Screen Delay Control*/
return;
}