% MATLAB file used in the solution of Example 14.2 of 
% "A First Course on Kinetics and Reaction Engineering."

% Data provided in the problem statement, in consistent units
V=2.3; % L
T=848.0; % K
P0=1036.0; % Torr
Rgas=62.366; % L-Torr/mol/K
t = [0; 0.1; 0.2; 0.3; 0.4; 0.5; 0.6; 0.7; 0.8; 0.9; 1.0]; % ms
Pz = [0; 121; 202; 275; 336; 375; 417; 416; 433; 457; 476]; % Torr

% calculate nA using equation (15) from the solution.
nA = V/Rgas/T*(P0 - 2*Pz);

% fit 2nd, third and 4th order polynomials to the nA vs t data
p2 = polyfit(t,nA,2)
f2 = polyval(p2,t);
p3 = polyfit(t,nA,3)
f3 = polyval(p3,t);
p4 = polyfit(t,nA,4)
f4 = polyval(p4,t);

% plot the results
figure
plot(t,nA,'o',t,f2,'--g',t,f3,':r',t,f4,'-.b')
xlabel('t');
ylabel('nA');
legend('data','2nd order','3rd order','4th order');