clear all

% Plot and label an accurate Otto cycle.
% 
% Executes properly on MATLAB version 9.4.0.813654 (R2018a).
% Executes mostly properly on OCTAVE version 4.2.1.
% Joshua C. Vaughan 1/8/2021


n = 1;    % mol
R = 8.31; % J/(K mol)
gam = 5/3; % unitless. this for monatomic (ideal) gas

V = linspace(0.05, 0.1, 2^8);  % m^3

% make two adiabats
P_adi1 = 1./(V.^gam);
P_adi2 = 2./(V.^gam);

% make connecting const vol steps
Pa = [P_adi1(1) P_adi2(1)];
Pb = [P_adi1(end) P_adi2(end)];
Va = [V(1) V(1)];
Vb = [V(end) V(end)];

% make exhaust stroke
P_exh = [P_adi1(end) P_adi1(end)];
V_exh = [V(1) V(end)];

% plot Otto cycle
ax_lw = 2;
pl_lw = 3;
ax_fs = 24;
la_fs = 24;
ti_fs = 24;
mk_sz = 10;
fontname = 'Arial';


figure(1); clf
hold on
plot(V_exh,P_exh,'k--',V,P_adi1,'b',Va,Pa,'g',V,P_adi2,'r',Vb,Pb,'k','linewidth',pl_lw)

% plot vertices
plot(Va,Pa,'ko','markersize',mk_sz,'markerfacecolor','k')
plot(Vb,Pb,'ko','markersize',mk_sz,'markerfacecolor','k')
plot(V_exh(1),P_exh(1),'ko','markersize',mk_sz,'markerfacecolor','k')

% add text labels
vt_fs = 32;
text(Va(1)*0.9,Pa(1),'C','fontsize',vt_fs,'horizontalalignment','center','fontweight','bold','color','k')
text(Va(2)*0.9,Pa(2),'D','fontsize',vt_fs,'horizontalalignment','center','fontweight','bold','color','k')
text(Vb(1)*1.05,Pb(1),'B','fontsize',vt_fs,'horizontalalignment','center','fontweight','bold','color','k')
text(Vb(2)*1.05,Pb(2),'E','fontsize',vt_fs,'horizontalalignment','center','fontweight','bold','color','k')
text(V_exh(1)*0.9,P_exh(1),'A','fontsize',vt_fs,'horizontalalignment','center','fontweight','bold','color','k')

xlabel('Volume','fontsize',la_fs)
ylabel('Pressure','fontsize',la_fs)
set(gca,'fontsize',la_fs)
set(gca,'linewidth',ax_lw)
set(gca,'tickdir','out')
set(gcf,'color',[1 1 1])
set(gca,'fontname',fontname)
xlim([0.04 0.11])
ylim([0 350])
set(gca,'LooseInset',get(gca,'TightInset'));

set(gca,'xticklabel',{'','','','',''})
set(gca,'yticklabel',{'','','','',''})



% add legend
h = legend('inject fuel+O_2; exhaust','adiabatic compression', 'const. {\itV} combustion', 'adiabatic expansion','const. {\itV} cooling');
set(h,'fontsize',18);
set(h,'linewidth',1.5);


