neville插值.txt

来自「数值分析程序」· 文本 代码 · 共 56 行

TXT
56
字号
#include <iostream.h>

void Neville(int n,double *x,double *y)
{
	int i,j;
	double *matrix;
	double _x;

	matrix=new double[n*n];
	for(i=0;i<n;i++)
		matrix[i*n]=y[i];

	cout<<"\nPlease input the inserted x:"<<endl;
	cin>>_x;

	for(i=1;i<n;i++)
		for(j=1;j<n&&i>=j;j++)
			matrix[i*n+j]=((_x-x[i])*matrix[(i-1)*n+j-1]-(_x-x[i-j])*matrix[i*n+j-1])/(x[i-j]-x[i]);
	
	cout<<"\nThe matrix is:"<<endl;
	for(i=0;i<n;i++)
		for(j=0;j<n&&i>=j;j++)
		{
			cout<<matrix[i*n+j]<<' '; 
			if(j==i) cout<<endl;
		}

	cout<<"\nThe result is: "<<matrix[(n-1)*n+n-1]<<endl;

	delete[] matrix;
}

void main()
{
	int i,n;
	double *x,*y;
	
	cout<<"Please input the sum of datas:"<<endl;
	cin>>n;

	x=new double[n];
	y=new double[n];

	cout<<"\nPlease input the Array x:"<<endl;
	for(i=0;i<n;i++)
		cin>>x[i];

	cout<<"\nPlease input the Array y:"<<endl;
	for(i=0;i<n;i++)
		cin>>y[i];
	
	Neville(n,x,y);

	delete[] x;
	delete[] y;
}

⌨️ 快捷键说明

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