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

📄 zxec.cpp

📁 一个最小二乘法程序
💻 CPP
字号:
#include<iostream.h>
#define N 7
int main()
{
	int x[N] = {1,3,4,5,6,7,8};
	int y[N] = {10,5,4,2,1,1,2};
	int a[N][3] = {0};
	int at[3][N] = {0};
	int A[3][3] = {0};
	float A1[3][3] = {0};
	float A_1[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
	int b[3] = {0};
	float answ[3] = {0};

	

	for(int i=0; i<N;i++)  //solute the a[N][3]
	{
		a[i][0] = x[i]*x[i];
		a[i][1] = x[i];
		a[i][2] = 1;
	}
   
	for(i=0;i<N;i++)     //reverse the a[N][3] to at[3][N]
	{
		at[0][i] = a[i][0];
		at[1][i] = a[i][1];
		at[2][i] = a[i][2];
	}


	for(i=0;i<3;i++)   //solute the A[3][3]
	{
		for(int j=0;j<3;j++)
		{
			int aij = 0;
			for(int k=0;k<N;k++)
			{
				aij += at[i][k]*a[k][j];
			}
			A[i][j] = aij;

		}
	}

 
	for(i=0;i<3;i++)        //solute the b[3]
	{
		int bi = 0;
		for(int k=0;k<N;k++)
		{
			bi += at[i][k]*y[k];
		}
		b[i] = bi;

	}

    cout<<"the matrix A[3][3] is:"<<endl;
	for(i=0;i<3;i++)           //test and display the A[3][3]
	{
		for(int j=0;j<3;j++)
		{
			cout<<A[i][j]<<"  ";
		}
		cout<<endl;
	}
    cout<<"the matix b[3] is:"<<endl;
	for(i=0;i<3;i++)               //test and display the b[3]
	{
		cout<<b[i]<<endl;
	}
	for(i=0;i<3;i++)      //copy A[3][3] to A1[3][3] to avoid A[3][3] changed
	{
		for(int j=0;j<3;j++)
		{
			A1[i][j] = (float)(A[i][j]);
			
		}
	}

           
	float s1;           //change the A1[3][3] and the A_1[3][3] to get the final A_1[3][3]
	s1 = A1[0][0];
	for(i=0;i<3;i++)     
	{
		
        A1[0][i] = A1[0][i]/s1;
		A_1[0][i] = A_1[0][i]/s1;
		
	}
	float s2,s3;
	s2 = A1[1][0];
	s3 = A1[2][0];
    for(i=0;i<3;i++)
	{
	   A1[1][i] = A1[1][i] - A1[0][i]*s2;
	   A1[2][i] = A1[2][i] - A1[0][i]*s3;
	   A_1[1][i] = A_1[1][i] - A_1[0][i]*s2;
	   A_1[2][i] = A_1[2][i] - A_1[0][i]*s3;
    }


   s1 = A1[1][1];
   for(i=0;i<3;i++)
   {
	   
	   A1[1][i] = A1[1][i]/s1;
	   A_1[1][i] = A_1[1][i]/s1;
   }
   s2 = A1[0][1];
   s3 = A1[2][1];
   for(i=0;i<3;i++)
   {
	   A1[0][i] = A1[0][i] - A1[1][i]*s2;
	   A1[2][i] = A1[2][i] - A1[1][i]*s3;
	   A_1[0][i] = A_1[0][i] - A_1[1][i]*s2;
	   A_1[2][i] = A_1[2][i] - A_1[1][i]*s3;
   }


   s1 = A1[2][2];
   for(i=0;i<3;i++)
   {
	   
	   A1[2][i] = A1[2][i]/s1;
	   A_1[2][i] = A_1[2][i]/s1;
   }
   s2 = A1[0][2];
   s3 = A1[1][2];
   for(i=0;i<3;i++)
   {
	   A1[0][i] = A1[0][i] - A1[2][i]*s2;
	   A1[1][i] = A1[1][i] - A1[2][i]*s3;
	   A_1[0][i] = A_1[0][i] - A_1[2][i]*s2;
	   A_1[1][i] = A_1[1][i] - A_1[2][i]*s3;
   }
   cout<<"the final matrix A1[3][3] and matrix A_1[3][3] is"<<endl;
   for (i=0;i<3;i++)      //test and display the final A1[i][j] ,A_1[i][j]
   {
	   for(int j=0;j<3;j++)
	   {
		   cout<<A1[i][j]<<"  ";
	   }
	   cout<<endl;
   }
   for (i=0;i<3;i++)
   {
	   for(int j=0;j<3;j++)
	   {
		   cout<<A_1[i][j]<<"  ";
	   }
	   cout<<endl;
   }

   for(i=0;i<3;i++)    //solute the answ[i] the final answer of the zxec problem
   {
	   answ[i] = A_1[i][0]*b[0] + A_1[i][1]*b[1] + A_1[i][2]*b[2];

   }
   
   cout<<"the answer is:"<<endl;
   for(i=0;i<3;i++)  //display the answ[i]
   {
	   cout<<"c"<<i<<"="<<answ[i]<<endl;
   }

   return 0;
}

⌨️ 快捷键说明

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