Solution of Linear Equation Group (c + +) __c++ by column principal element elimination method

Source: Internet
Author: User

Then last continued, last used the Gauss elimination method http://blog.csdn.net/qq_26025363/article/details/53027926, but, in the elimination process, can not make the main element a (ii) ≠0, but very hours, By dividing it, it leads to a serious increase in the order of other elements, an extension of rounding errors, and a result that results in unreliable calculations. So this time using the column main element elimination method, the idea is to exchange the row with the decimal number and the largest row in the column.
or write a code.

#include <iostream> #include <cmath> using namespace std;
const int n = 3;
    Swap 2 Number of sizes Template<class t> void swap (t& A, t& b) {T C;
    c = A;

    A = b;
b = C;
    }//Gaussian column main element elimination method void gaussin_l (double a[n][n], double b[n]) {int I, j, K;
    int col, row;
        for (k = 0; k < n-1; k++) {Double Ave = 0;
                Find the position of the largest element in the elimination column for (i = k; i < n; i++) if (Fabs (a[i][k)) > Ave) {
                Ave = Fabs (a[i][k]);
                cout << "Ave" << Ave << Endl;
                row = i;
    col = k; //If the diagonal element is 0, the same cannot be solved with the Gauss elimination method if (a[row][row] = = 0) {cout << "can ' t solve" <
            < Endl;
        Return

                        ///To swap the identified rows for the IF (k!= row) for (i = 0; i < n; i++) {

                        SWAP (A[row][i], a[k][i]);
            SWAP (B[k], b[row]); }
        Elimination process double C[n];
            for (j = k + 1; j < N; j + +) {C[j] = A[j][k]/a[k][k];
        cout << C[j] << Endl; for (i = k + 1; i < n; i++) {for (j = 1; j < N; j +) {a[
            I][J] = a[i][j]-c[i] * A[K][J];
        } B[i] = B[i]-c[i] * b[k];
    } double x[n];
    X[n-1] = b[n-1]/a[n-1][n-1]; for (i = n-2 i >= 0; i--) {double sum = 0; for (j = i + 1; j < N; j + +) sum = a I
        [j] * X[J];
    X[i] = (b[i]-sum)/a[i][i]; //printout for (i = 0; i < n; i++) cout << "x" << "[" << I << "]=" << x[i
] << Endl;
 }

`

For this algorithm, a brief analysis, the algorithm is basically still not separated from the Gauss elimination method, just a little bit of a sort, reduce the error, but also increased the time spent sorting, other than the Gauss elimination method is not different, but if you want to improve the accuracy of the error and so on, you can use the ranks of the elimination of the law, is to select the largest number from the entire matrix, as a divisor, and then elimination, then pick the second elimination matrix of the largest number of elimination.

================================== Test Code '

int main ()
{
double a[3][3] ={0.001,2.000,3.000,-1.000,3.712,4.623,-2.000,1.072,5.643};
Double B[3] = {1.000,2.000,3.000};
Gaussin_l (A, b);
return 0;
}

For the result, in fact, the error is not small, to 4 after the decimal point is not allowed, but if you change other floating-point number operation
For the result, sometimes the error can frighten you silly, this method is still not ideal. If you have a problem with this, you are welcome to exchange more ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.