
其次,jacobi从来没有保证收敛。实际上,jacobi对任意初值x0都收敛的充要条件是D^{-1}(D-A)所有特征值的绝对值都小于1。这里的hilbert矩阵好像不符合这个条件。
话说MATLAB程序写成这样我也是醉了。。。MATLAB自带很多矩阵的函数和运算符的啊,比如*可以是矩阵乘法,A\b直接解出Ax=b,diag()可以搞出矩阵的对角线部分,完全不用写那么多for的啊,看得我头疼。。。
function [x, error, iter, flag] = jacobi(A, x, b, max_it, tol)% jacobi.m solves the linear system Ax=b using the Jacobi Method.
%
% input AREAL matrix
% xREAL initial guess vector
% bREAL right hand side vector
% max_it INTEGER maximum number of iterations
% tol REAL error tolerance
%
% output xREAL solution vector
% errorREAL error norm
% iter INTEGER number of iterations performed
% flag INTEGER: 0 = solution found to tolerance
% 1 = no convergence given max_it
iter = 0 % initialization
flag = 0
bnrm2 = norm( b )
if ( bnrm2 == 0.0 ), bnrm2 = 1.0end
r = b - A*x
error = norm( r ) / bnrm2
if ( error <好棚 tol ) return, end
[m,n]=size(A)
[ M, N ] = split( A , b, 1.0, 1 ) % matrix splitting
for iter = 1:max_it % begin iteration
x_1 = x
x = M \ (N*x + b) % update approximation
error = norm( x - x_1 ) /唤袜做 norm( x ) % compute error
if ( error <和衡= tol )
break
end % check convergence
end
if ( error >tol )
flag = 1
end % no convergence
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)