?? fem2.cpp
字號(hào):
#include <iostream.h>
#include <math.h>
#include<stdlib.h>
#include<stdio.h>
void print(char *fn,double aa[],int mm,int nn)
{
FILE *cfPtr;
int i,j;
if((cfPtr=fopen(fn,"w+"))==NULL)
printf("File could not be opened\n");
else
{
for(i=0;i<mm;i++){
for (j=0;j<nn;j++)
{
fprintf(cfPtr,"%-f ",aa[i*nn+j]);
//fprintf(cfPtr,"%-f ", aa[i*n+j]);
if ((i*nn+j+1)%nn==0)
{fprintf(cfPtr,"\n");}
}
}
}
fclose(cfPtr);
}
main()
{
double BD=0,phi=0,r=0,t=0,u=0;
static int m=100,n=100;
int i,j;
double w=1.0E-3;
double omega=2-3.1415926*
sqrt(2*(1/((m-1)*(m-1))+1/((n-1)*(n-1))));
//double leigh=0.1,heigh=0.1;
//double hv=heigh/n;
//double hh=leigh/m;
double *a;
a= new double[m*n];
/////本段將邊界條件賦給數(shù)組
for (j=0;j<=n-1;j++)
{
a[j]=0;
}
for (j=0;j<=n-1;j++)
{
a[(m-1)*n+j]=10;
}
for (i=1;i<m-1;i++)
{
a[i*n]=0;
a[i*n+n-1]=0;
}
int Times=0;
///本段用超松弛迭代法計(jì)算
do {
Times=Times+1;
for (i=1;i<m-1;i++)
{
for (j=1;j<n-1;j++)
{
t=a[i*n+j];
phi=(a[(i+1)*n+j]+a[i*n+(j+1)]+a[(i-1)*n+j]+
a[i*n+(j-1)])/4;
a[i*n+j]=t+omega*(phi-t)/4;
r=fabs(a[i*n+j]-t);
//cout<<"The value of r is "<<r<<endl;
if ((r-u)>=0){
u=r;
}
if ((r/a[i*n+j])<=w)
continue;
}
}
}
while (r>w);
/////本段在屏幕上輸出計(jì)算結(jié)果和迭代次數(shù)
/*for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
int ttt=i*n+j;
cout<<"a["<<ttt+1<<"]="<<a[ttt]<<endl;
}
}*/
cout<<"The times of recicle are "<<Times<<endl;
//////下面語句將計(jì)算結(jié)果輸出到y(tǒng)usong.dat的文件中
print("yusong.dat",a,m,n);
delete []a;
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -