用c语言编写一个程序用高斯消元法求解线性方程组的解程序流程图 20
2个回答
展开全部
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n,i,j,k;
double a[100][100],b[100],o;
cout<<"输入未知数个数:"<<endl;
cin>>n;
cout<<"输入"<<n<<"个方程组(未知数系数及等号右边常数):"<<endl;
for (i=1;i<=n;i++)
for (j=1;j<=n+1;j++)
cin>>a[i][j];
for (i=1;i<=n;i++)
for (j=i+1;j<=n;j++)
if (fabs(a[j][i])>1e-7)
{
o=a[i][i]/a[j][i];
for (k=i;k<=n+1;k++)
a[j][k]=a[j][k]*o-a[i][k];
}
for (i=n;i>0;i--)
{
b[i]=a[i][n+1]/a[i][i];
for (j=i-1;j>0;j--)
a[j][n+1]=a[j][n+1]-b[i]*a[j][i];
}
cout<<"解得:"<<endl;
for (i=1;i<=n;i++) cout<<b[i]<<endl;
system("pause");
return 0;
}
#include <cmath>
using namespace std;
int main()
{
int n,i,j,k;
double a[100][100],b[100],o;
cout<<"输入未知数个数:"<<endl;
cin>>n;
cout<<"输入"<<n<<"个方程组(未知数系数及等号右边常数):"<<endl;
for (i=1;i<=n;i++)
for (j=1;j<=n+1;j++)
cin>>a[i][j];
for (i=1;i<=n;i++)
for (j=i+1;j<=n;j++)
if (fabs(a[j][i])>1e-7)
{
o=a[i][i]/a[j][i];
for (k=i;k<=n+1;k++)
a[j][k]=a[j][k]*o-a[i][k];
}
for (i=n;i>0;i--)
{
b[i]=a[i][n+1]/a[i][i];
for (j=i-1;j>0;j--)
a[j][n+1]=a[j][n+1]-b[i]*a[j][i];
}
cout<<"解得:"<<endl;
for (i=1;i<=n;i++) cout<<b[i]<<endl;
system("pause");
return 0;
}
展开全部
DEVC++通过
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
float sum( float a[][3],float x[],float y[],int n,int k)
{
float s=0;
for(int i=0;i<n;i++)
{
if(i<k)s+=a[k][i]*y[i];
else if(i> k)s+=a[k][i]*x[i];
}
return s;
}
int main()
{
float a[3][3]={5,2,1,
-1,4,2,
2,-3,10};
float b[]={-12,20,3};
float y[3];
float m=1e-3;
int i;
float x[]={-3,1,1},max,n=1;
cout<<endl;
while(1)
{
for( i=0;i<3;i++)
y[i] = (b[i]-sum(a,x,y,3,i))/a[i][i];
max=fabs(y[0]-x[0]);
for(i=1;i<3;i++)
if(fabs(y[i]-x[i])>max)max=fabs(y[i]-x[i]);
if(max<=m)
{
cout<<left<<setw(9)<<n++;
for(i=0;i<3;i++)
cout<<left<<setw(18)<<y[i];
cout<<endl<<endl;
break;
}
else
{
cout<<left<<setw(9)<<n++;
for(i=0;i<3;i++)
cout<<left<<setw(18)<<y[i];
cout<<endl;
for(i=0;i<3;i++)
x[i]=y[i];
}
}
system("PAUSE");
return 0;
}
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
float sum( float a[][3],float x[],float y[],int n,int k)
{
float s=0;
for(int i=0;i<n;i++)
{
if(i<k)s+=a[k][i]*y[i];
else if(i> k)s+=a[k][i]*x[i];
}
return s;
}
int main()
{
float a[3][3]={5,2,1,
-1,4,2,
2,-3,10};
float b[]={-12,20,3};
float y[3];
float m=1e-3;
int i;
float x[]={-3,1,1},max,n=1;
cout<<endl;
while(1)
{
for( i=0;i<3;i++)
y[i] = (b[i]-sum(a,x,y,3,i))/a[i][i];
max=fabs(y[0]-x[0]);
for(i=1;i<3;i++)
if(fabs(y[i]-x[i])>max)max=fabs(y[i]-x[i]);
if(max<=m)
{
cout<<left<<setw(9)<<n++;
for(i=0;i<3;i++)
cout<<left<<setw(18)<<y[i];
cout<<endl<<endl;
break;
}
else
{
cout<<left<<setw(9)<<n++;
for(i=0;i<3;i++)
cout<<left<<setw(18)<<y[i];
cout<<endl;
for(i=0;i<3;i++)
x[i]=y[i];
}
}
system("PAUSE");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询