matlab用雅可比迭代法求解线性方程组代码
时间: 2023-10-24 20:08:33 浏览: 392
Jacobi迭代法求解Ax=b方程组数值解MATLAB源代码
5星 · 资源好评率100%
以下是使用雅可比迭代法求解线性方程组的 MATLAB 代码:
```matlab
function [x, iter] = jacobi(A, b, x0, tol, maxiter)
% Jacobi iteration method for solving linear equations Ax = b
% A: coefficient matrix
% b: constant vector
% x0: initial guess
% tol: tolerance
% maxiter: maximum number of iterations
n = length(b);
x = x0;
iter = 0;
while iter < maxiter
x_new = zeros(n, 1);
for i = 1:n
x_new(i) = (b(i) - A(i,1:i-1)*x(1:i-1) - A(i,i+1:n)*x(i+1:n)) / A(i,i);
end
if norm(x_new - x) < tol
break;
end
x = x_new;
iter = iter + 1;
end
end
```
阅读全文