Write a MATLAB code to plot the P-V diagram for air standard cycle and calculate the thermal efficiency of an engine.
Aim: 1) Write a MATLAB code to plot the P-V diagram for air standard cycle.
2) Calculate the thermal efficiency of engine.
Theory: An Otto cycle is an idealized thermodynamic cycle that describes the functioning of a typical spark ignition piston engine. It is the thermodynamic cycle most found in automobile engines.
The Otto cycle is a description of what happens to a mass of gas as it is subjected to changes of pressure, temperature, volume, addition of heat, and removal of heat. The mass of gas that is subjected to those changes is called the system. The system, in this case, is defined to be the fluid (gas) within the cylinder. By describing the changes that take place within the system, it will also describe in inverse, the system's effect on the environment. In the case of the Otto cycle, the effect will be to produce enough net work from the system to propel an automobile and its occupants in the environment.
The Otto cycle is constructed from:
Top and bottom of the loop: a pair of quasi-parallel and isentropic processes (frictionless, adiabatic reversible).
Left and right sides of the loop: a pair of parallel isochoric processes (constant volume).

The processes are described by:
- Process 0–1 a mass of air is drawn into piston/cylinder arrangement at constant pressure.
- Process 1–2 is an adiabatic (isentropic) compression of the charge as the piston moves from bottom dead center (BDC) to top dead center (TDC).
- Process 2–3 is a constant-volume heat transfer to the working gas from an external source while the piston is at top dead center. This process is intended to represent the ignition of the fuel-air mixture and the subsequent rapid burning.
- Process 3–4 is an adiabatic (isentropic) expansion (power stroke).
- Process 4–1 completes the cycle by a constant-volume process in which heat is rejected from the air while the piston is at bottom dead center.
- Process 1–0 the mass of air is released to the atmosphere in a constant pressure process.
Matlab Code:
clear all
close all
clc
% Inputs
gamma = 1.4;
t3 =2300;
% State Variables
p1 = 101325;
t1 = 500;
% Engine geometric parameters
bore = 0.1;
stroke = 0.1;
con_rod = 0.15;
cr = 12;
% Calcuating the swept volume and clearance volume.
v_swept = (pi/4)*bore^2*stroke;
v_clearance = v_swept/(cr-1);
v1 = v_swept + v_clearance;
v2 = v_clearance;
% State variable at point2
%p2*v2^gamma = p1*v1^gamma
%p2 = p1*(v1/v2)^gamma = p1*(cr)^gamma
p2 = p1*cr^gamma;
%p1v1/t1 = p2v2/t2
t2 = p2*v2*t1/(p1*v1);
constant_c = p1*v1^gamma;
V_compression = Piston_Kinematics(bore,stroke,con_rod,cr,180,0);
P_compression = constant_c./V_compression.^gamma;
% State variables at state point 3
v3 = v2;
% p3v3/t3 = p2v2/t2 | p3 = p2*t3/t2
p3 = p2*t3/t2;
constant_c = p3*v3^gamma;
V_expansion = Piston_Kinematics(bore,stroke,con_rod,cr,0,180);
P_expansion = constant_c./V_expansion.^gamma;
% State variable at point 4
v4 = v1;
%p3v3^gamma = p4v4^gamma| p4 = p3*(v3/v4)^gamma
p4 = p3*(v3/v4)^gamma;
Term1 = 1/(cr^(gamma-1))
Thermal_Efficiency = (1-Term1)*100
figure(1)
plot([v1 V_compression V_expansion v4 v1],[p1 P_compression P_expansion p4 p1],'LineWidth',2,'color','b')
title('P-V Diagram Plot')
xlabel('Volume')
ylabel('Pressure')
Explanation of the code:
- The clear all, close all, clc are the commands to clear the workspace as you run the program.
- Engine geometric parameters like bore, stroke, con_rod, compression ratio(cr) are defined. In addition to this state variables like p1,t1,gamma,t3 are also defined.
- Clearance volume and swept volume are calculated using the formula.
- Volume at point1(v1) is the addition of swept volume and clearance volume. Volume at point2(v2) is the clearance volume.
- State variable at point 2 (p2 and t2) are calculated using isentropic gas relation and ideal gas law relation. Isentropic gas law is p2*v2*gamma = constant. Ideal gas law relation is given by p1v1/t1 = constant.
- Process 1-2 is a isentropic process. So the volume does not vary linearly. It varies by the relation
To calculate the volume V, piston kinematics function is defined which takes the input as bore, stroke, con_rod,cr,start_crank,end_crank. So V_compression is calculated using this function. For compression process, the starting crank angle is 180 and ending crank angle is 0. P_compression is calculated using isentropic gas relation.
- Variables at state point3 are calculated using isentropic relation and ideal gas law relation. As 3-4 is an isentropic expansion process, V_expansion is calculated using piston kinematics function. P_expansion is calculated using isentropic gas relation.
- State variables at point4 is calculated using ideal gas law relation.
- Thermal efficiency of an engine is calculated using the relation,
1-1/(cr^(gamma-1))*100
Results:


Conclusion: The matlab program is successfully compiled. Thermal efficiency of an engine is calculated. It mainly depends on the compression ratio. The P-V diagram for various processes of an Otto cycle are plotted.