Printing an error message if jacobi's method, does not converges.
古いコメントを表示
Hi, so I want to print an error message if my jacobi's method does not converge. I've made it so it Converges but dont know how to code the part where it prints if it doesnt.
% Accepts Inputs from the User's Matrix A and Vector B
a = input('Matrix A: ');
b = input('Vector B: ');
% Initial Guess
P = [0;0;0];
% Number of iterations
I = 10; % Max Iterations
L = length(b);
Z = zeros(L,1);
itc = 0; %Counts Number of Iterations
% Convergence Tolerance
t = 0.0001;
% Code
while n0 <= I & itc <10
for i = 1:L
Z(i) = (b(i)/a(i,i)) - (a(i,[1:i-1,i+1:L])*P([1:i-1,i+1:L]))/a(i,i); %Jacobi's Method
end
itc = 1+ itc;
fprintf (' %d Iteration \n',itc);
Z
% So ABS of X - Guess Vector?
if abs (Z-P) < t % if norm(r) < some tolerance , it is converged
break
end
P = Z;
end
採用された回答
その他の回答 (1 件)
Sulaymon Eshkabilov
2021 年 5 月 28 日
Hi,
Here is an easy solution:
...
if abs (Z-P) < t % if norm(r) < some tolerance , it is converged
break
else
error('No convergence achieved!')
end
P = Z;
end
...
3 件のコメント
Mustafa Ali
2021 年 5 月 28 日
Sulaymon Eshkabilov
2021 年 5 月 28 日
編集済み: Sulaymon Eshkabilov
2021 年 5 月 28 日
The above proposed code works for convergence t = 0.0001. Test your example with tighter convergence, i.e. t = 0.00000001 or sth like that and you will see the ERROR message.
If you wish to set up with the interation number then
...
t = 0.00001; % Convergence
while abs (Z-P) < t % convergence check
for i = 1:L
Z(i) = (b(i)/a(i,i)) - (a(i,[1:i-1,i+1:L])*P([1:i-1,i+1:L]))/a(i,i); %Jacobi's Method
end
itc = 1+ itc;
fprintf (' %d Iteration \n',itc);
Z
if itc > 10 % if norm(r) < some tolerance , it is converged
error('No convergence achieved!')
end
P = Z;
end
Mustafa Ali
2021 年 5 月 28 日
編集済み: Mustafa Ali
2021 年 5 月 28 日
カテゴリ
ヘルプ センター および File Exchange で Mathematics and Optimization についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
