⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hermite.cpp

📁 hermite插值多项式算法!感觉不错!上载给大家试用!
💻 CPP
字号:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>


float derivate(float*x,int n,int i);
float w(float variable,float*x,int n,int i);
float Hermite(float variable,float*x,float*y,float*dy,int n);
void input_n(int*pn);
void input_data(float*x,float*y,float*dy,int n);
float*my_malloc(int k);


void main()
{
	int n,flag=1;
	char c;
    float*x,*y,*dy;
	float variable;
    clrscr();
    printf("\nWelcome to use Hermite\'s interpolation polynomial to approximate the value of a\n\nfunction f(x).\n");
    input_n(&n);
    x=my_malloc(n+1);
    y=my_malloc(n+1);
	dy=my_malloc(n+1);
	input_data(x,y,dy,n);
	while(flag)
	{
		printf("\nPlease input the value of the independent variable X,then corresponding value of\nf(X) will be computed.\n\nX=");
		scanf("%f",&variable);
		Hermite(variable,x,y,dy,n);
		printf("\nWould you like to compute the value of f(X) at another independent variable? \n\nPress \"Y\" for\"YES\",any other key for\"NO\".\n");
		c=getch();
		printf("\n%c\n",c);
		if(c=='Y'||c=='y')
		{
			flag=1;
		}
		else
		{
			flag=0;
		}
	}
    printf("\nThank you for your using.\n\nPress any key to quit.\n");
    getch();	
}


float derivate(float*x,int n,int i)
{
	int j,k;
	float temp,derivative=0;
	for(j=0;j<=n;j++)
	{
		if(j!=i)
		{
			temp=1;
			for(k=0;k<=n;k++)
			{
				if(k!=i&&k!=j)
				{
					temp=temp*(x[i]-x[k]);
				}
			}
			derivative=derivative+temp;
		}
	}
	derivative=derivative/w(x[i],x,n,i);
	return derivative;
}

float w(float variable,float*x,int n,int i)
{
	float w=1;
	int j;
	for(j=0;j<=n;j++)
	{
		if(j!=i)
		{
			w=w*(variable-*(x+j));
		}
	}
	return w;
}



float Hermite(float variable,float*x,float*y,float*dy,int n)
{
	int i;
	float l_square,h1,h2,H=0;
	for(i=0;i<=n;i++)
	{
		l_square=pow(w(variable,x,n,i)/w(x[i],x,n,i),2);
		h1=l_square*(1-2*derivate(x,n,i)*(variable-x[i]));
		h2=l_square*(variable-x[i]);
		H=H+y[i]*h1+dy[i]*h2;
	}
	printf("\nf(%f)=%f\n",variable,H);
	return H;
}


void input_n(int*pn)
{
    printf("\nPlease input the number of the data you have.\"Enter\" to confirm.\n\nn=");
    scanf("%d",pn);
    while((*pn)<=0)
    {
		printf("\nSorry,your inputting is illeagle,please input again.\n\nA non-negtive integer is expected.\n\nn=");
		scanf("%d",pn);
    }
    *pn=*pn-1;
}



void input_data(float*x,float*y,float*dy,int n)
{
    int i,j,flag=1;
    printf("\nPlease input the data.Every datum should consist of an observed point(\"Xi\"),\n\ncorresponding observed value(\"Yi\") and derivative (\"Y'i\").\n\n\"Enter\" to confirm a datum.\n");
    for(i=0;i<=n;i++)
    {
        printf("\nX%d=",i+1);
        scanf("%f",x+i);
        while(i&&flag)
        {
			for(j=0;j<=i-1;j++)
			{
				if(*(x+j)==*(x+i))
				{
					printf("\nSorry,your inputting is error,please input again.\n\nThe observed points should be different from each other.\n");
					printf("\nX%d=",i+1);
					scanf("%f",x+i);
					break;
				}       	
			}
			if(j==i)
			{
				flag=0;
			}	
		}
		printf("Y%d=",i+1);
	    scanf("%f",y+i);
		printf("Y'%d=",i+1);
		scanf("%f",dy+i);
	}
	
}



float*my_malloc(int k)
{
  	float*q;
	q=(float*)malloc(4*k);
	if(!q)
  	{
   	 	printf("\nSorry,the program cannot run because Memory Request Is Rejected.\n");
    	printf("\nLots of thanks for your using.\n\nPress any key to quit.\n");
    	getch();
    	exit(-1);
  	}
	return q;
}


































⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -