FORTRAN语言用Jacobi迭代和Gauss-Seidel迭代法求解线性方程组AX=b
用的是FORTRAN语言,请哪位编程高手帮帮忙知道工作量有点大,如果完成了我会继续加分的谢谢~...
用的是FORTRAN语言,请哪位编程高手帮帮忙知道工作量有点大,如果完成了我会继续加分的谢谢~
展开
2个回答
2013-06-07
展开全部
源程序我看你怎么改#include "stdio.h "
void jacobi(double **a,double *b,double *x,int n,int Times)
{
int i,j,t;
double *xx=new double[n];
for(t=0;t <Times;t++)
{
for(i=0;i <n;i++)
{
xx[i]=b[i];
for(j=0;j <n;j++)
if(j!=i)
xx[i]-=a[i][j]*x[j];
xx[i]/=a[i][i];
}
for(i=0;i <n;i++)
x[i]=xx[i];
}
delete xx;
printf( "jacobi:\t ");
for(i=0;i <n;i++)
printf( "%6.4f ",x[i]);
printf( "\n ");
}
void gs(double **a,double *b,double *x,int n,int Times)
{
int i,j,t;
for(t=0;t <Times;t++)
{
for(i=0;i <n;i++)
{
x[i]=b[i];
for(j=0;j <n;j++)
if(j!=i)
x[i]-=a[i][j]*x[j];
x[i]/=a[i][i];
}
}
printf( "gs:\t ");
for(i=0;i <n;i++)
printf( "%6.4f ",x[i]);
printf( "\n ");
}
int main(int argc, char* argv[])
{
int i,j,n;
double **a,*b;
FILE *fp;
fp=fopen( "d:\\solve.txt ", "r ");
fscanf(fp, "%d ",&n);
a=new double*[n];
b=new double [n];
for(i=0;i <n;i++)
{
a[i]=new double[n];
for(j=0;j <n;j++)
fscanf(fp, "%lf ",a[i]+j);
fscanf(fp, "%lf ",b+i);
}
fclose(fp);
for(i=0;i <n;i++)
{
for(j=0;j <n;j++)
printf( "%6.4f ",a[i][j]);
printf( " %6.4f\n ",b[i]);
}
double *x=new double[n];
for(i=0;i <n;i++)
x[i]=0;
int T=20;
jacobi(a,b,x,n,T);
gs(a,b,x,n,T);
return 0;
}
d:\solve.txt
3
4 3 0 24
3 4 -1 30
0 -1 4 -24
void jacobi(double **a,double *b,double *x,int n,int Times)
{
int i,j,t;
double *xx=new double[n];
for(t=0;t <Times;t++)
{
for(i=0;i <n;i++)
{
xx[i]=b[i];
for(j=0;j <n;j++)
if(j!=i)
xx[i]-=a[i][j]*x[j];
xx[i]/=a[i][i];
}
for(i=0;i <n;i++)
x[i]=xx[i];
}
delete xx;
printf( "jacobi:\t ");
for(i=0;i <n;i++)
printf( "%6.4f ",x[i]);
printf( "\n ");
}
void gs(double **a,double *b,double *x,int n,int Times)
{
int i,j,t;
for(t=0;t <Times;t++)
{
for(i=0;i <n;i++)
{
x[i]=b[i];
for(j=0;j <n;j++)
if(j!=i)
x[i]-=a[i][j]*x[j];
x[i]/=a[i][i];
}
}
printf( "gs:\t ");
for(i=0;i <n;i++)
printf( "%6.4f ",x[i]);
printf( "\n ");
}
int main(int argc, char* argv[])
{
int i,j,n;
double **a,*b;
FILE *fp;
fp=fopen( "d:\\solve.txt ", "r ");
fscanf(fp, "%d ",&n);
a=new double*[n];
b=new double [n];
for(i=0;i <n;i++)
{
a[i]=new double[n];
for(j=0;j <n;j++)
fscanf(fp, "%lf ",a[i]+j);
fscanf(fp, "%lf ",b+i);
}
fclose(fp);
for(i=0;i <n;i++)
{
for(j=0;j <n;j++)
printf( "%6.4f ",a[i][j]);
printf( " %6.4f\n ",b[i]);
}
double *x=new double[n];
for(i=0;i <n;i++)
x[i]=0;
int T=20;
jacobi(a,b,x,n,T);
gs(a,b,x,n,T);
return 0;
}
d:\solve.txt
3
4 3 0 24
3 4 -1 30
0 -1 4 -24
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-06-07
展开全部
我有一个实例可以给你·可能你要自己改一下·是开源的·但你 要先加分给我
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询