如何让这个功能更快地运行?

时间:2015-03-23 20:54:25

标签: matlab matrix solver

我编写了这段代码,使用jacobi迭代解决了带状矩阵。它是一个改进的Jacobi函数,经过优化和#34;在带状矩阵上更快地工作。我已成功修改它只在矩阵中计算非零项,但它运行速度比原始代码慢得多。如何让它更有效地运行?

原始代码:

while (true)
iter = iter+1;
for r=1:m                           % looping through all rows of A
    sum = 0;                        % setting sum equal to 0
    for c=1:n                       % looping through all columns of A
        if r~=c                     % if the r does not equal c, meaning the current elements are not diagonals...
            sum = sum + A(r,c)*x0(c);% set sum = sum plus current element times cth element of x
        end

    x(r) = (-sum + b(r))/A(r,r);    % if r and c are diagonals, the x value at that element is -sum + b(r) divided by that element of A

    end
end
if abs(norm(x) - norm(x0)) < TOL    % checking tolerance
    break
end
x0 = x;                             % setting x0 = x before relooping
end

带状代码:

while (true)
%     iter = iter+1; 
for r=1:m                           % looping through all rows of A  
  for i = 1:hi                      % looping through the section of A at the beginning that does not have a complete "low"
      sum = 0;                      % assigning sum = 0 for each loop
    for r = i                       % looping through rows of first section
       for c = 1:r+hi;              % looping through only columns that go from the first column to hi after the diagona
        if r~=c                     % if the r does not equal c, meaning the current elements are not diagonals...
            sum = sum + A(r,c)*x0(c);% set sum = sum plus current element times cth element of x
          end
    x(r) = (-sum + b(r))/A(r,r);    % solving the row
       end

    end
  end
  for i = 1+hi:n-hi                 % looping through second section (section with a complete "low" and "hi" for each diagonal
      sum = 0;                      % setting sum = 0 for each loop
    for r = i                       % looping through each row of second section
       for c = r+low:r+hi;          % looping through only columns of current row that are in the bandwidth
        if r~=c                     % if the r does not equal c, meaning the current elements are not diagonals...
            sum = sum + A(r,c)*x0(c);% set sum = sum plus current element times cth element of x
        end

    x(r) = (-sum + b(r))/A(r,r);     % solving current row
       end

      end
  end
  for i = n+low+1:n                 % looping through final section of A (section with out a complete "hi" for each diagonal
      sum = 0;                      % setting sum = 0 for each loop
    for r = i                       % looping through each row of final section
       for c = r + low:n;           % looping through only columns of current row that are from "low" away from the diagonal to the end of the row
        if r~=c                     % if the r does not equal c, meaning the current elements are not diagonals...
            sum = sum + A(r,c)*x0(c);% set sum = sum plus current element times cth element of x
        end   
    x(r) = (-sum + b(r))/A(r,r);    % solving current row
       end   
    end
  end
end
if abs(norm(x) - norm(x0)) < TOL    % checking tolerance
    break
end 
   x0 = x;                              % assigning x0 as the new estimate from the previous attempt, x
end
sln = x0;                               % assigning output variable

我对其所做的更改将其分为3个部分(1个部分用于没有完整&#34;低&#34;以及&#34; hi&#34;在开始时,结尾处为1,对于中间部分为1。然后我将循环改为从低处开始并转到hi。为什么这个函数需要这么长时间才能运行?大约需要一分钟对于20x20矩阵。

0 个答案:

没有答案
相关问题