高斯消元法的简介
数学上,高斯消元法(或译:高斯消去法),是线性代数规划中的一个算法,可用来为线性方程组求解。但其算法十分复杂,不常用于加减消元法,求出矩阵的秩,以及求出可逆方阵的逆矩阵。不过,如果有过百万条等式时,这个算法会十分省时。一些极大的方程组通常会用迭代法以及花式消元来解决。当用于一个矩阵时,高斯消元法会产生出一个“行梯阵式”。高斯消元法可以用在电脑中来解决数千条等式及未知数。亦有一些方法特地用来解决一些有特别排列的系数的方程组。
代码实现
#include <stdio.h>
#include <math.h>
int n=4; //未知数个数
int main(void)
{
double a[n][n+1];
int f=1;
int bj=0;
double count;
int f1=1;
double bs;
double x[n];
//输入矩阵元素
for (int i = 0; i < n; i++)
{
printf("请输入第 %d 行",i+1);
for (int j = 0; j < n+1; j++)
{
scanf("%lf",&a[i][j]);
}
}
//转换为上三角矩阵
for (int i = 0; i < n; i++)
{
f=1;
//保证对角线非0
if(a[i][i]==0)
{
f=0;
for (int k = i+1; k < n; k++)
{
if(a[k][i] != 0)
{
bj=k;
f=1;
}
}
if (f==1)
{
//交换行
for (int q = 0; q < n+1; q++)
{
count=a[bj][q];
a[bj][q]=a[i][q];
a[i][q]=count;
}
}
else printf("erro");
}
//消元
for (int k = i+1; k < n; k++)
{
bs=a[k][i]/a[i][i];
for (int j = 0; j < n+1; j++)
{
a[k][j]=a[k][j]-bs*a[i][j];
}
}
}
//输出上三角矩阵
printf("上三角矩阵为:\n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n+1; j++)
{
printf("%.3lf ",a[i][j]);
}
printf("\n");
}
//求解
for (int i = n-1; i >=0; i--)
{
count=a[i][n];
for (int j= i+1; j<n ;j++)
{
count-=a[i][j]*x[j];
}
x[i]=count/a[i][i];
}
//输出解
printf("解为:\n");
for (int i = 0; i < n; i++)
{
printf("x[%d]=%.3lf\n",i+1,x[i]);
}
}
代码优化
对代码进行模块化处理,利于修改。
输入矩阵元素
void inputMatrix(double a[MAX_SIZE][MAX_SIZE + 1], int n) {
for (int i = 0; i < n; i++) {
printf("请输入第%d 行:", i + 1);
for (int j = 0; j < n + 1; j++) {
scanf("%lf", &a[i][j]);
}
}
}
高斯消元
void gaussianElimination(double a[MAX_SIZE][MAX_SIZE + 1], int n) {
for (int i = 0; i < n; i++) {
int f = 1;
int bj = 0;
double count;
double bs;
// 确保对角元素非零
if (a[i][i] == 0) {
f = 0;
for (int k = i + 1; k < n; k++) {
if (a[k][i] != 0) {
bj = k;
f = 1;
}
}
if (f == 1) {
// 交换行
for (int q = 0; q < n + 1; q++) {
count = a[bj][q];
a[bj][q] = a[i][q];
a[i][q] = count;
}
} else {
printf("Error: Diagonal element is zero, unable to perform Gaussian elimination.");
return;
}
}
// 消元
for (int k = i + 1; k < n; k++) {
bs = a[k][i] / a[i][i];
for (int j = 0; j < n + 1; j++) {
a[k][j] = a[k][j] - bs * a[i][j];
}
}
}
}
求解
void backSubstitution(double a[MAX_SIZE][MAX_SIZE + 1], int n, double x[MAX_SIZE]) {
for (int i = n - 1; i >= 0; i--) {
double count = a[i][n];
for (int j = i + 1; j < n; j++) {
count -= a[i][j] * x[j];
}
if (fabs(a[i][i]) < 1e-10) {
printf("Error: Singular matrix, back substitution not possible.");
return;
}
x[i] = count / a[i][i];
}
}
打印矩阵
void printMatrix(double a[MAX_SIZE][MAX_SIZE + 1], int n) {
printf("上三角矩阵为:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n + 1; j++) {
printf("%.3lf ", a[i][j]);
}
printf("\n");
}
}
打印解
void printSolution(double x[MAX_SIZE], int n) {
printf("解为:\n");
for (int i = 0; i < n; i++) {
printf("x[%d]=%.3lf\n", i + 1, x[i]);
}
}
主函数
#include <stdio.h>
#include <math.h>
#define MAX_SIZE 4 //未知数个数
void inputMatrix(double a[MAX_SIZE][MAX_SIZE + 1], int n);
void gaussianElimination(double a[MAX_SIZE][MAX_SIZE + 1], int n);
void backSubstitution(double a[MAX_SIZE][MAX_SIZE + 1], int n, double x[MAX_SIZE]);
void printMatrix(double a[MAX_SIZE][MAX_SIZE + 1], int n);
void printSolution(double x[MAX_SIZE], int n);
int main(void) {
double a[MAX_SIZE][MAX_SIZE + 1];
double x[MAX_SIZE];
inputMatrix(a, MAX_SIZE);
gaussianElimination(a, MAX_SIZE);
printMatrix(a, MAX_SIZE);
backSubstitution(a, MAX_SIZE, x);
printSolution(x, MAX_SIZE);
return 0;
总结
以上便是基于c++的应用高斯消元法求解线性方程组的全过程。
github链接:link