close all
clear all

P1 = 2;  %psi


dV = 1;  %Change in Volume
V2 = 1:.1:10;  %final volume (L)


V1 = V2+dV;

V1 = V1/1000;  %Convert to m^3
V2 = V2/1000;  %Convert to m^3
P1 = 4.45*P1/(0.0254^2);  %Convert to Pa

W = -P1*V1.*log(V2./V1);  %Joules

P2 = P1.*V1./V2;
P2iso = P1*(V1./V2).^(7/5);


for i = 1:length(V2)
    V = V1(i):(V2(i)-V1(i))/100:V2(i);
    I = P1*(V1(i)./V).^(7/5);
    Wiso(i) = -trapz(V,I);
end


figure
plot(100*1000*V2/dV,W,100*1000*V2/dV,Wiso)
set(gca,'FontSize',16);
xlabel('% Deadspace:  [100*(Final Volume)/(Delta Volume)]');
ylabel('Energy (J)');
legend('Adiabatic','Isothermal');
title('Work Done to compress diatomic ideal gas (20psi initial internal pressure)');

print -depsc2 CompressionWork

figure
plot(100*1000*V2/dV,P2*(.0254^2)/4.45,100*1000*V2/dV,P2iso*(.0254^2)/4.45);
set(gca,'FontSize',16);
xlabel('% Deadspace:  [100*(Final Volume)/(Delta Volume)]');
ylabel('Final Pressure (psi)');
legend('Adiabatic','Isothermal');
title('Final Internal Pressure (20psi initial internal pressure)');
print -depsc2 FinalPressure