需要帮助循环使用连续过度松弛方法的不同公差值

  • 本文关键字:方法 循环 帮助 连续 matlab
  • 更新时间 :
  • 英文 :


我为逐次超松弛方法编写了以下代码,其中它给出了为获得某个容差值的答案所需的迭代次数。

我想帮助调整这个代码,这样我就可以得到不同公差值的答案,即[10^-4,10^-6,10^-8,10^-10,10^-12]

我知道我需要在现有的while循环之外放一个for循环,但我不知道怎么做。有人能帮我做这个吗?

N=100;
ID = 30177207;% ENTER YOUR STUDENT ID HERE
[A,b]= set_A(ID,N);
xnew=linspace(0,0,length(A))';
n=size(xnew,1);
normVal=Inf;
nmax=1000; %number of maximum iterations which can be reached%
tol= 10^-4; % Tolerence for method%
iter=0;
omega = 1.7;
while normVal>tol && iter<nmax
x_old=xnew;

for i=1:n

sum1 = 0;
sum2 = 0;
for j=1:i-1  % sum the terms involving "new" values
sum1 = sum1 + A(i,j)*xnew(j);
end
for j=i+1:n    % sum the terms involving "old" values
sum2 = sum2 + A(i,j)*x_old(j);
end

xnew(i) = (b(i) - sum1 - sum2)/A(i,i); % new value of x(i)
xnew = (1-omega)+ omega*xnew; %relaxation, so for omega = 1 it will give same answer as the gauss seidel method
end
iter=iter+1;
normVal=norm(x_old-xnew);
end
fprintf('Solution of the system is in %d iterations',iter);

我无法运行您的代码,但由于您有一个在normVal > tol时运行的迭代方法,您可以做的是迭代tol的递减值,每次离开while循环时,您都会在给定的容差内得到解决方案,可能您对不同的容差有相同的解决方案。


N=100;
ID = 30177207;% ENTER YOUR STUDENT ID HERE
[A,b]= set_A(ID,N);
xnew=linspace(0,0,length(A))';
n=size(xnew,1);
normVal=Inf;
nmax=1000; %number of maximum iterations which can be reached%
tol= 10^-4; % Tolerence for method%
iter=0;
omega = 1.7;
for tol = [10^-4, 10^-6, 10^-8, 10^-10, 10^-12]
while normVal>tol && iter<nmax
x_old=xnew;

for i=1:n

sum1 = 0;
sum2 = 0;
for j=1:i-1  % sum the terms involving "new" values
sum1 = sum1 + A(i,j)*xnew(j);
end
for j=i+1:n    % sum the terms involving "old" values
sum2 = sum2 + A(i,j)*x_old(j);
end

xnew(i) = (b(i) - sum1 - sum2)/A(i,i); % new value of x(i)
xnew = (1-omega)+ omega*xnew; %relaxation, so for omega = 1 it will give same answer as the gauss seidel method
end
iter=iter+1;
normVal=norm(x_old-xnew);
end
fprintf('here you have a solution for tol = %g', tol);
end

相关内容

  • 没有找到相关文章

最新更新