- CPS小天才
-
x = zeros(size(b)); %初始解设置为与b同型的零向量
k = 0; %迭代次数的记数变量,初始量设为0
r = 1; %前后项之差的无穷范数
% % % % % % % % % % % % % % % %
D = diag(diag(A));
B = inv(D)*(D-A);
f = inv(D)*b;
% % % % % % % % % % % % % % % %
p = max(abs(eig(B))); %谱半径大于等于1就不收敛
if p >= 1
"迭代法不收敛"
return
end
while r >e
x0 = x;
x = B*x0 + f;
k = k + 1;
r = norm (x-x0,inf);
end
"所求解为"
x
"迭代次数为"
k
自己以前编的。。。。
- 床单格子
-
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 A REAL matrix
% x REAL initial guess vector
% b REAL right hand side vector
% max_it INTEGER maximum number of iterations
% tol REAL error tolerance
%
% output x REAL solution vector
% error REAL 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.0; end
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
% END jacobi.m