·急求!我的C语言代码运行不出结果,谁能帮帮我?
谁能帮我运行出结果啊,谢谢了!!/*------------子函数程序--------------*/#include<stdio.h>#include<stdlib.h...
谁能帮我运行出结果啊,谢谢了!!
/*------------子函数程序--------------*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <math.h>
#ifndef EPSILON
#define EPSILON 1.0e-10
#endif
int sor(double A[DIM][DIM],double b[DIM],double x[DIM],double omiga)
{
static int first=0;
int i,j,k;
static double x1[DIM],norm;
norm=0.0;
if(first==0)
{
randomize();
for(i=0;i<DIM;i++)
{x[i]=rand();norm=norm+x[i]*x[i];}
norm=sqrt(norm);
for(i=0;i<DIM;i++)x[i]=x[i]/norm;
first=1;
}
for(k=0;k<MI;k++)
{
for(i=0;i<DIM;i++)
{
x1[i]=b[i];
for(j=0;j<i;j++) x1[i]=x1[i]-A[i][j]*x[j];
for(j=i;j<DIM;j++) x1[i]=x1[i]-A[i][j]*x[j];
x1[i]=x1[i]+omiga*x1[i]/A[i][j];
}
norm=0.0;
for(i=0;i<DIM;i++)norm=norm+(x1[i]-x[i])*(x1[i]-x[i]);
norm=sqrt(norm);
for(i=0;i<DIM;i++)x[i]=x1[i];
if(norm<EPSILON)goto endd;
}
endd:return(k);
}
/*------------主函数程序--------------*/
/*sorm.c:successive overrelaxation method for linear systems*/
#define DIM 4
#define MI 1000
#include "sor.c"
extern int sor(double A[DIM][DIM],double b[DIM],double x[DIM],double omiga);
void main(void)
{
int i,itime,iter;
char ifdoyn;
static double A[DIM][DIM]={{0.78,-0.02,-0.12,-0.14},
{-0.02,0.86,-0.04,0.06},{-0.12,-0.04,0.72,-0.08},{-0.14,0.06,-0.08,0.74}};
static double x[DIM],b[DIM]={0.76,0.08,1.12,0.68};
system("cls");
itime=0;
omiga=1.03;
printf("SOR iterative method:\n");
again:iter=sor(A,b,x,omiga);
itime=itime+iter;
if(iter<MI)itime=itime+1;
printf("Iterative %3d times.\n",itime);
for(i=0;i<DIM;i++)printf("%15.10f\n",x[i]);
if(iter<MI)
{
printf("Success!\n");
getch();
}
else
{
printf("IF iterative again?(y/n)\n");
ifdoyn=getch();
if(ifdoyn=='y'||ifdoyn=='Y')goto again;
}
}
这个的答案是:迭代次数10次。
我不太懂啊,那应该怎么修改才能运行出来啊??
那就是没办法运行出来了吗??这个是数学中的SOR迭代法的算法,做论文用的,急死了:(
你的这个是用什么软件运行的啊?我怎么还是运行不出来呢? 展开
/*------------子函数程序--------------*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <math.h>
#ifndef EPSILON
#define EPSILON 1.0e-10
#endif
int sor(double A[DIM][DIM],double b[DIM],double x[DIM],double omiga)
{
static int first=0;
int i,j,k;
static double x1[DIM],norm;
norm=0.0;
if(first==0)
{
randomize();
for(i=0;i<DIM;i++)
{x[i]=rand();norm=norm+x[i]*x[i];}
norm=sqrt(norm);
for(i=0;i<DIM;i++)x[i]=x[i]/norm;
first=1;
}
for(k=0;k<MI;k++)
{
for(i=0;i<DIM;i++)
{
x1[i]=b[i];
for(j=0;j<i;j++) x1[i]=x1[i]-A[i][j]*x[j];
for(j=i;j<DIM;j++) x1[i]=x1[i]-A[i][j]*x[j];
x1[i]=x1[i]+omiga*x1[i]/A[i][j];
}
norm=0.0;
for(i=0;i<DIM;i++)norm=norm+(x1[i]-x[i])*(x1[i]-x[i]);
norm=sqrt(norm);
for(i=0;i<DIM;i++)x[i]=x1[i];
if(norm<EPSILON)goto endd;
}
endd:return(k);
}
/*------------主函数程序--------------*/
/*sorm.c:successive overrelaxation method for linear systems*/
#define DIM 4
#define MI 1000
#include "sor.c"
extern int sor(double A[DIM][DIM],double b[DIM],double x[DIM],double omiga);
void main(void)
{
int i,itime,iter;
char ifdoyn;
static double A[DIM][DIM]={{0.78,-0.02,-0.12,-0.14},
{-0.02,0.86,-0.04,0.06},{-0.12,-0.04,0.72,-0.08},{-0.14,0.06,-0.08,0.74}};
static double x[DIM],b[DIM]={0.76,0.08,1.12,0.68};
system("cls");
itime=0;
omiga=1.03;
printf("SOR iterative method:\n");
again:iter=sor(A,b,x,omiga);
itime=itime+iter;
if(iter<MI)itime=itime+1;
printf("Iterative %3d times.\n",itime);
for(i=0;i<DIM;i++)printf("%15.10f\n",x[i]);
if(iter<MI)
{
printf("Success!\n");
getch();
}
else
{
printf("IF iterative again?(y/n)\n");
ifdoyn=getch();
if(ifdoyn=='y'||ifdoyn=='Y')goto again;
}
}
这个的答案是:迭代次数10次。
我不太懂啊,那应该怎么修改才能运行出来啊??
那就是没办法运行出来了吗??这个是数学中的SOR迭代法的算法,做论文用的,急死了:(
你的这个是用什么软件运行的啊?我怎么还是运行不出来呢? 展开
3个回答
展开全部
运行倒是能运行了,结果好像还不对
/*------------子函数程序--------------*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <math.h>
#ifndef EPSILON
#define EPSILON 1.0e-10
#endif
#define DIM 4
#define MI 1000
int sor(double A[DIM][DIM],double b[DIM],double x[DIM],double omiga)
{
static int first=0;
static double x1[DIM],norm;
int i,j,k;
norm=0.0;
srand(time(0));
if(first==0)
{
for(i=0;i<DIM;i++)
{
x[i]=rand();
norm=norm+x[i]*x[i];
}
norm=sqrt(norm);
for(i=0;i<DIM;i++)
{
x[i]=x[i]/norm;
}
first=1;
}
for(k=0;k<MI;k++)
{
for(i=0;i<DIM;i++)
{
x1[i]=b[i];
for(j=0;j<i;j++)
{
x1[i]=x1[i]-A[i][j]*x[j];
}
for(j=i;j<DIM;j++)
{
x1[i]=x1[i]-A[i][j]*x[j];
}
x1[i]=x1[i]+omiga*x1[i]/A[i][j];
}
norm=0.0;
for(i=0;i<DIM;i++)
{
norm=norm+(x1[i]-x[i])*(x1[i]-x[i]);
}
norm=sqrt(norm);
for(i=0;i<DIM;i++)
{
x[i]=x1[i];
}
if(norm<EPSILON)
{
goto endd;
}
}
endd:
return(k);
}
/*------------主函数程序--------------*/
/*sorm.c:successive overrelaxation method for linear systems*/
void main(void)
{
static double A[DIM][DIM]=
{{0.78,-0.02,-0.12,-0.14},
{-0.02,0.86,-0.04,0.06},
{-0.12,-0.04,0.72,-0.08},
{-0.14,0.06,-0.08,0.74}};
static double x[DIM],b[DIM]=
{0.76,0.08,1.12,0.68};
double omiga=1.03;
int i,itime,iter;
char ifdoyn;
system("cls");
itime=0;
printf("SOR iterative method:\n");
again:
iter=sor(A,b,x,omiga);
itime=itime+iter;
if(iter<MI)itime=itime+1;
{
printf("Iterative %3d times.\n",itime);
}
for(i=0;i<DIM;i++)
{
printf("%15.10f\n",x[i]);
}
if(iter<MI)
{
printf("Success!\n");
getch();
}
else
{
printf("IF iterative again?(y/n)\n");
ifdoyn=getch();
if(ifdoyn=='y'||ifdoyn=='Y')
{
goto again;
}
}
}
/*------------子函数程序--------------*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <math.h>
#ifndef EPSILON
#define EPSILON 1.0e-10
#endif
#define DIM 4
#define MI 1000
int sor(double A[DIM][DIM],double b[DIM],double x[DIM],double omiga)
{
static int first=0;
static double x1[DIM],norm;
int i,j,k;
norm=0.0;
srand(time(0));
if(first==0)
{
for(i=0;i<DIM;i++)
{
x[i]=rand();
norm=norm+x[i]*x[i];
}
norm=sqrt(norm);
for(i=0;i<DIM;i++)
{
x[i]=x[i]/norm;
}
first=1;
}
for(k=0;k<MI;k++)
{
for(i=0;i<DIM;i++)
{
x1[i]=b[i];
for(j=0;j<i;j++)
{
x1[i]=x1[i]-A[i][j]*x[j];
}
for(j=i;j<DIM;j++)
{
x1[i]=x1[i]-A[i][j]*x[j];
}
x1[i]=x1[i]+omiga*x1[i]/A[i][j];
}
norm=0.0;
for(i=0;i<DIM;i++)
{
norm=norm+(x1[i]-x[i])*(x1[i]-x[i]);
}
norm=sqrt(norm);
for(i=0;i<DIM;i++)
{
x[i]=x1[i];
}
if(norm<EPSILON)
{
goto endd;
}
}
endd:
return(k);
}
/*------------主函数程序--------------*/
/*sorm.c:successive overrelaxation method for linear systems*/
void main(void)
{
static double A[DIM][DIM]=
{{0.78,-0.02,-0.12,-0.14},
{-0.02,0.86,-0.04,0.06},
{-0.12,-0.04,0.72,-0.08},
{-0.14,0.06,-0.08,0.74}};
static double x[DIM],b[DIM]=
{0.76,0.08,1.12,0.68};
double omiga=1.03;
int i,itime,iter;
char ifdoyn;
system("cls");
itime=0;
printf("SOR iterative method:\n");
again:
iter=sor(A,b,x,omiga);
itime=itime+iter;
if(iter<MI)itime=itime+1;
{
printf("Iterative %3d times.\n",itime);
}
for(i=0;i<DIM;i++)
{
printf("%15.10f\n",x[i]);
}
if(iter<MI)
{
printf("Success!\n");
getch();
}
else
{
printf("IF iterative again?(y/n)\n");
ifdoyn=getch();
if(ifdoyn=='y'||ifdoyn=='Y')
{
goto again;
}
}
}
2008-05-31
展开全部
/* gseidel.c */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <math.h>
#ifndef EPSILON
#define EPSILON 1.0e-10
#endif
#define DIM 4
#define MI 1000
int gseidel(double A[DIM][DIM],double b[DIM],double x[DIM])
{
static int first=0;
int i,j,k;
static double x1[DIM],norm;
norm=0.0;
if(first==0)
{
randomize();
for(i=0;i<DIM;i++)
{x[i]=rand();norm=norm+x[i]*x[i];}
norm=sqrt(norm);
for(i=0;i<DIM;i++)x[i]=x[i]/norm;
first=1;
}
for(k=0;k<MI;k++)
{
for(i=0;i<DIM;i++)
{
x1[i]=b[i];
for(j=0;j<i;j++) x1[i]=x1[i]-A[i][j]*x[j];
for(j=i+1;j<DIM;j++) x1[i]=x1[i]-A[i][j]*x[j];
x1[i]=x1[i]/A[i][i];
}
norm=0.0;
for(i=0;i<DIM;i++)norm=norm+(x1[i]-x[i])*(x1[i]-x[i]);
norm=sqrt(norm);
for(i=0;i<DIM;i++)x[i]=x1[i];
if(norm<EPSILON)goto endd;
}
endd:return(k);
}
/* main.c */
#include "gseidel.c"
void main(void)
{
int i,itime,iter;
char ifdoyn;
static double A[DIM][DIM]={{0.78,-0.02,-0.12,-0.14},
{-0.02,0.86,-0.04,0.06},{-0.12,-0.04,0.72,-0.08},{-0.14,0.06,-0.08,0.74}};
static double x[DIM],b[DIM]={0.76,0.08,1.12,0.68};
system("cls");
itime=0;
x[0]=4.0;
x[1]=6.0;
x[2]=8.0;
printf("Gauss-Seidel iterative method:\n");
again:iter=gseidel(A,b,x);
itime=itime+iter;
if(iter<MI)itime=itime+1;
printf("Iterative %3d times.\n",itime);
for(i=0;i<DIM;i++)printf("%15.10f\n",x[i]);
if(iter<MI)
{
printf("Success!\n");
getch();
}
else
{
printf("If iterative again?(y/n)\n");
ifdoyn=getch();
if(ifdoyn=='y'||ifdoyn=='Y')goto again;
}
}
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <math.h>
#ifndef EPSILON
#define EPSILON 1.0e-10
#endif
#define DIM 4
#define MI 1000
int gseidel(double A[DIM][DIM],double b[DIM],double x[DIM])
{
static int first=0;
int i,j,k;
static double x1[DIM],norm;
norm=0.0;
if(first==0)
{
randomize();
for(i=0;i<DIM;i++)
{x[i]=rand();norm=norm+x[i]*x[i];}
norm=sqrt(norm);
for(i=0;i<DIM;i++)x[i]=x[i]/norm;
first=1;
}
for(k=0;k<MI;k++)
{
for(i=0;i<DIM;i++)
{
x1[i]=b[i];
for(j=0;j<i;j++) x1[i]=x1[i]-A[i][j]*x[j];
for(j=i+1;j<DIM;j++) x1[i]=x1[i]-A[i][j]*x[j];
x1[i]=x1[i]/A[i][i];
}
norm=0.0;
for(i=0;i<DIM;i++)norm=norm+(x1[i]-x[i])*(x1[i]-x[i]);
norm=sqrt(norm);
for(i=0;i<DIM;i++)x[i]=x1[i];
if(norm<EPSILON)goto endd;
}
endd:return(k);
}
/* main.c */
#include "gseidel.c"
void main(void)
{
int i,itime,iter;
char ifdoyn;
static double A[DIM][DIM]={{0.78,-0.02,-0.12,-0.14},
{-0.02,0.86,-0.04,0.06},{-0.12,-0.04,0.72,-0.08},{-0.14,0.06,-0.08,0.74}};
static double x[DIM],b[DIM]={0.76,0.08,1.12,0.68};
system("cls");
itime=0;
x[0]=4.0;
x[1]=6.0;
x[2]=8.0;
printf("Gauss-Seidel iterative method:\n");
again:iter=gseidel(A,b,x);
itime=itime+iter;
if(iter<MI)itime=itime+1;
printf("Iterative %3d times.\n",itime);
for(i=0;i<DIM;i++)printf("%15.10f\n",x[i]);
if(iter<MI)
{
printf("Success!\n");
getch();
}
else
{
printf("If iterative again?(y/n)\n");
ifdoyn=getch();
if(ifdoyn=='y'||ifdoyn=='Y')goto again;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2008-05-30
展开全部
norm越界了,用long double也会越界,请检查这一段程序的算法。
******************************************
这一段:
static double x1[DIM],norm;
norm=0.0;
if(first==0)
{
randomize();
for(i=0;i<DIM;i++)
{x[i]=rand();norm=norm+x[i]*x[i];}/*norm会溢出,而且即使将norm定义为long double也还会溢出*/
norm=sqrt(norm);
******************************************
这一段:
static double x1[DIM],norm;
norm=0.0;
if(first==0)
{
randomize();
for(i=0;i<DIM;i++)
{x[i]=rand();norm=norm+x[i]*x[i];}/*norm会溢出,而且即使将norm定义为long double也还会溢出*/
norm=sqrt(norm);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询