Printing an error message if jacobi's method, does not converges.
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
Sulaymon Eshkabilov
el 28 de Mayo de 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 comentarios
Mustafa Ali
el 28 de Mayo de 2021
Sulaymon Eshkabilov
el 28 de Mayo de 2021
Editada: Sulaymon Eshkabilov
el 28 de Mayo de 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
el 28 de Mayo de 2021
Editada: Mustafa Ali
el 28 de Mayo de 2021
Categorías
Más información sobre Mathematics and Optimization en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
