function [x, iter] = SOR(A, b, omega, max_iter, tol) n = length(b); x = zeros(n, 1); err = inf; k = 0; while err > tol && k < max_iter k = k + 1; x_old = x; for i = 1:n x(i) = (1 - omega) * x_old(i) + omega * (b(i) - A(i, 1:i-1) * x(1:i-1) - A(i, i+1:n) * x_old(i+1:n)) / A(i, i); end err = norm(x - x_old); end iter = k; end 出错 SORm (第 2 行) n = length(b);
时间: 2023-12-06 07:03:38 浏览: 139
SOR.rar_SOR_SOR解法_解矩阵方程组
看起来你在 SOR 函数中没有使用 function 关键字来定义函数,导致 MATLAB 无法识别该函数。你需要在 SOR 函数的开头加上 function 关键字,比如:
```matlab
function [x, iter] = SOR(A, b, omega, max_iter, tol)
n = length(b);
x = zeros(n, 1);
err = inf;
k = 0;
while err > tol && k < max_iter
k = k + 1;
x_old = x;
for i = 1:n
x(i) = (1 - omega) * x_old(i) + omega * (b(i) - A(i, 1:i-1) * x(1:i-1) - A(i, i+1:n) * x_old(i+1:n)) / A(i, i);
end
err = norm(x - x_old);
end
iter = k;
end
```
这样就可以成功定义 SOR 函数了。
阅读全文