An example of C++ implementation for solving a multivariate first order equation

  • 2020-06-03 07:29:37
  • OfStack

An example of C++ is presented in this paper. To share for your reference, specific as follows:

Note: The isometric matrix of n*n is calculated here. The code is as follows:


#include<iostream>
#include<math.h>
#include<fstream>
#include<stdlib.h>
using namespace std;
void print(double (*pArray)[4], int iWidth,int iHigh);
void main(){
  int n,m;
  double a[3][4] = {
    {100, 10, 1, 10},
    {400, 20, 1, 20},
    {900, 30, 1, 10},
  };// The first 4 The columns are augmented matrices 
  int i,j;
  n = 3;
  cout<<" The interface number of the input system: ";
  cout<<n<<endl;
  cout<<" Input augmented matrix: "<<endl;
  for(i = 0; i < n; i++){
    for(j = 0; j < n + 1;j++){
      cout<<a[i][j]<<"  ";
    }
    cout<<endl;
  }
  for(j = 0; j < n; j++){
    double max = 0;
    double imax = 0;
    for(i = j; i < n; i++){
      if(imax < fabs(a[i][j])){
        imax = fabs(a[i][j]);
        max = a[i][j];// Gets the largest element in the column of each row 
        m = i;
      }
    }
    if(fabs(a[j][j]) != max) {
      double b = 0;
      for(int k = j;k < n + 1; k++){
        b = a[j][k];
        a[j][k] = a[m][k];
        a[m][k] = b;
      }
    }
    print(a, 3, 4);
    for(int r = j;r < n + 1;r++){
      a[j][r] = a[j][r] / max;// Divide the row's column by its first column 1 The goal is to make the first element zero 1
    }
    print(a, 3, 4);
    for(i = j + 1;i < n; i++){
      double c = a[i][j];
      if(c == 0) continue;
      for(int s = j;s < n + 1;s++){
        double tempdata = a[i][s];
        a[i][s] = a[i][s] - a[j][s] * c;// Subtract the number of rows before and after, bring down 1 On line or 1 The first element of the row is 0
        print(a, 3, 4);
      }
      print(a, 3, 4);
    }
    print(a, 3, 4);
  }
  for(i = n - 2; i >= 0; i--){
    for(j = i + 1;j < n; j++){
      double tempData = a[i][j];
      double data1 = a[i][n];
      double data2 = a[j][n];
      a[i][n] = a[i][n] - a[j][n] * a[i][j];
      print(a, 3, 4);
    }
  }
  print(a, 3, 4);
  cout<<" The solution to the system is: "<<endl;
  for(int k = 0; k < n; k++){
    cout<<"x"<<k<<" = "<<a[k][n]<<endl;
  }
}
void print(double (*pArray)[4], int iWidth,int iHigh) {
  std::cout<<"Array: "<<"\n";
  for(int i = 0; i < iWidth; i++){
    for(int j = 0; j < iHigh;j++){
      cout<<pArray[i][j]<<"  ";
    }
    cout<<endl;
  }
}

PS: Here are a few more computing tools for you to take a step further:

Online 1 element function (equation) solving calculation tool:
http://tools.ofstack.com/jisuanqi/equ_jisuanqi

Scientific Calculators online Using _ Advanced calculators online calculation:
http://tools.ofstack.com/jisuanqi/jsqkexue

Online calculators _ Standard calculators:
http://tools.ofstack.com/jisuanqi/jsq

I hope this article is helpful for C++ programming.


Related articles: