Least square method for overdetermined equations
- Today, I did a calculation method assignment. I didn't find any similar reference materials on the Internet. Many students call library functions directly in Python to write conveniently, but I think it's a greater exercise for beginners to write in c or c + +;
- The code has been debugged and can run smoothly. If you need help, you can take it for reference. If you don't know how to use it, you can trust me;
//Least square method for overdetermined equations
#include<iostream>
#define SIZE 20 //The maximum size of the predefined matrix to be transposed is 20 * 20
using namespace std;
//The input array of the following functions and the array defined in the main function are all of the same name, but they do not refer to the same function
//Input function of matrix
void inputMatrix(int a[][SIZE] , int n, int m){ //The subscript of two-dimensional array parameter must be determined
int i,j;
for(i = 0;i < n;i++){
for(j = 0;j < m;j++){
cin>>a[i][j];
}
}
}
//Output function of matrix
void outputMatrix(int b[][SIZE] , int n, int m){
int i,j;
for(i = 0;i < n;i++){
for(j = 0;j < m;j++){
cout<<b[i][j]<<" ";
}
cout<<"\n"; //Wrap one line at a time
}
}
//Transpose operation of matrix
void matrixTransport(int a[][SIZE], int b[][SIZE], int row, int column){//row and column of a
int i,j;
//int row, column;
for(i = 0; i<column; i++){
for(j = 0; j<row; j++){
b[i][j] = a[j][i];
}
}
}
/********************************************
A:Matrix A; B: matrix B; C: multiplication result matrix; rowA: row number of a; columnB: column number of B; columnA: column number of a
********************************************/
//It is necessary to ensure that two arrays can be multiplied. There is no judgment condition added here. It can be done by default. I hope the reader can understand this.
void matrixMul(int A[][SIZE], int B[][SIZE], int C[][SIZE], int rowA, int columnB, int columnA){
for (int i=0;i<rowA;i++){
for (int j=0; j<columnB;j++){
C[i][j] = 0;
for (int k=0;k<columnA;k++){
C[i][j]+=A[i][k]*B[k][j];
}
}
}
}
//The Gauss elimination method can be used to find the solution of the system of equations. The reader can also use other methods to find the solution of the system of equations.
void Gauss(int a[][SIZE], int b[][SIZE], int rowA, int colA){
float A[50][50];//Matrix A for storing elements
float B[50];//Element B for storing results
int row=rowA;
int col=colA;
//Input matrix A
int i, j, k;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
A[i][j]=a[i][j];
}
cout<<endl;
}
//Input matrix B
for(i=0; i<row;i++)
{
B[i]=b[i][0];
}
cout<<endl;
//It is impossible to use the Gauss elimination method if there are 0 elements on the main diagonal of the matrix
for(i=0;i<row;i++)
{
if(A[i][i]==0)
{
cout<<"This matrix cannot use Gauss elimination method!"<<endl;
break;
}
}
float C[row];//Store the coefficient of primary row transformation for row subtraction
//The whole process algorithm of elimination
for(k=0; k<row-1;k++)
{
//Find out the coefficient of the K-th elementary line transformation
for(i=k+1;i<row;i++)
{
C[i]=A[i][k]/A[k][k];
}
//K-th elimination calculation
for(i=k+1;i<row;i++)
{
for(j=0;j<row;j++)
{
A[i][j]=A[i][j]-C[i]*A[k][j];
}
B[i]=B[i]-C[i]*B[k];
}
}
//Using X to store solution
float X[row];
X[row-1]=B[row-1]/A[row-1][row-1];
//Find out the solution of every unknown number
for(i=row-2;i>=0;i--)
{
double Sum=0;
for(j=i+1;j<row;j++)
{
Sum+=A[i][j]*X[j];
}
X[i]=(B[i]-Sum)/A[i][i];
}
cout<<"The results are as follows:"<<endl<<endl;
for(i=0;i<row;i++)
{
cout<<"X"<<i+1<<"="<<X[i]<<endl;
}
}
//Main function
int main(){
//Define array and initialize
int a[SIZE][SIZE]={0};//To store the coefficient matrix to the right of the equation
int b[SIZE][SIZE]={0};//Used to store the matrix after a transpose
int c[SIZE][SIZE]={0};//Used to store the results of the transpose before and after multiplication
int d[SIZE][SIZE]={0}; //Used to store the results in the right side of the equal sign
int e[SIZE][SIZE]={0}; //Used to store results in AT*b
int f[SIZE][SIZE]={0}; //Augmented matrix for storing final equations
int row,column;
//Determination of the number of matrix rows and columns[
cout<<"Please enter the number of rows of the matrix you want to transpose : row = ";
cin>>row;
cout<<"\n Please enter the number of columns of the matrix you want to transpose : column = ";
cin>>column;
//Function call and main function realization
printf("Please enter a %d X %d Matrix of A \n" , row , column);
inputMatrix(a, row , column);
outputMatrix(a, row , column);
matrixTransport(a, b, row, column);
printf("Transposed matrix AT yes :\n");
outputMatrix(b, column, row);//After transpose, the number of rows of b = the number of columns of a, and the number of columns of b equals the number of rows of A
matrixMul(b, a, c, column, column, row); //The first column is the number of rows of b, the second column is the number of columns of a, and the number of columns of rowWieb
printf("The result of matrix multiplication AT*A:\n");
outputMatrix(c, column, column);
printf("Please enter a matrix to the right of the equation d, \n"); //Row row row 1 column matrix
inputMatrix(d, row , 1);
matrixMul(b, d, e, column, 1, row);
printf("The result of matrix multiplication AT*b:\n");
outputMatrix(e, row, 1);
//Combine the c and e matrices in f to get the augmented matrix of the final system of equations
int i, j;
for(i=0; i<column; i++){
for(j=0; j<column; j++){
f[i][j]=c[i][j];
}
}
for(i=0; i<column; i++){
f[i][column]=e[i][0];
}
cout<<"Output the final equations:\n";
outputMatrix(f, column, column+1);
//Solutions of output final equations
Gauss(c, e, column, column);
getchar();
return 0;
}