【问题标题】:Finding optimal weight factor for SOR寻找 SOR 的最佳权重因子
【发布时间】: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


【解决方案1】:

这很容易做到。只需循环遍历w 的值并确定每个w 的迭代总数。函数完成后,检查这是否是获得解决方案所需的当前最小迭代次数。如果是,则更新最终解决方案。一旦我们遍历所有w,结果将是产生最少迭代次数以收敛的解向量。请记住,SOR 具有 w,因此它包含 w = 0w = 20 &lt; w &lt; 2,因此我们不能在范围内包含 0 或 2。因此,请执行以下操作:

omega_vec = 0.01:0.01:1.99;
final_x = x0;
min_iter = intmax;

for w = omega_vec
    [x, iter] = SORtest(A, b, x0, TOL, w);
    if iter < min_iter
        min_iter = iter;
        final_x = x;
    end
end

循环检查每个w 的迭代总数是否小于当前最小值。如果是,请记录并记录解向量是什么。在所有w 中最小的最终解向量将存储在final_x 中。

【讨论】:

  • 当我尝试此代码时,它无法正常运行。它说“忙”,直到我终止它。
  • @user3681755 - 是的......那是因为您可能提供了一个解决方案不收敛的欧米茄。标准做法是限制迭代总数。如果超出此范围,则停止。将此作为您的 while 循环:while(l &lt;= 200),其中 200 是您要使用的最大迭代次数。
  • @user3681755 - 顺便说一句,您的代码中有错误。您想返回 l,而不是 i。您还希望 l0 开头。我已经编辑了您的代码以反映这一点。请检查它。
  • 完美。感谢您的所有帮助!
  • 我的荣幸!祝你好运!
猜你喜欢
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-08
  • 1970-01-01
  • 2013-07-01
  • 1970-01-01
相关资源
最近更新 更多