当前位置:文档之家› 工程电磁场上机实验

工程电磁场上机实验

for(int i=0; i<=ROW-1; i++) {
cout<<"Row"<<i<<":"<<endl; for(int j=0,k=1; j<=COL-1; j++,k++) {
//printf("%lf", phi[i][j]); //printf(" "); cout<<setw(10)<<setiosflags(ios::fixed)<<setprecision(4)<<phi[i][j]<<" "; if(k%5==0)
}
void Copy(double phi[ROW][COL], double phi2[ROW][COL]) {
for(int i=0; i<=ROW-1; i++) {
for(int j=0; j<=COL-1; j++) {
phi2[i][j]=phi[i][j]; } } }
int Check(double phi[ROW][COL], double phi2[ROW][COL]) {
} } num++; }while(Check(phi, phi2)==1);
Print(phi); cout<<"迭代次数:"<<num;
cout<<endl<<endl<<"phi[10][20]="<<phi[10][20]<<endl<<"phi[11][21]="<<phi[11][ 21]<<endl;
int num=0; do{
Copy(phi, phi2); for(int i=1; i<ROW-1; i++) { for(int j=1; j<COL-1; j++) { phi[i][j]=phi[i][j]+(phi[i+1][j]+phi[i][j+1]+phi[i-1][j]+phi[i][j-1ain(void) {
double a=1.0; double phi[ROW][COL]; double phi2[ROW][COL]; Init(phi); Copy(phi, phi2);
cout<<endl<<"请输入加速收敛因子 a(输入 a=0 时退出):"<<endl; cin>>a; while(a!=0) {
cout<<endl<<"请输入加速收敛因子 a(输入 a=0 时退出):"<<endl; cin>>a; Init(phi); Copy(phi, phi2); }
return 0; }
void Init(double phi[ROW][COL]) {
for(int i=0; i<=ROW-1; i++) {
int f=0; for(int i=0; i<=ROW-1; i++) {
for(int j=0; j<=COL-1; j++)
{ if(abs(phi[i][j]-phi2[i][j])>ERROR) { f=1; return f; }
} } return f; }
void Print(double phi[ROW][COL]) {
cout<<endl; } //printf("\n"); cout<<endl; } //printf("\n"); cout<<endl; }
实验结果:
for(int j=0; j<=COL-1; j++) {
phi[i][j]=0; } } for(int i=0; i<=ROW-1; i++) { phi[i][0]=0; phi[i][COL-1]=0; } for(int i=0; i<=COL-1; i++) { phi[0][i]=0; phi[ROW-1][i]=100; }
using namespace std;
#define COL 41 #define ROW 21 #define ERROR 0.001
void Init(double phi[ROW][COL]); void Copy(double phi[ROW][COL], double phi2[ROW][COL]); int Check(double phi[ROW][COL], double phi2[ROW][COL]); void Print(double phi[ROW][COL]);
工程电磁场上机实验
题目:设有一金属矩形槽,其边长分别为 a=40,b=20,其侧壁与底面电位均为
零,顶盖电位为 100V,如下图所示,试编程求槽内电位分布。
y
b
100V
GND1
ax
图 1 金属矩形槽及其电位分布
解:利用有限元法,用 C++语言编程。 C++代码: #include <iostream> #include <cmath> #include <iomanip> //#include <fstream> //#include <stdbool.h>
相关主题