线性方程组 精确解 近似解 算法整理

线性方程组 精确解 近似解 算法整理_第1张图片
此时我们只讨论当m==n时的情况,即n阶线性方程组,它的系数矩阵为n+1列为b。
note:
代码假设输入格式为n,代表n阶行列式,随后紧跟着n行,每行n+1个数,代表系数矩阵。

  1. 求精确解的方法:
    1.1 高斯消元法:
//Gauss消元
#include 

using namespace std;

double matrix[20][21], x[20];
int n;

void print_matrix() {
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n+1; j++)
            cout << matrix[i][j] << " ";
        cout << endl;
    }
    cout << endl;
}

int main() {


    //输入
    cin >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n+1; j++)
            cin >> matrix[i][j];
    print_matrix();

    //消元过程
    for(int i = 1; i <= n-1; i++) {
        for(int j = i+1; j <= n; j++) {//第j行
            double l = matrix[j][i] / matrix[i][i];
            cout << "l[" << j << "][" << i << "] = " << l << endl;
            for(int k = i; k <= n+1; k++)//j行的每一个元素
                matrix[j][k] = matrix[j][k] - l*matrix[i][k];
            print_matrix();
        }
    }

    //回代过程
    for(int i = n; i >= 1; i--) {
        double sum = 0;
        for(int j = i+1; j <= n; j++)//求累加和
            sum += matrix[i][j] * x[j];
        x[i] = (matrix[i][n+1] - sum) / matrix[i][i];
    }
    for(int i = 1; i <= n; i++)
        cout << "x[" << i << "] = " << x[i] << endl;

    return 0;
}

1.2高斯主元素消元法:
可以有很好的数值稳定性,避免较大的舍入误差
1.2.1 完全主元素消元法:
note: 选最大主元素的时候不能选择常数列

#include 
#include 

using namespace std;

double matrix[20][21], x[20];
int pos[20];//记录当前列对应的原列序号
int n;

void print_matrix() {
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n+1; j++)
            cout << matrix[i][j] << " ";
        cout << endl;
    }
    cout << endl;
}

void swap_row(int row1, int row2) {
    for(int i = 1; i <= n+1; i++)
        swap(matrix[row1][i], matrix[row2][i]);
}
void swap_col(int col1, int col2) {
    for(int i = 1; i <= n; i++)
        swap(matrix[i][col1], matrix[i][col2]);
}

void find_max(int k) {
    int r = k, c = k;
    double maxs = abs(matrix[k][k]);
    for(int i = k; i <= n; i++) {
        for(int j = k; j <= n; j++) {
            if(abs(matrix[i][j]) > maxs) {
                r = i;
                c = j;
                maxs = abs(matrix[i][j]);
            }
        }
    }
    pos[k] = c;  pos[c] = k;
    swap_row(k, r);  swap_col(k, c);
}

int main() {
    freopen("/Users/really/Documents/code/Gaussdata.txt","r",stdin);

    //输入
    cin >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n+1; j++)
            cin >> matrix[i][j];
    print_matrix();

    //消元过程
    for(int i = 1; i <= n-1; i++) {
        find_max(i);//使主元素选取相较更大的值

        for(int j = i+1; j <= n; j++) {//第j行
            double l = matrix[j][i] / matrix[i][i];
            cout << "l[" << j << "][" << i << "] = " << l << endl;
            for(int k = i; k <= n+1; k++)//j行的每一个元素
                matrix[j][k] = matrix[j][k] - l*matrix[i][k];
            print_matrix();
        }
    }

    //回代过程
    for(int i = n; i >= 1; i--) {
        double sum = 0;
        for(int j = i+1; j <= n; j++)//求累加和
            sum += matrix[i][j] * x[j];
        x[i] = (matrix[i][n+1] - sum) / matrix[i][i];
    }
    for(int i = 1; i <= n; i++) {//将映射的解回归原位置
        if(pos[i] > i)//不用!=避免重复交换
            swap(x[i], x[pos[i]]);
    }
    for(int i = 1; i <= n; i++)
        cout << "x[" << i << "] = " << x[i] << endl;

    return 0;
}

