Printing an error message if jacobi's method, does not converges.
Show older comments
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
Accepted Answer
More Answers (1)
Sulaymon Eshkabilov
on 28 May 2021
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 Comments
Mustafa Ali
on 28 May 2021
Sulaymon Eshkabilov
on 28 May 2021
Edited: Sulaymon Eshkabilov
on 28 May 2021
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
on 28 May 2021
Edited: Mustafa Ali
on 28 May 2021
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!