Otto cycle PV diagram and thermal efficiency using MATLAB
AIM
To plot a PV diagram and calculate the thermal efficiency of otto cycle by using MATLAB
EXPLANATION OF OTTO CYCLE
The Otto cycle is the theoretical cycle of interest when one is considering reciprocating SI engines. The four-stroke Otto cycle is made up of the following four internally reversible processes.

1–2, isentropic compression;
2–3, constant-volume heat addition;
3–4, isentropic expansion;
4–1, constant-volume heat rejection.
MATLAB PROGRAM
%QUESTION
clear all %used to clear all function and variables in workspace and system memory
close all %used to close all plots
clc %used to clear all text in command window
%INPUTS
%Pressure in an otto cycle(N/m2)
p1=101325;
%Temperature in an otto cycle (k)
t1=500;
t3=2300;
%Engine Paramters in (m)
bore=0.1;
stroke=0.1;
connecting_rod=0.15;
%Connecting Ratio
compression_ratio=12;
%Gamma for air
gamma=1.4;
%To find swept volume and clearance volume in (m^3)
swept_voulume=(pi/4)*bore^2*stroke; %swept volume is the difference between the tdc and bdc
clearance_volume=swept_voulume/(compression_ratio-1); %clearance volume is the volume between the cylinder head to piston when piston at tdc
%TO FIND PRESSURE AND VOLUME AT ALL POINTS
%ISENTROPIC COMPRESSION PROCESS 1-2
%t2/t1=(v1/v2)^gamma-1=cr^gamma-1 eq(1)
%p1v1^gamma=p2v2^gamma=constant eq(2)
%cr=v1/v2 eq(3)
%To find volume 1
v2=clearance_volume; %Clearance is also known as v2
v1=compression_ratio*v2; %from eq(3)
%To find pressure 2
p2=(p1*(v1^gamma))/(v2^gamma); %from eq(2)
%CONSTAN VOLUME HEAT ADDITION 2-3
%p2/t2=p3/t3 eq(5)
%cr=v1/v2=v4/v3 eq(6) %expansion ration is equal to cr
%To find pressure 3
t2=t1*compression_ratio^(gamma-1); %from eq(1) and eq(4)
p3=p2*t3/t2; %from eq(5)
%To find volume3
v3=v2; %from eq(6) %
%ISENTROPHIC EXPANSION PROCESS 3-4
%p3v3^gamma=p4v4^gamma %eq(7)
%To find volume 4
v4=v1; %from eq(6)
%To find pressure 4
p4=(p3*(v3^gamma))/(v4^gamma); %from eq(7)
%TO FIND VOULME OF COMPRESSION
volume_of_compression=engine_kinematics(bore,stroke,connecting_rod,compression_ratio,180,0);
constant=p1*v1^gamma; %from eq(2)
pressure_0f_compression=constant./(volume_of_compression.^gamma); %from eq(2
volume_of_expansion=engine_kinematics(bore,stroke,connecting_rod,compression_ratio,0,180);
constant=p3*v3^gamma; %from eq(7)
pressure_of_expansion=constant./(volume_of_expansion.^gamma); %from eq(7
%PLOTTING PRESSURE AND VOLUME AT ALL POINTS
hold on
plot(v1,p1,'x','color','r','linewidth',1.5) %plot used to create a 2D line plot by values of x and y
plot(v2,p2,'x','color','r','linewidth',1.5) %linewidth used to increase or decrease the line width
plot(v3,p3,'x','color','r','linewidth',1.5)
plot(v4,p4,'x','color','r','linewidth',1.5)
plot(volume_of_compression,pressure_0f_compression,'color','r','linewidth',1)
plot(volume_of_expansion,pressure_of_expansion,'color','r','linewidth',1)
plot([v2 v3],[p2 p3],'color','r','linewidth',1)
plot([v4 v1],[p4 p1],'color','r','linewidth',1)
xlabel('V') %Used to denote x-axis
ylabel('P') %Used to denote y-axis
grid on
title('PV DIAGRAM OF OTTO CYCLE')
%QUESTION 2
%THERMAL EFFICIENCY OF OTTOCYCLE
N=(1-(1/(compression_ratio^(gamma-1))))*100
%To obtained Compression and expansion of volume
%function is used to accepts inputs and return outputs
function [v]= engine_kinematics(bore,stroke,connecting_rod,compression_ratio,start,end_crank)
%INPUTS
%Pin_radius of piston in(m)
crank_pin=stroke/2;
%crank radius of piston in (m)
crank_radius=connecting_rod/crank_pin;
theta=linspace(start,end_crank,180); %linspace used to divide the matrix equally
%To find swept volume and clearance volume in (m^3)
swept_voulume=(pi/4)*bore^2*stroke; %swept volume is the difference between the tdc and bdc
clearance_volume=swept_voulume/(compression_ratio-1); %clearance volume is the volume between the cylinder head to piston when piston at tdc
%forumula of compression and expansion volume
v=(1+(0.5*(compression_ratio-1))*((crank_radius+1-cosd(theta))-((crank_radius^2-sind(theta).^2).^0.5))).*clearance_volume;
end
ERROR FACED WHILE PROGRAMMING
Error=Variable is not assigned properly
Steps taken to overcome=Assigned the variable correctly

Error= ‘end’ variblae assigned in MATLAB
Steps taken to overcome=end variable is used to close the functions,loop.So Changed the name of the variable ‘end’ into “end_crank”

Error= Not followed the BODMAS Rule
Steps taken to overcome=Re checked all the inputs,forumulas

OUTPUT:
GRAPH PV DIAGRAM
Pressure and outputs of all points are plotted

THERMAL EFFICIENCY
Thermal efficiency of otto cycle has been calculated
