C语言实现高斯主元素法

在这里插入图片描述
PS:每个列主元素都是当前列最大值

#include<stdio.h>
#define Col 4//列数
#define Row 3//行数
void Print(double a[Row][Col]) {//打印a数组
	for (int i = 0; i < Row; i++)//找到最大主元,记录行号和列号
	{
		for (int j = 0; j < Col; j++)
		{
			printf("%f  ", a[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

void  ColumnSwap(double a[Row][Col], int m, int n) //m和n列交换
{
	double temp;
	for (int i = 0; i < Row; i++)
	{
		temp = a[i][m];
		a[i][m] = a[i][n];
		a[i][n] = temp;
	}
}

void  RowSwap(double a[Row][Col], int m, int n) //m和n行交换
{
	double temp;
	for (int i = 0; i < Col; i++)
	{
		temp = a[m][i];
		a[m][i] = a[n][i];
		a[n][i] = temp;
	}
}
void Compute(double a[Row][Col], double result[Col - 1])//给定上三角矩阵,求解
{
	for (int i = Col - 2; i >= 0; i--)//求列主元的值
	{
		double sum = 0;
		//求sum
		for (int j = Col - 2; j > i; j--)
		{
			sum = sum + a[i][j] * result[j];
		}
		//求result解
		result[i] = (a[i][Col - 1] - sum) / a[i][i];
	}
}
void  SelectColumn(double a[Row][Col], int n) //找列主元
{
	double Max;
	int RowNum = n;
	Max = a[n][n];
	for (int i = n; i < Row; i++)//找到最大列主元,记录行号
	{
		if (a[i][n] > Max)
		{
			Max = a[i][n];
			RowNum = i;
		}
	}
	RowSwap(a, RowNum, n);//两行交换
}
void  SelectTotal(double a[Row][Col], int n) //找全主元
{
	double Max;
	int   RowNum = n, ColNum = n;
	Max = a[n][n];
	for (int i = n; i < Row; i++)//找到最大主元,记录行号和列号
	{
		for (int j = n; j < Col - 1; j++)
		{
			if (a[i][j] > Max)
			{
				Max = a[i][j];
				RowNum = i;
				ColNum = j;
			}
		}
	}
	RowSwap(a, RowNum, n);//两行交换
	ColumnSwap(a, ColNum, n);//两列交换
}

void ColumnEliminate(double a[Row][Col], double result[Col - 1])//高斯列主元素消去法
{
	double MainElem, Elem;//主元,普通元
	for (int i = 0; i < Row; i++)
	{
		SelectColumn(a, i); //找列主元
		Print(a);
		MainElem = a[i][i];
		for (int row = i + 1; row < Row; row++)
		{
			Elem = a[row][i];
			for (int col = i; col < Col; col++)
			{
				a[row][col] = a[row][col] + Elem*a[i][col] * (-1) / MainElem;
			}
		}
		Print(a);
	}
	Compute(a, result);
}
void TotalEliminate(double a[Row][Col], double result[Col - 1])//高斯全主元素消去法
{
	double MainElem, Elem;//主元,普通元
	for (int i = 0; i < Row; i++)
	{
		SelectTotal(a, i); //找列主元
		Print(a);
		MainElem = a[i][i];
		for (int row = i + 1; row < Row; row++)
		{
			Elem = a[row][i];
			for (int col = i; col < Col; col++)
			{
				a[row][col] = a[row][col] + Elem*a[i][col] * (-1) / MainElem;
			}
		}
		Print(a);
	}
	Compute(a, result);
}

int main()
{
	double a[3][4] = { 
	{ -0.002, 2, 2, 0.4 },
	{ 1, 0.78125, 0, 1.3816 },
	{ 3.996, 5.5625, 4, 7.4178 }, };
	double result[Col - 1];
	ColumnEliminate(a, result);//列主元消去法
							   //TotalEliminate(a, result);//全主元消去法,上下方法每次只能用一种
	Print(a);
	printf("x=(");
	for (int i = 0; i <Col - 1; i++)
	{
		printf("%f", result[i]);

		if(i < Col - 2)
			printf(",");

	}
	printf(")");
	getchar();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值