%% --------------------- Part 2 ------------------ %%

%% Pressure v volume %%
data = readmatrix('Book2.xlsx', 'Range', 'B5:C724');
angle = data(:,1);
volume = data(:,2);

rpm = [6500 6000 5500 5000 4500 4250 4000 3750 3500 3000 2500 2000 1500 1000]; 

[AngleGrid, RPMgrid] = meshgrid(angle, rpm);
VolumeGrid = repmat(volume', length(rpm), 1);

surf(AngleGrid, RPMgrid, VolumeGrid);
shading interp
xlabel('Crank Angle (°)')
ylabel('RPM')
zlabel('Volume (cc)')
title('Volume v Crank Angle')
grid on
data = readmatrix('Book3.xlsx', 'Range', 'B5:P724');

volume = data(:,1);
pressure = data(:,2:end);

rpm = [6500 6000 5500 5000 5400 4250 4000 3500 3000 2500 2000 1750 1500 1000];

PressureGrid = pressure';
[VolumeGrid, RPMgrid] = meshgrid(volume, rpm);

surf(VolumeGrid, RPMgrid, PressureGrid);
shading interp
xlabel('Volume (cc)')
ylabel('RPM')
zlabel('Pressure')
title('Pressure vs Volume')
grid on

% We can see that the pressure is at its lowest during the intake stroke as
% the piston moves down and volume increases. During compression, the piston
% moves upward, reducing volume and raising the pressure until combustion near 
% TDC, causing a sharp spike. In the power stroke, the expanding gases 
% increase the volume while the pressure falls. During the exhaust stroke, 
% the piston pushes the gases out, keeping pressure relatively high. The 
% overall loop shape varies slightly with RPM. %


%% Pressure v crank angle %%
data = readmatrix('Book5.xlsx', 'Range', 'B5:P724');
angle = data(:,1);  
pressure = data(:,2:end);

rpm = [6500 6000 5500 5000 5400 4250 4000 3500 3000 2500 2000 1750 1500 1000];
PressureGrid = pressure'; 
[AngleGrid, RPMgrid] = meshgrid(angle, rpm);
surf(AngleGrid, RPMgrid, PressureGrid);
shading interp
xlabel('Crank Angle (°)')
ylabel('RPM')
zlabel('Pressure')
title('Pressure vs Crank Angle')
grid on

% We can see the pressure rising gradually during the compression stroke, 
% then increases sharply during combustion, reaching a peak slightly after 
% TDC, around 20 degrees, where it produces maximum force on the crank. 
% After this peak, pressure falls through the expansion stroke as the gases
%  push the piston down. The pressure remains low during intake and rises 
% slightly again during exhaust. % 

%% Polytropic index v crank angle %% 
data = readmatrix('Book3.xlsx', 'Range', 'A5:P724');

angle   = data(:,1); 
volume  = data(:,2);
pressure = data(:,3:end);

lnV = log(volume); 
lnP = log(pressure); 
n = diff(lnP) ./ -diff(lnV); 
angle_n = angle(1:end-1); 
rpm = [6500 6000 5500 5000 4500 4250 4000 3750 3500 3000 2500 2000 1500 1000];
nGrid = n';  
[AngleGrid, RPMgrid] = meshgrid(angle_n, rpm);
surf(AngleGrid, RPMgrid, nGrid);
shading interp
xlabel('Crank Angle (°)')
ylabel('RPM')
zlabel('Polytropic Index n')
title('Polytropic Index v Crank Angle')
grid on

% We can see that the polytropic index stays mostly flat during intake and 
% exhaust because the gase behaves nearly ideally. A rise near TDC reflects
%  compression and the start of combustion, where the gas becomes stiffer. 
% Larger peaks near BDC occur during expansion, where hot gases and heat 
% transfer cause the gas to deviate from ideal behaviour. These features 
% appear at the same crank angles for all RPMs. %

%% Log of pressure v Log of volume %%
data = readmatrix('Book3.xlsx', 'Range', 'B5:P724');

volume = data(:,1);
pressure = data(:,2:end); 
lnV = log(volume);
lnP = log(pressure);
rpm = [6500 6000 5500 5000 4500 4250 4000 3750 3500 3000 2500 2000 1500 1000];

lnPgrid = lnP';
[lnVgrid, RPMgrid] = meshgrid(lnV, rpm);
surf(lnVgrid, RPMgrid, lnPgrid);
shading interp
xlabel('ln(Volume)')
ylabel('RPM')
zlabel('ln(Pressure)')
title('Log of Pressure v Log of Volume')
grid on

% We can see that the compression and expansion strokes appear almost 
% linear, with the slope representing the effective polytropic index. 
% Intake and exhaust strokes are much flatter because pressure changes very
%  little. Small deviations in slope reflect combustion and heat transfer 
% effects. The overall shape remains consistent across the RPMs.% 

%% Volume v crank angle %%
data = readmatrix('Book2.xlsx', 'Range', 'B5:C724');
angle = data(:,1);
volume = data(:,2);

rpm = [6500 6000 5500 5000 4500 4250 4000 3750 3500 3000 2500 2000 1500 1000]; 

[AngleGrid, RPMgrid] = meshgrid(angle, rpm);
VolumeGrid = repmat(volume', length(rpm), 1);

surf(AngleGrid, RPMgrid, VolumeGrid);
shading interp
xlabel('Crank Angle (°)')
ylabel('RPM')
zlabel('Volume (cc)')
title('Volume v Crank Angle')
grid on

% During the intake stroke, we can see the volume increasing as the piston 
% moves from TDC to BDC. During compression, the piston travels back up and
%  the volume decreases. Near TDC, combustion begins and the power stroke 
% pushes the piston down again, increasing the volume. Finally, during the 
% exhaust stroke, the piston moves back up to expel the gases, reducing the
%  volume. This pattern repeats consistently across the RPMs. %

%% Rate of change of pressure v crank angle %%
data = readmatrix('Book3.xlsx', 'Range', 'A5:P724');

angle    = data(:,1); 
volume   = data(:,2); 
pressure = data(:,3:end); 
dP = diff(pressure); 
dTheta = diff(angle); 

dP_dTheta = dP ./ dTheta; 
angle_d = angle(1:end-1); 
dPgrid = dP_dTheta'; 
[AngleGrid, RPMgrid] = meshgrid(angle_d, rpm);
figure
surf(AngleGrid, RPMgrid, dPgrid);
shading interp
xlabel('Crank Angle (°)')
ylabel('RPM')
zlabel('dP/d\theta (Pa/deg)')
title('Rate of Change of Pressure vs Crank Angle')
grid on

% We can see how quickly pressure chnages throughout the cycle. A sharp 
% spike near TDC marks the start of combustion, where pressure rises 
% rapidly. This is followed by a steep drop as the piston mvoes down and 
% the gases expand. Away from TDC, the curve stays close to zero becasue 
% pressure changes slowly during intake, compression, and exhaust. %

%% Torque v crank angle %%
data = readmatrix('Book3.xlsx', 'Range', 'A5:P724');

angle    = data(:,1);
volume   = data(:,2);  
pressure = data(:,3:end); 


rpm = [6500 6000 5500 5000 4500 4250 4000 3750 3500 3000 2500 2000 1500 1000];

dV = diff(volume);

angle_torque = angle(1:end-1);

torque = pressure(1:end-1,:) .* dV; 

TorqueGrid = torque';                  
[AngleGrid, RPMgrid] = meshgrid(angle_torque, rpm);

figure
surf(AngleGrid, RPMgrid, TorqueGrid);
shading interp
xlabel('Crank Angle (°)')
ylabel('RPM')
zlabel('Torque (Nm)')
title('Torque v Crank Angle')
grid on

% We can see that the torque peaks shortly after TDC, when combustion 
% produces the highest force on the piston. It then decreases during the 
% expansion stroke as pressure falls. Negative torque appears during 
% intake, compression, and exhaust becauase the piston is doing work on the
%  gas rather than the other way around. The torque pulse becomes narrower 
% and shifts slightly with increasing RPM. %

%% Temperature v crank angle %%
data = readmatrix('Book3.xlsx', 'Range', 'A5:P724');

angle        = data(:,1);
volume_cc    = data(:,2); 
pressure_bar = data(:,3:end); 

volume = volume_cc / 1e6; 
pressure = pressure_bar * 1e5;

R = 287;  
T_intake = 300; 
AFR_stoich = 14.7;
lambda = 0.9;
AFR = AFR_stoich * lambda;

P_intake = 1.356e5;
V_max = max(volume);

m_air = (P_intake * V_max) / (R * T_intake);
m_fuel = m_air / AFR;
m_total = m_air + m_fuel;

temperature = (pressure .* volume) ./ (m_total * R);   % 720×14

rpm = [6500 6000 5500 5000 4500 4250 4000 3750 3500 3000 2500 2000 1500 1000];

TempGrid = temperature';                 
[AngleGrid, RPMgrid] = meshgrid(angle, rpm);

figure
surf(AngleGrid, RPMgrid, TempGrid);
shading interp
xlabel('Crank Angle (°)')
ylabel('RPM')
zlabel('Temperature (K)')
title('Temperature vs Crank Angle')
grid on

% We can see that the temperature remains low during intake and 
% compression, then rises sharply near TDC, when combustion begins and heat
%  is released. The peak temperature occurs shortly after TDC, when the 
% burning gases reach their maximum energy. As the piston moves down during
%  expansion, temperature falls as the gases expand and do work. Higher 
% RPMs tend to show higher peak temperature because there is less time for 
% heat to escape. %