1.2.2 列主元素消元法:
相比完全主元素效率更高,但是稳定性不如完全主元素,要求系数行列式不为0.

#include 
#include 

using namespace std;

double matrix[20][21], x[20];
int n;

void print_matrix() {
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n+1; j++)
            cout << matrix[i][j] << " ";
        cout << endl;
    }
    cout << endl;
}

void swap_row(int row1, int row2) {
    for(int i = 1; i <= n+1; i++)
        swap(matrix[row1][i], matrix[row2][i]);
}

void find_max(int k) {
    int r = k;
    double maxs = abs(matrix[k][k]);
    for(int i = k; i <= n; i++) {
        if(abs(matrix[i][k]) > maxs) {
            r = i;
            maxs = abs(matrix[i][k]);
        }
    }
    swap_row(k, r);
}

int main() {
    freopen("/Users/really/Documents/code/Gaussdata.txt","r",stdin);

    //输入
    cin >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n+1; j++)
            cin >> matrix[i][j];
    print_matrix();

    //消元过程
    for(int i = 1; i <= n-1; i++) {
        find_max(i);//使主元素选取相较更大的值

        for(int j = i+1; j <= n; j++) {//第j行
            double l = matrix[j][i] / matrix[i][i];
            cout << "l[" << j << "][" << i << "] = " << l << endl;
            for(int k = i; k <= n+1; k++)//j行的每一个元素
                matrix[j][k] = matrix[j][k] - l*matrix[i][k];
            print_matrix();
        }
    }

    //回代过程
    for(int i = n; i >= 1; i--) {
        double sum = 0;
        for(int j = i+1; j <= n; j++)//求累加和
            sum += matrix[i][j] * x[j];
        x[i] = (matrix[i][n+1] - sum) / matrix[i][i];
    }

    for(int i = 1; i <= n; i++)
        cout << "x[" << i << "] = " << x[i] << endl;

    return 0;
}

1.3 矩阵分解法:

#include 

using namespace std;

double matrix[20][21], x[20];
int n;

void print_matrix() {
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n+1; j++)
            cout << matrix[i][j] << " ";
        cout << endl;
    }
    cout << endl;
}


int main() {
    //输入
    cin >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n+1; j++)
            cin >> matrix[i][j];
    print_matrix();

    //LU分解
    for(int i = 1; i <= n-1; i++) {
        for(int j = i+1; j <= n; j++) {//第j行
            double l = matrix[j][i] / matrix[i][i];
            cout << "l[" << j << "][" << i << "] = " << l << endl;
            matrix[j][i] = l;

            for(int k = i+1; k <= n; k++)//j行的每一个元素
                matrix[j][k] = matrix[j][k] - l*matrix[i][k];
            print_matrix();
        }
    }

    //回代y的值并保存在x当中
    for(int i = 1; i <= n; i++) {
        double sum = 0;
        for(int j = 1; j < i; j++)
            sum += matrix[i][j] * x[j];
        x[i] = (matrix[i][n+1] - sum);
    }

    //更新常数列的值为y的值
    for(int i = 1; i <= n; i++)
        matrix[i][n+1] = x[i];

    //回代x的值
    for(int i = n; i >= 1; i--) {
        double sum = 0;
        for(int j = i+1; j <= n; j++)//求累加和
            sum += matrix[i][j] * x[j];
        x[i] = (matrix[i][n+1] - sum) / matrix[i][i];
    }

    for(int i = 1; i <= n; i++)
        cout << "x[" << i << "] = " << x[i] << endl;

    return 0;
}
  1. 求近似解的方法:
    2.1 迭代法

你可能感兴趣的:(数学,算法杂记)