6 views (last 30 days)

function [v,err,count] = MS1_Ideal_Gas(P)

% MS1_Ideal_Gas

% 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')

% 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 = 20; % 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 = 1;

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);

poly_df = [0 3 -2 C]; % = 3*Z^2 - 2*Z + (A - B - (B^2))

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

df = polyval(poly_df,Z);

Z = Z - (f/df); % Newton-Raphson iteration

% Terminate iteration if function is sufficiently small at current

% estimate

if abs(f) < Tolerance

break

end

end

v = Z*R*T/P; % Subsitiution to find v

% 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

help MS1_Ideal_Gas; % help function

end

When I type in the comments at the beginning if the file into the command window, it runs fine. However, When I tried to turn it into a help file the code no longer runs. How can I change the help file so it acts like I am just typing all of the first comments into the command window?

Pravin Jagtap
on 26 Feb 2020

Hello Savana,

The issue which you are facing is not clear from your question and code. I found a similar question you posted as an extension of this question.

I am assuming you are facing issues with the 'help' function and executing the 'MS1_Ideal_Gas' function file. You need to pass the argument 'P' which is scalar value as per code (Make sure that you are in the same directory before executing following command).

>> MS1_Ideal_Gas(1)

Refer to the following link for understanding the usage of 'help' function:

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.