牛顿插值法 C语言版算法

#include "stdio.h"
#include "malloc.h"
#include "fstream"
#include<conio.h>
void main()
{
FILE *fp1;int i,j,k,n;
double X[20]={0,1,2,4},Y[20]={1,9,23,3};
double OneResult=0,sum=0;
double newx=6;
double temp_a=1,temp_b=0,temp_c=0,temp_d=1,x,y;
	
	if((fp1=fopen("result.txt","wt"))==NULL)
		{
			printf("can't open file\n");
			getch();
			exit(1);
		}		
//input module
	printf("您要输入几组数据?请输入组数:");
	scanf("%d",&n);
	if(n>20)
	{
		printf("为了保证精度,请输入20组以内的数据。");
		scanf("%d",&n);
	}
	for(i=0;i<n;i++)
	{
		printf("输入第%d组数据:\n",i+1);
		printf("X[%d]=",i);
		scanf("%lf",&X[i]);
		printf("Y[%d]=",i);
		scanf("%lf",&Y[i]);
	}
	printf("要插入的x值:");
	scanf("%lf",&newx);

//computing module
	printf("\n开始计算...\n");	
for(i=0;i<n;i++) //显示输入的数据
	{
		printf("X[%d]=%lf  Y[%d]=%lf\n",i,X[i],i,Y[i]);
		fprintf(fp1,"X[%d]=%lf  Y[%d]=%lf\n",i,X[i],i,Y[i]);
	}

for(k=0;k<n;k++)
{
	for(i=0;i<=k;i++)  //求差商 函数值的线性组合
	{
		temp_a=1;		
		x=X[i];
		y=Y[i];
		for(j=0;j<=k;j++)
		{
			if(i!=j)
			{
				temp_a *=(X[i]-X[j]);
			}
		}
		temp_b=y/temp_a;
		temp_c +=temp_b;  
		if(i>0)
		{
			temp_d*=(newx-X[i-1]);	/*temp_d表示累和 x-x[i]*/
		}
	}
	//printf("temp_d[%d]:%lf     ",k,temp_d);  
	printf("        求得%d阶差商:%lf\n",k,temp_c);	/*求得k阶差商*/
	fprintf(fp1,"        求得%d阶差商:%lf\n",k,temp_c);
	OneResult=temp_c*temp_d;	/*OneResult为插值多项式的第k个分量*/
	printf("插值多项式第%d个分量:%lf\n",k,OneResult);
	fprintf(fp1,"插值多项式第%d个分量:%lf\n",k,OneResult);

	sum+=OneResult;		/*sum为最终结果*/
	temp_b=0;	
	temp_c=0;
	temp_d=1;
}
printf("公式计算结果:%lf\n",sum);
fprintf(fp1,"公式计算结果:%lf\n",sum);
}

程序运行结果:



KEY Point :


  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include #include #include typedef strUCt data { float x; float y; }Data;//变量x和函数值y的结构 Data d[20];//最多二十组数据 float f(int s,int t)//牛顿插值法,用以返回插商 { if(t==s+1) return (d[t].y-d[s].y)/(d[t].x-d[s].x); else return (f(s+1,t)-f(s,t-1))/(d[t].x-d[s].x); } float Newton(float x,int count) { int n; while(1) { cout<>n; if(n<=count-1)// 插值次数不得大于count-1次 break; else system("cls"); } //初始化t,y,yt。 float t=1.0; float y=d[0].y; float yt=0.0; //计算y值 for(int j=1;j<=n;j++) { t=(x-d[j-1].x)*t; yt=f(0,j)*t; //cout<<f(0,j)<<endl; y=y+yt; } return y; } float lagrange(float x,int count) { float y=0.0; for(int k=0;k<count;k++)//这儿默认为count-1次插值 { float p=1.0;//初始化p for(int j=0;j<count;j++) {//计算p的值 if(k==j)continue;//判定是否为同一个数 p=p*(x-d[j].x)/(d[k].x-d[j].x); } y=y+p*d[k].y;//求和 } return y;//返回y的值 } void main() { float x,y; int count; while(1) { cout<>count; if(count<=20) break;//检查输入的是否合法 system("cls"); } //获得各组数据 for(int i=0;i<count;i++) { cout<<"请输入第"<<i+1<>d[i].x; cout<<"请输入第"<<i+1<>d[i].y; system("cls"); } cout<>x; while(1) { int choice=3; cout<<"请您选择使用哪种插值法计算:"<<endl; cout<<" (0):退出"<<endl; cout<<" (1):Lagrange"<<endl; cout<<" (2):Newton"<<endl; cout<>choice;//取得用户的选择项 if(choice==2) { cout<<"你选择了牛顿插值计算方法,其结果为:"; y=Newton(x,count);break;//调用相应的处理函数 } if(choice==1) { cout<<"你选择了拉格朗日插值计算方法,其结果为:"; y=lagrange(x,count);break;//调用相应的处理函数 } if(choice==0) break; system("cls"); cout<<"输入错误!!!!"<<endl; } cout<<x<<" , "<<y<<endl;//输出最终结果 }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值