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

📄 3107002005_5th_newton.cpp

📁 牛顿插值方法
💻 CPP
字号:
// 3107002005_5th_NEWTON.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>
#include <fstream.h>
#include <math.h>
double n=5;


double y[21][21]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
double x[21]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};



void NEWTON(int n1)
{
	int j=1;
	while(j<=n1)
	{
		for(int q=j;q<=n1;q++)
		{
			y[j][q]=(y[j-1][q]-y[j-1][q-1])/(2/n*j);
		
		}
		j++;
	}
}

double res_N(double nn2)//计算1/(1+25x^2)   x=-1+nn2*2/n的值
{
	double u=0;
	for(int i=0;i<=n;i++)
	{
		double v=1;
		for(int j=0;j<i;j++)
		{
			v=v*((-1+nn2*2/100)-x[j]);
		}
		u=u+y[i][i]*v;
	}
	return u;
}

/*double error(double nn2)
{
	double u=0;
	for(double nn=0;nn<=nn2;nn++)
		if(y[0][nn]-f[
*/


int main(int argc, char* argv[])
{
	ofstream outfile1;
	double error=0;

	outfile1.open("牛顿插值输出.txt");
	
	//cout<<endl<<"函数在各个插值点的函数值为:"<<endl;

	for(int i=0;i<=n;i++)
	{
		y[0][i]=1/(1+25*(-1+i*2/n)*(-1+i*2/n));
		x[i]=-1+i*2/n;
	//	cout<<y[0][i]<<' ';
	}
	
	NEWTON(n);
//	cout<<endl<<n<<"次牛顿插值的函数为:"<<endl;
	outfile1<<endl<<n<<"次牛顿插值的函数为:"<<endl;
//	cout<<y[0][0];
	outfile1<<y[0][0];
	for(int j=1;j<=n;j++)
	{
//		cout<<"+"<<y[j][j];
		outfile1<<"+"<<y[j][j];
		for(int z=0;z<j;z++)
		{
//			cout<<"(x-("<<x[z]<<"))";
			outfile1<<"(x-("<<x[z]<<"))";
		}
	
		}

//	cout<<endl;
	outfile1<<endl;
//	cout<<"当k=1.2.3....10,检验式的结果为:"<<endl;
	outfile1<<"当k=1.2.3....10,检验式的结果为:"<<endl;
	for(double k=1;k<=10;k++)
	{
		double a,b;
		a=res_N(k);
	//	res[k]=a;
	
		b=1/(1+25*(-1+k*2/100)*(-1+k*2/100));
		if(error<fabs(a-b))
			error=fabs(a-b);

//		cout<<res_N(k)<<' ';
		outfile1<<res_N(k)<<' ';
	}
//	cout<<endl;
	outfile1<<endl;
//	cout<<"当k=1.2.3....10,原函数的结果为:"<<endl;
	outfile1<<"当k=1.2.3....10,原函数的结果为:"<<endl;
	for(k=1;k<=10;k++)
	{

//		cout<<1/(1+25*(-1+k*2/100)*(-1+k*2/100))<<' ';
		outfile1<<1/(1+25*(-1+k*2/100)*(-1+k*2/100))<<' ';
	}


//	cout<<endl;
	outfile1<<endl;

//	cout<<"当k=99.98.97....90,检验式的结果为:"<<endl;
	outfile1<<"当k=99.98.97....90,检验式的结果为:"<<endl;
	for( k=99;k>=90;k--)
	{
		double a,b;
		a=res_N(k);
	//	res[k]=a;
	
		b=1/(1+25*(-1+k*2/100)*(-1+k*2/100));
		if(error<fabs(a-b))
			error=fabs(a-b);

//		cout<<res_N(k)<<' ';
		outfile1<<res_N(k)<<' ';
	}
//	cout<<endl;
	outfile1<<endl;
//	cout<<"当k=99.98.....90,原函数的结果为:"<<endl;
	outfile1<<"当k=99.98.....90,原函数的结果为:"<<endl;
	for(k=99;k>=90;k--)
	{

//		cout<<1/(1+25*(-1+k*2/100)*(-1+k*2/100))<<' ';
		outfile1<<1/(1+25*(-1+k*2/100)*(-1+k*2/100))<<' ';
	}

//	cout<<endl;
	outfile1<<endl;
//	cout<<"牛顿插值误差为"<<error<<endl;
	outfile1<<"牛顿插值误差为"<<error<<endl;
	outf.close();




	return 0;
}

⌨️ 快捷键说明

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