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

 Accepted Answer

One approach would be to set a limit on ‘itc’. If it fails to converge before ‘itc’ reaches some value (perhaps ‘iteration_limit’) then the error would display. Use an if bolck to test in each iteration, and display if ‘itc’ exceeds that limit.
EDIT — (28 May 2021 at 4:00)
% Code
iteration_limit = 11;
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;
if itc > iteration_limit
error('Jacobi method did not converge by %d iterations.',iteration_limit)
break
end
end
.

5 Comments

Sorry could you perhaps show me an example of what you mean. I'm kinda new to this haha.
Hmm, I changed it to 11 and still no error
% 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 = 11; % Max Iterations
L = length(b);
Z = zeros(L,1);
itc = 0; %Counts Number of Iterations
% Convergence Tolerance
t = 0.0001;
% Code
iteration_limit = 11;
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;
if itc > iteration_limit
error('Jacobi method did not converge by %d iterations.',iteration_limit)
break
end
end
I am not certain what the inputs should be, so I am not certain how to test your code.
First, check to see what ‘itc’ is. It may never be greater than 11.
Testing that if block itself:
iteration_limit = 11;
itc = 12;
if itc > iteration_limit
error('Jacobi method did not converge by %d iterations.',iteration_limit)
% break
end
Jacobi method did not converge by 11 iterations.
So that part of the code works! (I commented-out the break call because it throws an error if it is not inside a loop, so not applicable in this specific test.)
.
Hey, thanks for helping out. For Matix A I used [2 1 3;8 8 13;10 9 19] and for Vector B i used [ 7;37;47]. I did get a result. But just to confirm. Did I input your code corretly?
% 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 itc < I;
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 converged % if norm(r) < some tolerance , it is converged
break
end
P = Z;
end
iteration_limit = 11;
itc = 12;
if itc > iteration_limit
error('Jacobi method did not converge by %d iterations.',iteration_limit)
% break
end
Did I input your code corretly?
Not the way I intended that it be used.
I changed the code to do what I intended, and since the routine converges in about 11 iterations with the test matrices, I changed ‘iteration_limit’ to 9 to test the convergence failure if block.
It works correctly.
a = [2 1 3;8 8 13;10 9 19]
a = 3×3
2 1 3 8 8 13 10 9 19
b = [ 7;37;47]
b = 3×1
7 37 47
% 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
iteration_limit = 9; % <— CHANGE THIS TO 11 (OR WHATEVER VALUE YOU WANT FOR THE LIMIT)
while itc < I;
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;
if itc >= iteration_limit
error('Jacobi method did not converge by %d iterations.',iteration_limit)
break
end
end
1 Iteration
Z = 3×1
3.5000 4.6250 2.4737
2 Iteration
Z = 3×1
-2.5230 -2.8947 -1.5592
3 Iteration
Z = 3×1
7.2862 9.6817 5.1728
4 Iteration
Z = 3×1
-9.1000 -11.0670 -5.9472
5 Iteration
Z = 3×1
17.9543 23.3893 12.5054
6 Iteration
Z = 3×1
-26.9528 -33.6507 -18.0551
7 Iteration
Z = 3×1
47.4080 60.9174 32.5991
8 Iteration
Z = 3×1
-75.8574 -95.7566 -51.3335
9 Iteration
Z = 3×1
128.3785 163.8993 87.7570
Jacobi method did not converge by 9 iterations.
Success!
.

Sign in to comment.

More Answers (1)

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

If I do that, it still doesnt show the error message, I have to do something with the iteration number. I want it so that it goes to iteration 11 and says "ERROR" something like that.
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
Hey, so it works but, is there a way where It can displays number if iterations until it can't converge anymore and prints an error message. The code you've given works very good but it stops at iteration 11 which is converged. I want it so that it displays until it actually cannot converge.
These are what im using for Matix A and vector B
Matix A: [19 3 1;3 10 2;1 2 10]
Vector B: [ 19;29;35]
PS: I commented saying I wanted it to go up to the 11th iteration and stop. But i realised it would just be incorrect for my task.

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!