MATLAB Answers

How can I get this function to run with a Tolerance of 10^-7 while keeping the same numerical outputs

4 views (last 30 days)
Quincey Jones
Quincey Jones on 20 Feb 2020
Answered: Pravin Jagtap on 27 Feb 2020
function [v,err,count] = MS1_Ideal_Gas(P)
help MS1_Ideal_Gas % help function
% Version 1: created 18/02/20. Author: Savana Stewart
% UCD ID: 19208141
% Inputs:
R = 0.082057; % Ideal gas constant in L atm / K mol
T = 293; % Temperature in K
Tc = 416.90; % Critical temperature of Cl_2 in K
Pc = 78.72918; % Critical Pressure of Cl_2 in atm
a = ((R^2)*(Tc^(5/2)))/(9*Pc*(2^(1/3)-1));
b = (R*Tc*(2^(1/3)-1))/(3*Pc);
% P Pressure in atm
% Outputs:
% v equals molar volume (V/n) in L/mol
% err equals modulus of function evaluated at approximate root.
% count is number of iterations taken by Newton-Raphson algorithm.
if (~isscalar(P)) || (~isreal(P)) || P <= 0
error('Input argument P must be positive real scalar.')
end
Iteration_limit = 1000; % maximum number of iterations permitted
Tolerance = 10^7; % maximum acceptable value for modulus of
% function evaluated at estimated root3of
A = (a*P)/((R^2)*(T^(5/2)));
B = (b*P)/(R*T);
v = R * T / P; % Molar volume
Z = (P*v)/(R*T);
C = A-B-B^2; % Substitution to simplify equation
poly_f = [1 -1 C -A*B]; % = (Z^3) - (Z^2) + (A-B-(B^2))*Z - (A*B)
f = polyval(poly_f,Z);
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
% poly_f = [1 -1 C -A*B]; % = (Z^3) - (Z^2) + (A-B-(B^2))*Z - (A*B)
% f = polyval(poly_f,Z);
poly_df = [0 3 -2 C]; % = 3*Z^2 - 2*Z + (A - B - (B^2))
df = polyval(poly_df,Z);
Z = Z - (f/df); % Newton-Raphson iteration
v = Z*R*T/P; % Subsitiution to find v
% Terminate iteration if function is sufficiently small at current
% estimate
if abs(f) < Tolerance
break
end
end
% to find numerical values for table 2 in report:
v1 = R * T / P;
fprintf('P = %d \nv (Ideal Gas Law) = %d \nv (Redlich-Kwong) = %d \nrequires %d iterations \n \n',P,v1,v,count);
err = abs(f); % Error is magnitude of f(v) at final root estimate
end
%%
% COPY THE FOLLOWING INTO THE COMMAND WINDOW
% R = 0.082057; % Ideal gas constant in L atm / K mol
% T = 293; % Temperature in K
% 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] = MS1_Ideal_Gas(P(i));
% v(i)=u;
% end
%
% P1=[1:0.1:100];
% V1=R*T./P1;
% plot(P1,V1) %ideal
% hold on
% plot(P,v,'xr') %Redlich
% title('Molar Volume vs Pressure for Cl_2')
% xlabel('Pressure P (atm)')
% ylabel('Molar Volume v (L/mol)')
% legend({'Ideal Gas Law','Redlich-Kwong Equation'},'Location','northeast')
How can I get this to run with a lower tolerance to get more iteration, while keeping the same numerical answers for v?

  0 Comments

Sign in to comment.

Answers (1)

Pravin Jagtap
Pravin Jagtap on 27 Feb 2020
Hello Savana,
I am assuming that the above question in the extension of your earlier question (https://www.mathworks.com/matlabcentral/answers/506581-how-to-get-this-help-file-to-run). Refer to the answer provided there.
In the given code Tolerance is declared as 'Tolerance = 10^7'. I think you can change the value as per your requirements and execute the script as mentioned in an earlier answer.

  0 Comments

Sign in to comment.

Tags