# Why does it say Iteration limit reached and how do I fix it?

62 views (last 30 days)
Quincey Jones on 13 Feb 2020
Commented: KALYAN ACHARJYA on 13 Feb 2020
function [v,err,count] = Clapeyrons(P)
% Calculate molar volume as predicted by the Ideal Gas Law equation
% using the Newton-Raphson algorithm with initial estimate
% determined by ideal gas law.
% Inputs:
R = 0.082057; % Ideal gas constant in L atm / K mol
T = 293; % Temperature in K
Tc = 416.90; % Critical temperature of Cl2 in K
Pc = 78.72918; % Critical Pressure of Cl2 in atm
a = ((R^2)*(Tc^(5/2)))/(9*Pc*(2^(1/3)-1));
b = (R*Tc*(2^(1/3)-1))/(3*Pc);
% P = 1; % Pressure in atm
% Outputs:
% v equals molar volume in L/mol as predicted by
% equation for gas considered at temperature T and pressure P.
% err equals modulus of function evaluated at approximate root.
% count is number of iterations taken by Newton-Raphson algorithm.
% Version 1: created 24/04/19. Author: Paul Curran
% Version 2: created 06/02/20. Author: Savana Stewart
if (~isscalar(P)) || (~isreal(P)) || P <= 0
error('Input argument P must be positive real scalar.')
end
Iteration_limit = 20; % maximum number of iterations permitted
Tolerance=10^-7; % maximum acceptable value for modulus of
A = (a*P)/((R^2)*(T^(5/2)));
B = (b*P)/(R*T);
v = R * T / P;
Z = (P*v)/(R*T);
C = A-B-B^2;
f1 = [1 -1 C -A*B]; % f = (Z^3) - (Z^2) + (A-B-(B^2))*Z - (A*B)
f = polyval(f1,Z);
v = Z*R*T/P;
for count = 1:Iteration_limit + 1
% Terminate with error message if iteration limit exceeded:
if count == Iteration_limit + 1
error('Iteration limit reached. Iteration did not converge.')
end
% Terminate iteration if function is sufficiently small at current
% estimate
if abs(f)<Tolerance
break
end
%f_dash = 3*Z^2 - 2*Z + (A - B - (B^2)); % Evaluate derivative of f
f_dash = [0 3 -2 C];
Z = Z - (f1./f_dash); % Newton-Raphson iteration
end
err = abs(f); % Error is magnitude of f(v) at final root estimate
end
%%
% COPY THE FOLLOWING INTO THE COMMAND WINDOW
% P = [1 1.5 2 2.5 3 5 10 15 25 50 100];
% v = zeros(1,length(P));
% for i=1:length(P)
% [u,err,count]=Clapeyrons(P(i));
% v(i)=u;
% end
%
% P1=[1:0.1:100];
% V1=R*T./P1;
% plot(P1,V1)
% hold on
% plot(P,v,'or')
I says iteration limit reached but I only changed the format a little. How do I fix it?

KALYAN ACHARJYA on 13 Feb 2020
Edited: KALYAN ACHARJYA on 13 Feb 2020
Its doing as defined in the code, when the iteration reached more than iteration limit, it reflects the message as mentioned within the code
if count == Iteration_limit + 1
error('Iteration limit reached. Iteration did not converge.')
end
Before getting the break statement, your code reached the limit, please check, there may be abs(f) never less than Tolerance
if abs(f)<Tolerance
break
end
Its simple code, debug it step by step, ensure that it reached the break statement before reched the iteration limit, ensure that f is changing in right direction during each iteration. The loop execute the iteration as number of times=iteration limit.
Choose the right parameters values or codition to reached the break before reached to operation limit, see the tolerance value also.

Quincey Jones on 13 Feb 2020
I know that the statement tells me errors, I meant why is the interation not converging. Where is the error
KALYAN ACHARJYA on 13 Feb 2020
Change the variables value or tolerance value, when I change the tolerance value, it converging
Tolerance=10^7; % I know this is the correct value as required
# Do the Basic Maths, you get the appropriape typical values, so that it converge
>> [v,err,count]=Clapeyrons(5)
v =
4.8085
err =
0.0572
count =
1
>>