jacobi迭代法程序问题matlab

jacobi迭代法程序问题matlab,第1张

首先,程序好像闭银写错了。我洞态芹记得用jacobi解Ax=b的算法是:记D为A的对角线部分纳毕,每次循环令x^{n+1}=D^{-1}((D-A)x^{n}+b)。b是不会变的,而你程序里的b一直在变。

其次,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


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12243804.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-22
下一篇2023-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存