Lab 4

Calls M-function gaslaw to calculate V and Z for different values of P, for gaseous ammonia. Also plots volume and compressibility ratio vs pressure, and approximates the pressure at which ammonia behaves as an ideal gas.

Contents

For-loop

We use a for-loop to call the function
             gaslaw
for each given value of P
% column vector of pressures
P=[56;100;150;180;190;200;210;220;280;330;450;560;1100;1670;2230];
% length of vector
n=length(P);
for k=1:n
    [v,z]=gaslaw(P(k));
    V(k,1)=v;    % V and Z are column vectors
    Z(k,1)=z;
end

Table of results

The columns display the following values:

After trial and error an adequate format is chosen

format short e
M=[P,V,Z]
M =

  5.6000e+001  5.7489e-001  8.7183e-001
  1.0000e+002  2.7461e-001  7.4364e-001
  1.5000e+002  1.3066e-001  5.3073e-001
  1.8000e+002  9.2065e-002  4.4877e-001
  1.9000e+002  8.6811e-002  4.4667e-001
  2.0000e+002  8.3022e-002  4.4966e-001
  2.1000e+002  8.0121e-002  4.5564e-001
  2.2000e+002  7.7800e-002  4.6351e-001
  2.8000e+002  6.9542e-002  5.2731e-001
  3.3000e+002  6.5774e-002  5.8779e-001
  4.5000e+002  6.0499e-002  7.3725e-001
  5.6000e+002  5.7610e-002  8.7366e-001
  1.1000e+003  5.0970e-002  1.5183e+000
  1.6700e+003  4.7931e-002  2.1677e+000
  2.2300e+003  4.6165e-002  2.7879e+000

Plots

% data
R = 0.08206;
T = 450;
Vi = R*T./P;
%plots
figure(1)
plot(P,V,P,Vi)
title('Molar volume vs Pressure')
legend('ideal gas law','van der Waals equation')
xlabel('P')
ylabel('V')
figure(2)
plot(P,Z)
hold on
ezplot('1',[0,2500]) % plots straight line y = 1
title('Compresibility factor vs Pressure with van der Waals equation')
xlabel('P')
ylabel('Z')

Approximation of pressure at which ammonia behaves as an ideal gas

Use ginput and click with mouse.

% return to default format
format
% waits for click on intersection
P = ginput(1)
P =

  665.3226    0.9956

Code for function gaslaw

type gaslaw.m
function [V, Z] = gaslaw( P )
%gaslaw Calculates molar volume  and the compresibility factor
%   using the van der Waals equation
%   for fixed values of Tc, Pc, R and T (given below)
% Input parameter P: pressure
% Output parameters V: molar volume; Z: compresibility factor;


Tc = 405.5;
Pc = 111.3;
R = 0.08206;
T = 450;

%Calculates parameters a and b for van der Wals equation
a = (27*R^2*Tc^2)/(64*Pc);
b = (R*Tc)/(8*Pc);

%Calculates V0 from the ideal gas law
V0 = R*T/P;

%Defines function for van der Waals equation (and plots it)
vanderwaals = @(V)(P+a./V.^2).*(V-b)-R*T;
vplot = linspace(0,5);
fplot = vanderwaals(vplot);
plot(vplot,fplot)
grid
title('van der Waals equation')
xlabel('V')
ylabel('f(V)')

%Calculates V with van der Waals equation and displays it
V = fzero(vanderwaals,V0);
hold on
plot(V,0,'ro')
hold off

%Calculates the compresibility factor
Z = (P*V)/(R*T);
end