signed

QiShunwang

“诚信为本、客户至上”

实验六 变质法求解线性方程

2021/4/26 13:30:02   来源:

实验六 变质法求解线性方程

  • 题目描述
  • 伪代码
  • 代码实现
  • 运行结果

题目描述

在这里插入图片描述
在这里插入图片描述

伪代码

void BetterForwardElimination()
	/* 把a矩阵加入到上三角矩阵当中 */
	for i ← 0 to rows do
		for j ← 0 to rows do
			a_up_det[i][j] ← a[i][j]
	/* 把b向量加入到上三角矩阵的最后一列 */
	for i ← 0 to rows do
		a_up_det[i][columns - 1] ← b[i];
	// 检验解是否唯一
	for i ← 0 to rows do
		/* 交换使得该列最大的元素在第一行 */
		pivotrow ← i;
		for j ← i + 1 to rows do 
			if abs(a_up_det[j][i]) > abs(a_up_det[pivotrow][i])
				pivotrow ← j
		for k ← i to columns do 
			swap(a_up_det[i][k], a_up_det[pivotrow][k])

		for j ← i + 1 to rows
			tmp ← a_up_det[j][i] / a_up_det[i][i];
			for k ← i to columns do
				a_up_det[j][k] ← a_up_det[j][k] - a_up_det[i][k] * tmp;

代码实现

#include<iostream>
#include<cmath>
using namespace std;
const int columns = 4;
const int rows = 3;
double a[rows][rows], b[rows], a_up_det[rows][columns];
/* 输出一个A的上三角矩阵 */
void BetterForwardElimination();
void swap(double& p, double& q);
void printArray();
int main() {
	cout << "请输入一个3×3的矩阵,以空格为分隔:" << endl;
	
	for (int i = 0; i < rows; ++i) {
		for (int j = 0; j < rows; ++j) {
			cin >> a[i][j];
		}
	}
	cout << "请输入一个向量,以空格为分隔:" << endl;
	for(int i = 0; i < rows; ++i) {
		cin >> b[i];
	}
	
	BetterForwardElimination();

	/* 求解 */
	double x1, x2, x3;
	x3 = a_up_det[rows - 1][columns - 1] / a_up_det[rows - 1][columns - 2];
	x2 = (a_up_det[rows - 2][columns - 1] - x3 * a_up_det[rows - 2][columns - 2]) / a_up_det[rows - 2][columns - 3];
	x1 = (a_up_det[rows - 3][columns - 1] - x3 * a_up_det[rows-3][columns-2] - x2 * a_up_det[rows - 3][columns - 3]) / a_up_det[rows - 3][columns - 4];
	cout << "x1 = " << x1 << endl << "x2 = " << x2 << endl << "x3 = " << x3 << endl;
	return 0;
}

void BetterForwardElimination() {
	/* 把a矩阵加入到上三角矩阵当中 */
	for (int i = 0; i < rows; ++i)
		for (int j = 0; j < rows; ++j)
			a_up_det[i][j] = a[i][j];
	/* 把b向量加入到上三角矩阵的最后一列 */
	for (int i = 0; i < rows; ++i) 
		a_up_det[i][columns - 1] = b[i];

	/* 检验是否有唯一解 */
	int flag = 0;
	double t = a_up_det[0][0] / a_up_det[1][0];
	for (int i = 1; i < columns; ++i) {
		if (a_up_det[0][i] / a_up_det[1][i] != t) {
			flag = 1;
			break;
		}
	}
	if (flag == 0) {
		cout << "No solution" << endl;
		return;
	}
	flag = 0;
	double t = a_up_det[0][0] / a_up_det[2][0];
	for (int i = 1; i < columns; ++i) {
		if (a_up_det[0][i] / a_up_det[2][i] != t) {
			flag = 1;
			break;
		}
	}
	if (flag == 0) {
		cout << "No solution" << endl;
		return;
	}
	flag = 0;
	double t = a_up_det[1][0] / a_up_det[2][0];
	for (int i = 1; i < columns; ++i) {
		if (a_up_det[1][i] / a_up_det[2][i] != t) {
			flag = 1;
			break;
		}
	}
	if (flag == 0) {
		cout << "No solution" << endl;
		return;
	}
	
	for (int i = 0; i < rows; ++i) {
		/* 交换使得该列最大的元素在第一行 */
		int pivotrow = i;
		for (int j = i + 1; j < rows; ++j) 
			if (abs(a_up_det[j][i]) > abs(a_up_det[pivotrow][i])) 
				pivotrow = j;
		for (int k = i; k < columns; ++k) 
			swap(a_up_det[i][k], a_up_det[pivotrow][k]);

		for (int j = i + 1; j <rows; ++j) {
			double tmp = a_up_det[j][i] / a_up_det[i][i];
			for (int k = i; k < columns; ++k) 
				a_up_det[j][k] -= a_up_det[i][k] * tmp;
		}
		printArray();
	}
}

void swap(double &p, double &q) {
	double t = p;
	p = q;
	q = t;
}

void printArray() {
	cout << endl;
	for (int i = 0; i < rows; ++i) {
		for (int j = 0; j < columns; ++j) {
			cout << a_up_det[i][j] << "\t";
		}
		cout << endl;
		cout << endl;
	}
}

运行结果

在这里插入图片描述