我正在尝试测试一个简单的一维泊松求解器,以表明有限差分方法与并且对输入函数使用延迟校正会产生与.
所以,方程是边界条件。我尝试使用的方法是使用离散化运算符
(示例矩阵是。)然后求解。我已经证明,理论上这应该与收敛,但是当我在 Matlab 上测试它时,我只得到收敛。
然后,我正在尝试我的课程讲师所说的“延迟更正”,并在解决之前我得出的结论是,更正应该是。我已经证明这应该与收敛,但在 Matlab 中我仍然得到。
这是 Matlab 脚本:
function [u err] = threeptsolve(ureal, du2, h)
% INPUT: 'ureal' is the function handle for the real solution.
% 'du2' is the function handle for the second derivative of 'ureal'
% 'h' is the step size
% OUTPUT: 'u' is the approximated solution
% 'err' is the error at each point
x = [0:h:1]';
n = length(x);
f = -du2(x);
realu = ureal(x);
A = 2 * eye(n);
A = A + diag(-1*ones(n-1,1), 1) + diag(-1*ones(n-1,1), -1);
A = (1/h^2) * A;
% uncomment if using deferred correction
% f = f + h^2/12 * A * f;
u = A\f;
err = (realu - u);
end
当我使用一些示例平滑函数(具有适当的边界值)尝试此操作,然后使用再次尝试时,我在计算时得到(近似)二的向量。err1 ./ err2(1:2:end)
我的数学错了,还是我的代码?