我正在尝试求解一维泊松方程,以模拟电容器电介质中 10 微米间隔的电势。我遇到的主要问题是红色/黑色 Gauss-Seidel 代码似乎并没有真正更新潜在矩阵中的值,因此不会收敛。
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float x=0;
bool exit = false;
float V [11] = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
float Q [11];
// set up q matrix
for (int i = 0; i <= 10; i++)
{
Q[i] = 50*sin(3.14*x/0.00001);
x += 0.00001;
}
while(!exit)
{
if(V[10] <= 3.29)
{
// red sweep
for(int i=1; i <= 10; i+=2)
{
V[i] = -0.5*(0.00001*Q[i]-V[i-1]-V[i+1]);
}
//black sweep
for(int j=2; j <= 10; j+=2)
{
V[j] = -0.5*(0.00001*Q[j]-V[j-1]-V[j+1]);
}
}
else exit=true;
}
for(int i=0;i<=10;i++)
{
cout << V[i] << endl;
}
return 0;
}