【问题标题】:A problem in using Five-point difference format solve Poisson equation with MatlabMatlab中使用五点差分格式求解泊松方程的一个问题
【发布时间】:2020-06-22 03:48:10
【问题描述】:

问题集是这个泊松方程。 我想用五点差格式来解决它。但是我的代码有些问题。

enter image description here

代码

function F = fivepointdiff(~,n)
h=1/n;
N=2*(n-1)*n+(3*n-1)*(n-1);
XY=zeros(2,n);
for i = 1:n
    for j=1:n-1
        XY(:,(n-1)*(i-1)+j)=[1+j*h;i*h];
    end
end
for i =1:n-1
    for j=1:3*n-1
        XY(:,n*(n-1)+(3*n-1)*(i-1)+j)=[j*h;1+i*h];
    end
end
for i=1:n
    for j=1:n-1
        XY(:,n*(n-1)+(3*n-1)*(n-1)+(n-1)*(i-1)+j)=[1+j*h;2*1+(i-1)*h];
    end
end
A=zeros(N,N);
for i=1:N
    for j=1:N
        if(i==j)
            A(i,j)=4;
        else if(((XY(1,i)-XY(1,j))^2+(XY(2,i)-XY(2,j))^2)<2*h*h)
                A(i,j)=-1;
            end
        end
    end
end
f=zeros(N,1);
for i =1:N
    f(i,1)=h*h;
end
U=bicg(A,f,0,1,100);
F=[XY;U'];
                

当我运行它时,有一些错误

五点差异(1, 25)

警告:BICG 可能无法实现输入 tol 尝试使用更大的公差

在 bicg 中(第 104 行)

在五点差异中(第 35 行)

使用 bicg 时出错(第 135 行)

预处理器必须是一个大小为 2976 的方阵以匹配问题大小。

fivepointdiff 中的错误(第 35 行)

U=bicg(A,f,0,1,100);

【问题讨论】:

    标签: matlab math equation pde mathematical-expressions


    【解决方案1】:

    您以错误的方式使用函数 'bicg'。

    根据:https://www.mathworks.com/help/matlab/ref/bicg.html

    x = bicg(A,b,tol,maxit,M)
    
    • 第三个参数 'tol' 是公差,它不能等于 0,这是 您收到警告的原因。 (警告:BICG 可能无法实现输入 tol 使用更大的公差)
    • 'maxit' 是最大迭代次数,不符合逻辑 将其设置为 1。
    • 最后,M 应该是一个与 A 具有相同维度的矩阵,而不是 您指定的数字。使用此矩阵可以改善 问题和计算的效率及其使用是可选的。

    所以,你需要修改函数参数,可能是这样的:

    x = bicg(A,b,1e-6,1000)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多