【发布时间】:2015-04-12 11:14:35
【问题描述】:
我正在使用 SOR 方法,需要找到最佳权重因子。我认为解决此问题的一个好方法是使用从 0 到 2 的多个 omega 运行我的 SOR 代码,然后存储每个的迭代次数。然后我可以看到哪个迭代是最低的,它对应的是哪个欧米茄。然而,作为一个新手程序员,我不确定如何去做。
这是我的 SOR 代码:
function [x, l] = SORtest(A, b, x0, TOL,w)
[m n] = size(A); % assigning m and n to number of rows and columns of A
l = 0; % counter variable
x = [0;0;0]; % introducing solution matrix
max_iter = 200;
while (l < max_iter) % loop until max # of iters.
l = l + 1; % increasing counter variable
for i=1:m % looping through rows of A
sum1 = 0; sum2 = 0; % intoducing sum1 and sum2
for j=1:i-1 % looping through columns
sum1 = sum1 + A(i,j)*x(j); % computing sum using x
end
for j=i+1:n
sum2 = sum2 + A(i,j)*x0(j); % computing sum using more recent values in x0
end
x(i) =(1-w)*x0(i) + w*(-sum1-sum2+b(i))/A(i,i); % assigning elements to the solution matrix.
end
if abs(norm(x) - norm(x0)) < TOL % checking tolerance
break
end
x0 = x; % assigning x to x0 before relooping
end
end
【问题讨论】:
-
如果 omega 为 1,您将如何运行您的代码?如果您能回答这个问题,那么请考虑如何使用 for 循环逐步更改 onega 并重新运行您的函数。
-
如果欧米茄是一种,那它就是高斯赛德尔法
标签: matlab matrix numerical-methods