matlab用雅可比迭代法求解线性方程组的代码
时间: 2023-10-31 07:07:31 浏览: 252
jacobi迭代法解线性方程组,matlab代码
4星 · 用户满意度95%
以下是使用雅可比迭代法求解线性方程组的 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
```
阅读全文