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!
