純C/C++_高斯消去法(Gaussian Elimination)解聯立方程式
純C/C++_高斯消去法(Gaussian Elimination)解聯立方程式
資料來源(對應程式碼專案GITHUB):https://github.com/jash-git/C_Gaussian-Elimination
題目:https://github.com/jash-git/C_Gaussian-Elimination/blob/master/%E9%A1%8C%E7%9B%AE/35377.jpg?raw=true
/*———————————————————–*/
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
/*
利用高斯消去法,求解線性代數聯立方程式
i+j=8
K-L=6
i+K=13
j+L=8
*/
int gaus(double *a,double *b,int n)
{ int *js,l,k,i,j,is,p,q;
double d,t;
js=(int *)malloc(n*sizeof(int));
l=1;
for (k=0;k<=n-2;k++)
{ d=0.0;
for (i=k;i<=n-1;i++)
for (j=k;j<=n-1;j++)
{ t=fabs(a[i*n+j]);
if (t>d) { d=t; js[k]=j; is=i;}
}
if (d+1.0==1.0) l=0;
else
{ if (js[k]!=k)
for (i=0;i<=n-1;i++)
{ p=i*n+k; q=i*n+js[k];
t=a[p]; a[p]=a[q]; a[q]=t;
}
if (is!=k)
{ for (j=k;j<=n-1;j++)
{ p=k*n+j; q=is*n+j;
t=a[p]; a[p]=a[q]; a[q]=t;
}
t=b[k]; b[k]=b[is]; b[is]=t;
}
}
if (l==0)
{ free(js); printf(“fail\n”);
return(0);
}
d=a[k*n+k];
for (j=k+1;j<=n-1;j++)
{ p=k*n+j; a[p]=a[p]/d;}
b[k]=b[k]/d;
for (i=k+1;i<=n-1;i++)
{ for (j=k+1;j<=n-1;j++)
{ p=i*n+j;
a[p]=a[p]-a[i*n+k]*a[k*n+j];
}
b[i]=b[i]-a[i*n+k]*b[k];
}
}
d=a[(n-1)*n+n-1];
if (fabs(d)+1.0==1.0)
{ free(js); printf(“fail\n”);
return(0);
}
b[n-1]=b[n-1]/d;
for (i=n-2;i>=0;i–)
{ t=0.0;
for (j=i+1;j<=n-1;j++)
t=t+a[i*n+j]*b[j];
b[i]=b[i]-t;
}
js[n-1]=n-1;
for (k=n-1;k>=0;k–)
if (js[k]!=k)
{ t=b[k]; b[k]=b[js[k]]; b[js[k]]=t;}
free(js);
return(1);
}
main()
{ int i;
static double a[4][4]={
{1,1,0,0},
{0,0,1,-1},
{1,0,1,0},
{0,1,0,1}
};
static double b[4]={8,6,13,8};
double *pa,*pb;
pa=(double *)a;
pb=(double *)b;
if (gaus(pa,pb,4)!=0)
for (i=0;i<=4;i++)
printf(“x(%d)=%e\n”,i,b[i]);
}