永发信息网

用matlab把雅可比迭代的程序写出来

答案:1  悬赏:30  手机版
解决时间 2021-06-02 18:13
  • 提问者网友:我的未来我做主
  • 2021-06-02 08:00
用matlab把雅可比迭代的程序写出来
最佳答案
  • 五星知识达人网友:春色三分
  • 2021-06-02 08:16
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
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