高斯消去法c语言
⑴ 如何用c语言用高斯消去法求解线性方程组程序
我们老师要非线性方程组的求解方法,哭死了
⑵ 高斯消去法 c语言
#include<stdio.h>
#define demension 4//修改数组的维数可以在这里改动
void transferM(double array[][demension])
//上三角
{
int row1,row2,col;
double m;
for(row1=0;row1<demension;row1++)
{
for(row2=row1+1;row2<demension;row2++)
{
m=array[row2][row1]/array[row1][row1];
for(col=row1;col<demension;col++)
{
array[row2][col]-=array[row1][col]*m;
}
}
}
}
void main()
{
double a[demension][demension]={
{3.2,2,5,3},
{34,45,64,23},
{23,34,54,5},
{23,34,4,56}
};
transferM(a);
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
printf("%6.2lf",a[i][j]);
}
printf("\n");
}
}
⑶ Gauss消去法C语言程序
void gauss(double a[n][n+1],double x[n])
{
int i,j,k;
double temp,s,l;
for(i=0;i<n-1;i++)
{
//选列主元
k=i;
for(j=i+1;j<n;j++)
{ if(fabs(a[j][i])>fabs(a[k][i]))
k=j;
}
//换行
if(k!=i)
for(j=i;j<=n;j++)
{
temp=a[i][j];
a[i][j]=a[k][j];
a[k][j]=temp;
}
//消元
for(j=i+1;j<n;j++)
{
l=1.0*a[j][i]/a[i][i];
for(k=0;k<n+1;k++)
a[j][k]=a[j][k]-a[i][k]*l;
}
print(a);
printf("\n");
}
print(a);
//回代
x[n-1]=a[n-1][n]/a[n-1][n-1];
for(i=n-2;i>=0;i--)
{
s=0.0;
for(j=i;j<n;j++)
{
if(j==i)
continue;
s+=a[i][j]*x[j];
}
x[i]=(a[i][n]-s)/a[i][i];
}
}
⑷ 用高斯消去法求线性方程组,怎样用c语言编写
#include<conio.h>
#include<math.h>
#include <stdio.h>
#include <assert.h>
#define A(j,k) (*(A+(n+1)*j+k))
#define B(j,k) (*(B+n*j+k)) int GS(double *A, double *result , int n) //编写高斯消去法求线性法
{ double t;
int i,j,k,tj;
for(k=0;k<n-1;k++)
{t=A(k,k);<br> tj=k;<br> for(j=k+1;j<n;j++) //A(j,k)=*(*(A+j)+k)<br> if(fabs(A(j,k))>fabs(t)){t=A(j,k);tj=j;}
if(tj!=k)
{ for(i=0;i<=n;i++)
{ t=A(tj,i);A(tj,i)=A(k,i);A(k,i)=t;}
}
for(i=k+1;i<n;i++)
{ if(A(k,k)==0) return 0;
t=A(i,k)/A(k,k);//t=i/k±
⑸ 用c语言编程高斯全主元消去法(请用图中的过程)
#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 = 1;i <= n;i++)
{
for(j = 1;j<=n;j++)
{
cin>>a[i][j];//系数
}
cin>>b[i];//结果
}
return 0;
}
int find(int k)//寻找第k列主元
{
int i,tag = k;
double maxv=0;
for(i = k;i <= n;i++)
{
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 = 1;j <= n;j++)
{
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 = k; j <= n;j++)
{
a[k][j]/=temp;
}
b[k]/=temp;
}
void disME(int k)
{
int i,j;
for(i =1 ;i<=n;i++)
{
for(j = i;j<= n;j++)
{
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+1;i<= n;i++)
{
t = a[i][k]/a[k][k];
for(j = k;j <= n;j++)
{
a[i][j]-=a[k][j] * t;
}
b[i] -= b[k] * t;
}
}
void Gauss()//高斯消元法
{
int i,j,k;
for(k = 1;k < n;k++)//共进行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 - 1;i>=1;i--)
{
for(j = i+1;j <=n ;j++)
{
b[i] -= a[i][j] * b [j];
}
x[i] = b[i];
}
}
void dis()
{
int i;
cout<<"解方程得:"<<endl;
for(i = 1;i<=n;i++)
{
cout<<"x"<<i<<" = ";
printf("%.5f\n",x[i]);
}
}
int main()
{
set();
Gauss();
dis();
system("pause");
return 0;
}
⑹ 用c语言实现高斯消去法,解三元一次方程组。求具体程序!!
#include<iostream>
#include<cmath>
usingnamespacestd;
#defineMAX50
voidinput(doublea[MAX][MAX+1],intn)
{
cout<<"输入原方程组的增广矩阵"<<endl;
for(inti=0;i<n;i++)
for(intj=0;j<n+1;j++)
cin>>a[i][j];
}
voidoutput(doublex[],intn)
{
cout<<"Gauss消去法得到的原方程组的解为"<<endl;
for(intk=0;k<n;k++)
cout<<x[k]<<"";
}
intmain()
{
doublea[MAX][MAX+1],x[MAX],sum,max,t;
intn,i,j,k,max_i;
cout<<"输入原方程组的阶"<<endl;cin>>n;
input(a,n);
for(k=0;k<n-1;k++)//选主元素
{max=a[k][k];
max_i=k;
for(i=k+1;i<n;i++)
if(fabs(a[i][k])>fabs(max))
{
max=a[i][k];
max_i=i;
}
if(max==0)
break;
if(max_i!=k)//交换两行
for(j=k;j<n+1;j++)
{
t=a[k][j];
a[k][j]=a[max_i][j];
a[max_i][j]=t;
}
for(i=k+1;i<n;i++)
{
a[i][k]=a[i][k]/-a[k][k];
for(j=k+1;j<n+1;j++)
a[i][j]=a[i][j]+a[i][k]*a[k][j];
}//消元
}
if(max==0)cout<<"原方程组无解"<<endl;
else
{
for(k=n-1;k>=0;k--)
{
sum=0;
for(j=k+1;j<n;j++)
sum=sum+a[k][j]*x[j];
x[k]=(a[k][n]-sum)/a[k][k];
}//回代
output(x,n);
cout<<endl;
}
return0;
}
⑺ 用C语言写一个高斯消元法解方程组的程序
我们以方程组
2x1
+
6x2
-
x3
=
-12
5x1
-
x2
+2x3
=
29
-3x1
-
4x2
+
x3
=
5
为例
来说明楼主自己把方程组化为矩阵形式。以下为源代码
。
#include
#include
#include
#include
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]=
2;
a[0][1]=
6;
a[0][2]=
-1;
a[1][0]=
5;
a[1][1]=-1;
a[1][2]=
2;
a[2][0]=-3;
a[2][1]=-4;
a[2][2]=
1;
b[0]
=
-12;
b[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=0;i<3;i++)
{
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=1;k<=n;k++)
{
for(l=k;l<=n;l++)
if(fabs(a[l-1][k-1])>ep)
break;
else
if(l==n)
return(0);
if(l!=k)
{
for(j=k;j<=n;j++)
{
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+1;j<=n;j++)
a[k-1][j-1]=t*a[k-1][j-1];
b[k-1]*=t;
for(i=k+1;i<=n;i++)
{
for(j=k+1;j<=n;j++)
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-1;i>=1;i--)
for(j=i+1;j<=n;j++)
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=0;n<=r-1;++n)
{
y[n]=&x[c*n];
}
return
y
;
}
void
TwoArrayFree(double
**x)
{
free(x[0]);
free(x);
}
⑻ 二元域的高斯消元法C语言
//自己写的,二元域没啥挑战性,还是弄正常的
//有逆矩阵时返回,无时返回NULL
/*
matrix.h
*/
#ifndefHEAD_matrix
#defineHEAD_matrix
typedefstructmatrix*Matrix;
MatrixcreateMatrix(longdouble*data,unsignedlonglength);
MatrixcreateIdentityMatrix(unsignedlonglength);
MatrixplicateMatrix(constMatrixtrx);
voidfreeMatrix(Matrixtrx);
MatrixcaculateInversedMatrix(constMatrixtrx);
constchar*allocateDiscriptionOfMatrix(Matrixtrx);
#endif
/*
matrix.c
*/
#include<stdlib.h>
#include<assert.h>
#include"matrix.h"
#define__nullable
#define__notnull
#definelineOfMatrix(trx,x)(trx->matrixData+trx->length*(x))
#defineelementOfMatrix(trx,x,y)(lineOfMatrix(trx,x)[y])
#defineBytesOfMatrixSize(sizeof(longdouble)*length*length)
structmatrix
{
unsignedlonglength;
longdoublematrixData[];
};
(Matrixtrx,unsignedlonglineIndex,longdouble__notnullk);
(Matrixtrx,unsignedlongxLineIndex,longdouble__notnullk,unsignedlongyLineIndex);
(constMatrixtrx,unsignedlongcolumnIndex,);
(Matrixtrx,unsignedlongxLineIndex,unsignedlongyLineIndex);
constwchar_t*allocateDiscriptionOfMatrix(Matrixtrx)
{
FILE*tempFilePointer=tmpfile();
staticconstwchar_t*format=L"Matrix%lux%lu ";
unsignedlonglength=trx->length;
unsignedlongstringLength=fwprintf(tempFilePointer,format,length,length);
for(unsignedlonglineIndex=0;lineIndex<length;lineIndex++)
{
//wcscat(result,L"[");
for(=0;elementIndexOfLine<length;elementIndexOfLine++)
if(elementIndexOfLine+1==length)
stringLength+=fwprintf(tempFilePointer,L"%5.4Lg",elementOfMatrix(trx,lineIndex,elementIndexOfLine));
else
stringLength+=fwprintf(tempFilePointer,L"%5.4Lg,",elementOfMatrix(trx,lineIndex,elementIndexOfLine));
//if(lineIndex+1==length)
// wcscat(result,L"]");
//else
// wcscat(result,L"] ");
}
stringLength+=length*3-1;
char*result;
if((result=malloc(stringLength+1))!=NULL)
{
=0;
indexOfResultOutput+=swprintf(result,stringLength+1,format,length,length);
for(unsignedlonglineIndex=0;lineIndex<length;lineIndex++)
{
memcpy(result+indexOfResultOutput,L"[",sizeof(wchar_t)*2);
indexOfResultOutput+=2;
for(=0;elementIndexOfLine<length;elementIndexOfLine++)
if(elementIndexOfLine+1==length)
indexOfResultOutput+=swprintf(result+indexOfResultOutput,stringLength+1-indexOfResultOutput,L"%5.4Lg",elementOfMatrix(trx,lineIndex,elementIndexOfLine));
else
indexOfResultOutput+=swprintf(result+indexOfResultOutput,stringLength+1-indexOfResultOutput,L"%5.4Lg,",elementOfMatrix(trx,lineIndex,elementIndexOfLine));
if(lineIndex+1==length)
{
memcpy(result+indexOfResultOutput,L"]",sizeof(wchar_t)*2);
indexOfResultOutput+=2;
}
else
{
memcpy(result+indexOfResultOutput,L"] ",sizeof(wchar_t)*3);
indexOfResultOutput+=3;
}
}
}
fclose(tempFilePointer);
returnresult;
}
staticunsignedlong
MatrixcreateMatrix(longdouble*data,unsignedlonglength)
{
if(length==0||data==NULL)returnNULL;
Matrixresult;
if((result=malloc(sizeof(structmatrix)+BytesOfMatrixSize)==NULL)returnNULL;
memcpy(result->matrixData,data,BytesOfMatrixSize);
returnresult;
}
MatrixcreateIdentityMatrix(unsignedlonglength)
{
if(length==0)returnNULL;
Matrixresult;
if((result=malloc(sizeof(structmatrix)+BytesOfMatrixSize)==NULL)returnNULL;
for(unsignedlongindex=0;index<length;index++)
elementOfMatrix(result,index,index)=1.0L;
returnresult;
}
MatrixplicateMatrix(constMatrixtrx)
{
if(trx==NULL)returnNULL;
Matrixresult;
unsignedlonglength=trx->length;
if((result=malloc(sizeof(structmatrix)+BytesOfMatrixSize)==NULL)returnNULL;
memcpy(result,trx,sizeof(structmatrix)+BytesOfMatrixSize);
returnresult;
}
voidfreeMatrix(Matrixtrx)
{
free(trx);
}
/*FUNCTION:caculateInversedMatrix
*SUCCEED:inversedMatrix
*FAILED:NULL
*INFO:ifcan'
*/
MatrixcaculateInversedMatrix(constMatrixtrx)
{
if(trx==NULL)returnNULL;
constunsignedlonglength=trx->length;
Matrixresult=createIdentityMatrix(length);
MatrixmodifiedMatrix=plicateMatrix(trx);
for(unsignedlonglineIndex=0;lineIndex<length;lineIndex++)
{
;
if((searchForCurrentLineCount=searchForCurrentLineCount(modifiedMatrix,lineIndex,lineIndex))==0)
{
freeMatrix(modifiedMatrix);
freeMatrix(result);
returnNULL;
}
if(searchForCurrentLineCount-1!=lineIndex)
{
manipulateMatrixSwapLineXY(modifiedMatrix,lineIndex,searchForCurrentLineCount-1);
manipulateMatrixSwapLineXY(result,lineIndex,searchForCurrentLineCount-1);
}
for(unsignedlongrestLineIndex=lineIndex+1;restLineIndex<length;restLineIndex++)
if(elementOfMatrix(modifiedMatrix,restLineIndex,lineIndex)!=0.0L)
{
longdoublek=elementOfMatrix(modifiedMatrix,restLineIndex,lineIndex)/elementOfMatrix(modifiedMatrix,lineIndex,lineIndex);
(modifiedMatrix,restLineIndex,k,lineIndex);
(result,restLineIndex,k,lineIndex);
}
}
for(unsignedlonglineCount=length;lineCount>0;lineCount--)
{
#ifdeflineIndex
#
#else
#definelineIndex(lineCount-1)
for(unsignedlongrestLineIndex=0;restLineIndex<lineIndex;restLineIndex++)
if(elementOfMatrix(modifiedMatrix,restLineIndex,lineIndex)!=0.0L)
{
longdoublek=elementOfMatrix(modifiedMatrix,restLineIndex,lineIndex)/elementOfMatrix(modifiedMatrix,lineIndex,lineIndex);
(modifiedMatrix,restLineIndex,k,lineIndex);
(result,restLineIndex,k,lineIndex);
}
#undeflineIndex
#endif
}
for(unsignedlonglineIndex=0;lineIndex<length;lineIndex++)
if(elementOfMatrix(modifiedMatrix,lineIndex,lineIndex)!=1.0L)
{
longdoublek=1/elementOfMatrix(modifiedMatrix,lineIndex,lineIndex);
//manipulateMatrixMutiplyK(modifiedMatrix,lineIndex,k);
manipulateMatrixMutiplyK(result,lineIndex,k);
}
freeMatrix(modifiedMatrix);
returnresult;
}
/*FUNCTION:matrixLineNoneZeroCount
*SUCCEED:Countoflinereturned(notindex)
*FAILED:Zero
*INFO:
*/
(constMatrixtrx,unsignedlongcolumnIndex,)
{
assert(trx!=NULL);
assert(columnIndex<trx->length);
assert(searchFromLineIndex<trx->length);
for(unsignedlonglineIndex=searchFromLineIndex;lineIndex<trx->length;lineIndex++)
if(elementOfMatrix(trx,lineIndex,columnIndex)!=0.0L)returnlineIndex+1;
return0;
}
(Matrixtrx,unsignedlonglineIndex,longdouble__notnullk)
{
assert(trx!=NULL);
assert(lineIndex<trx->length);
assert(k!=0.0L);
longdouble*thisLine=lineOfMatrix(trx,lineIndex);
for(unsignedlongindex=0;index<trx->length;index++)
thisLine[index]=thisLine[index]*k;
}
(Matrixtrx,unsignedlongxLineIndex,longdouble__notnullk,unsignedlongyLineIndex)
{
assert(trx!=NULL);
assert(k!=0.0L);
assert(xLineIndex<trx->length);
assert(yLineIndex<trx->length);
longdouble*xLine=lineOfMatrix(trx,xLineIndex),
*yLine=lineOfMatrix(trx,yLineIndex);
for(unsignedlongindex=0;index<trx->length;index++)
xLine[index]-=k*yLine[index];
}
(Matrixtrx,unsignedlongxLineIndex,unsignedlongyLineIndex)
{
assert(trx!=NULL);
assert(xLineIndex<trx->length);
assert(yLineIndex<trx->length);
longdouble*xLine=lineOfMatrix(trx,xLineIndex),
*yLine=lineOfMatrix(trx,yLineIndex);
for(unsignedlongindex=0;index<trx->length;index++)
{
xLine[index]^=yLine[index];
yLine[index]^=xLine[index];
xLine[index]^=yLine[index];
}
}