数值分析

#include 
#include 
#include 
#define PrecisionLevel 1.0e-12 //迭代精度
double Z[6][6];//将Z声明为全局变量,便于后面操作
double** CreateMatrix(int m,int n);
void DestroyMatrix(double**A,int m,int n);
bool GaussElimination(double **A,int n,double* b);
double InfiniteNorm(double*b,int n);
void CoefficientMatrix(double* x,int m,double* y,int n,double** z,int k1,int k2,double** C);
double P_Value(double **C,int k,double x,double y);
double** SurfaceFitting(int& k);
//-----------------------------------------------
//生成一个m*n矩阵
double** CreateMatrix(int m,int n)
{
	double** A;
	int i;
	A=new double* [m];
	for(i=0;imax)
			{
				max=fabs(A[i][j]);
				l=j;
			}
		if(l!=i)  //判断并换行
		{
			for(j=i; j=0; i--)
	{
		sum=0;
		for(j=i+1; jmax)
			max=fabs(b[i]);
	}
	return max;
}
//-----------------------------------------------
//分片二次代数插值,z=z(u,t)
double PiecewiseInterpolation(double t,double u)
{
	double pnm=0.0;
	int i,j;
	bool flag=false;  //判断t和u是否在插值区间内的标志
	for(i=0; i<6; i++)
	{
		if((u>0.4*i-0.4/2)&&(u<=0.4*i+0.4/2))
			for(j=0;j<6;j++)
			{
				if((t>0.2*j-0.2/2)&&(t<=0.2*j+0.2/2))
				{
					flag=true;
					break;
				}
			}
		if(flag)
			break;
	}
	if(!flag)
	{
		printf("u=%e,t=%e is not in the expected interval\n",u,t);
		return 0;
	}

	if(i==0)  //边界的处理
	{
		i=1;
		if(j==0)
			j=1;
		else if(j==5)
			j=4;
	}
	else if(i==5)
	{
		i=4;
		if(j==0)
			j=1;
		else if(j==5)
			j=4;
	}
	
	pnm=Z[j-1][i-1]*((u-0.4*i)*(u-0.4*(i+1))*(t-0.2*j)*(t-0.2*(j+1)))/((-0.4)*(-0.8)*(-0.2)*(-0.4))
		+Z[j][i-1]*((u-0.4*i)*(u-0.4*(i+1))*(t-0.2*(j-1))*(t-0.2*(j+1)))/((-0.4)*(-0.8)*(0.2)*(-0.2))
		+Z[j+1][i-1]*((u-0.4*i)*(u-0.4*(i+1))*(t-0.2*(j-1))*(t-0.2*j))/((-0.4)*(-0.8)*(0.2)*(0.4))
		+Z[j-1][i]*((u-0.4*(i-1))*(u-0.4*(i+1))*(t-0.2*j)*(t-0.2*(j+1)))/((-0.4)*(0.4)*(-0.2)*(-0.4))
		+Z[j][i]*((u-0.4*(i-1))*(u-0.4*(i+1))*(t-0.2*(j-1))*(t-0.2*(j+1)))/((-0.4)*(0.4)*(0.2)*(-0.2))
		+Z[j+1][i]*((u-0.4*(i-1))*(u-0.4*(i+1))*(t-0.2*(j-1))*(t-0.2*j))/((-0.4)*(0.4)*(0.2)*(0.4))
		+Z[j-1][i+1]*((u-0.4*(i-1))*(u-0.4*i)*(t-0.2*j)*(t-0.2*(j+1)))/((0.8)*(0.4)*(-0.2)*(-0.4))
		+Z[j][i+1]*((u-0.4*(i-1))*(u-0.4*i)*(t-0.2*(j-1))*(t-0.2*(j+1)))/((0.8)*(0.4)*(0.2)*(-0.2))
		+Z[j+1][i+1]*((u-0.4*(i-1))*(u-0.4*i)*(t-0.2*(j-1))*(t-0.2*j))/((0.8)*(0.4)*(0.2)*(0.4));
	return pnm;
}
//-----------------------------------------------
//求解非线性方程组,给定x和y,求解出t,u,v,w
void NonlinearEquationSolver(double x,double y,double* X)
{
	double x2[4];   //x2=[t u v w]'
	double del_x2[4];
	double** G=CreateMatrix(4,4);  //调用CreateMatrix函数,构建矩阵
	int i,j,k;
	for(i=0; i<4; i++)
	{
		del_x2[i]=0;
		x2[i]=1.0;  //给定初值
	}

	k=0;  //解非线性方程组的迭代次数
	do
	{
		k++;
		for(i=0; i<4; i++)
			x2[i] = x2[i]+del_x2[i];

		//计算Jacobi矩阵F',即G
		for(i=0; i<4; i++)
			for(j=0; j<4; j++)
				G[i][j]=1.0;
		G[0][0]=-0.5*sin(x2[0]);
		G[1][1]=0.5*cos(x2[1]);
		G[2][0]=0.5;
		G[2][2]=-sin(x2[2]);
		G[3][1]=0.5;
		G[3][3]=cos(x2[3]);
		
		//计算F,即-del_x2
		del_x2[0]=-(0.5*cos(x2[0])+x2[1]+x2[2]+x2[3]-x-2.67);
		del_x2[1]=-(x2[0]+0.5*sin(x2[1])+x2[2]+x2[3]-y-1.07);
		del_x2[2]=-(0.5*x2[0]+x2[1]+cos(x2[2])+x2[3]-x-3.74);
		del_x2[3]=-(x2[0]+0.5*x2[1]+x2[2]+sin(x2[3])-y-0.79);
		GaussElimination(G,4,del_x2);   //调用GaussElimination函数计算线性方程组F'*del_x=-F,解del_x即del_x2
	}while(fabs(InfiniteNorm(del_x2,4)/InfiniteNorm(x2,4))>PrecisionLevel);  //调用InfiniteNorm函数计算向量的无穷范数

	for(i=0;i<4;i++)
		X[i]=x2[i];
	DestroyMatrix(G,4,4);
}
//-----------------------------------------------
//求方阵的逆矩阵
bool InverseMatrix(double** A,int n)
{
	double* b;
	double** A1=CreateMatrix(n,n);
	double** A2=CreateMatrix(n,n);
	b=new double[n];
	int i,j,k;
	for(k=0; k7)
			break;
		DestroyMatrix(C,k+1,k+1);
		C=NULL;
	}
	//--------以上求解问题的第(2)问和第(3)问--------
	DestroyMatrix(F,11,21);
	return C;
}
//-----------------------------------------------
//主函数
void main()
{
	double **C;
	int i,j,k;
	double X[4];
	//初始化Z矩阵(二维数表)
	Z[0][0]=-0.5;  Z[0][1]=-0.34; Z[0][2]=0.14;  Z[0][3]=0.94;  Z[0][4]=2.06;  Z[0][5]=3.5;
	Z[1][0]=-0.42; Z[1][1]=-0.5;  Z[1][2]=-0.26; Z[1][3]=0.3;   Z[1][4]=1.18;  Z[1][5]=2.38;
	Z[2][0]=-0.18; Z[2][1]=-0.5;  Z[2][2]=-0.5;  Z[2][3]=-0.18; Z[2][4]=0.46;  Z[2][5]=1.42;
	Z[3][0]=0.22;  Z[3][1]=-0.34; Z[3][2]=-0.58; Z[3][3]=-0.5;  Z[3][4]=-0.1;  Z[3][5]=0.62;
	Z[4][0]=0.78;  Z[4][1]=-0.02; Z[4][2]=-0.5;  Z[4][3]=-0.66; Z[4][4]=-0.5;  Z[4][5]=-0.02;
	Z[5][0]=1.5;   Z[5][1]=0.46;  Z[5][2]=-0.26; Z[5][3]=-0.66; Z[5][4]=-0.74; Z[5][5]=-0.5;
	
	C=SurfaceFitting(k);   //调用SurfaceFitting函数求曲面拟合系数C_rs
	printf("\n系数矩阵C的计算结果:\n");
	for(i=0; i<=k; i++)
	{
		for(j=0;j<=k;j++)
			printf("%20.12e  ",C[i][j]);
		printf("\n");
	}
	//--------以下求解问题的第(4)问--------
	printf("\n检验p(x,y)逼近f(x,y)的效果:\n");
	printf("\n数表xi*,yj*,f(xi*,yj*),p(xi*,yj*):\n");
	for(i=1; i<=8; i++)
		for(j=1; j<=5; j++)
		{
			NonlinearEquationSolver(0.1*i,0.5+0.2*j,X);
			printf("%f %f %20.12e %20.12e\n",0.1*i,0.5+0.2*j,PiecewiseInterpolation(X[0],X[1]),P_Value(C,k,0.1*i,0.5+0.2*j));
		}
	//--------以上求解问题的第(4)问--------
	DestroyMatrix(C,k+1,k+1);

	system("pause");
}
//-----------------------------------------------

你可能感兴趣的:(数值分析)