clear all

% Plot and label an accurate Diesel 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

V1 = linspace(0.03294, 0.1, 2^8);  % m^3
V2 = linspace(0.05, 0.1, 2^8);  % m^3

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

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

% make exhaust stroke
P_exh = [P_adi1(end) P_adi1(end)];
V_exh = [V1(1) V1(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--',V1,P_adi1,'b',[V1(1) V2(1)],[P_adi2(1) P_adi2(1)],'g',V2,P_adi2,'r',[V1(end) V1(end)],[P_adi1(end) P_adi2(end)],'k','linewidth',pl_lw)


% plot vertices
plot([V1(1) V2(1)],[P_adi1(1) P_adi2(1)],'ko','markersize',mk_sz,'markerfacecolor','k')
plot([V1(end) V1(end)],[P_adi1(end) P_adi2(end)],'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)*1.15,Pa(1),'D','fontsize',vt_fs,'horizontalalignment','center','fontweight','bold','color','k')
text(Va(2)*0.8,Pa(2),'C','fontsize',vt_fs,'horizontalalignment','center','fontweight','bold','color','k')
text(Vb(1)*1.07,Pb(1),'B','fontsize',vt_fs,'horizontalalignment','center','fontweight','bold','color','k')
text(Vb(2)*1.07,Pb(2),'E','fontsize',vt_fs,'horizontalalignment','center','fontweight','bold','color','k')
text(V_exh(1)*0.8,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.02 0.139])
ylim([0 400])
set(gca,'LooseInset',get(gca,'TightInset'));

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



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


