用c语言编程高斯全主元消去法(请用图中的过程)

Python010

用c语言编程高斯全主元消去法(请用图中的过程),第1张

#include <iostream>

#include <iomanip.h>

using namespace std

#define N 20

double a[N][N]

double x[N+1]

double b[N+1]

int n//n方程个数,n未知数个数

int set( )

{

cout<<"请输入方程的个数和未知数个数: "<<endl

cin>>n

int i,j

cout<<"请输入方程组(逐个输入方程 i)"<<endl

for(i = 1i <= ni++)

{

for(j = 1j<=nj++)

{

cin>>a[i][j]//系数

}

cin>>b[i]//结果

}

return 0

}

int find(int k)//寻找第k列主元

{

int i,tag = k

double maxv=0

for(i = ki <= ni++)

{

if(a[i][k] >maxv)

{

maxv = a[i][k]

tag = i

}

}

return tag

}

void exchange(int i1,int i2)//将第 i1 i2行互换

{

int j

for(j = 1j <= nj++)

{

swap(a[i1][j],a[i2][j])

}

swap(b[i1],b[i2])

}

void div(int k)//让第k个方程的首项系数为1

{

double temp = a[k][k]

for(int j = kj <= nj++)

{

a[k][j]/=temp

}

b[k]/=temp

}

void disME(int k)

{

int i,j

for(i =1 i<=ni++)

{

for(j = ij<= nj++)

{

if(a[i][j])

{

if(a[i][j]==1)

{ if(j==n)

cout<<"x"<<j

else

cout<<"x"<<j<<" + "

}

else

{

if(j==n)

cout<<a[i][j]<<"x"<<j

else

cout<<a[i][j]<<"x"<<j<<" + "

}

}

}

cout<<" = "<<b[i]<<endl

}

system("pause")

}

void eliminate(int k)//消元

{

int i,j

double t

for(i = k+1i<= ni++)

{

t = a[i][k]/a[k][k]

for(j = kj <= nj++)

{

a[i][j]-=a[k][j] * t

}

b[i] -= b[k] * t

}

}

void Gauss()//高斯消元法

{

int i,j,k

for(k = 1k <nk++)//共进行n - 1次消元

{

int l = find(k)//寻找第k次消元法的主元方程

if(l!=k)

{

exchange(l,k)

}

//消元

div(k)

eliminate(k)

cout<<"第"<<k<<"次消元结果:"<<endl

disME(k)

}

div(k)

x[k] = b[k]

//disM()

for(i = n - 1i>=1i--)

{

for(j = i+1j <=n j++)

{

b[i] -= a[i][j] * b [j]

}

x[i] = b[i]

}

}

void dis()

{

int i

cout<<"解方程得:"<<endl

for(i = 1i<=ni++)

{

cout<<"x"<<i<<" = "

printf("%.5f\n",x[i])

}

}

int main()

{

set()

Gauss()

dis()

system("pause")

return 0

}

我们以方程组2x1 + 6x2 - x3 = -12

5x1 - x2 +2x3 = 29

-3x1 - 4x2 + x3 = 5

为例 来说明楼主自己把方程组化为矩阵形式。以下为源代码 。

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

#include <math.h>

int GS(int,double**,double *,double)

double **TwoArrayAlloc(int,int)

void TwoArrayFree(double **)

int main(void)

{

int i,n

double ep,**a,*b

n = 3

ep = 1e-4

a = TwoArrayAlloc(n,n)

b = (double *)calloc(n,sizeof(double))

if(b == NULL)

{

printf("memory get error\n")

exit(1)

}

a[0][0]= 2a[0][1]= 6a[0][2]= -1

a[1][0]= 5a[1][1]=-1a[1][2]= 2

a[2][0]=-3a[2][1]=-4a[2][2]= 1

b[0] = -12b[1] = 29 b[2] = 5

if(!GS(n,a,b,ep))

{

printf("can't solve it with GS elimination\n")

exit(0)

}

printf("The solution of equations is as follows:\n")

for(i=0i<3i++)

{

printf("x%d = %.2f\n",i,b[i])

}

TwoArrayFree(a)

free(b)

return 0

}

int GS(n,a,b,ep)

int n

double **a

double *b

double ep

{

int i,j,k,l

double t

for(k=1k<=nk++)

{

for(l=kl<=nl++)

if(fabs(a[l-1][k-1])>ep)

break

else if(l==n)

return(0)

if(l!=k)

{

for(j=kj<=nj++)

{

t = a[k-1][j-1]

a[k-1][j-1] =a[l-1][j-1]

a[l-1][j-1] =t

}

t=b[k-1]

b[k-1]=b[l-1]

b[l-1]=t

}

t=1/a[k-1][k-1]

for(j=k+1j<=nj++)

a[k-1][j-1]=t*a[k-1][j-1]

b[k-1]*=t

for(i=k+1i<=ni++)

{

for(j=k+1j<=nj++)

a[i-1][j-1]-=a[i-1][k-1]*a[k-1][j-1]

b[i-1]-=a[i-1][k-1]*b[k-1]

}

}

for(i=n-1i>=1i--)

for(j=i+1j<=nj++)

b[i-1]-=a[i-1][j-1]*b[j-1]

return(1)

}

double **TwoArrayAlloc(int r,int c)

{

double *x,**y

int n

x=(double *)calloc(r*c,sizeof(double))

y=(double **)calloc(r,sizeof(double*))

for(n=0n<=r-1++n)

{

y[n]=&x[c*n]

}

return y

}

void TwoArrayFree(double **x)

{

free(x[0])

free(x)

}