Otto cycle in matlab
AIM:
Write code that can solve an otto cycle and make plots for it.
OBJECTIVE:
- Your code should create a PV diagram
- You should output the thermal efficiency of the engine.
OTTO CYCLE:
Otto Cycle is the theoretical thermodynamic cycle which describes the working of a spark ignition engine. This type of spark ignition engines is the most common type of engines used in automobiles.
The Otto cycle is the study of what happens to a mass of gas when it is subjected to changes in pressure, temperature, volume, adding heat and removing heat.

1-2=ISENTROPIC COMPRESSION
2-3=CONSTANT VOLUME HEAT ADDITION
3-4=ISENTROPIC EXPANSION
4-1=CONTANT VOLUME HEAT EXPANSION
ENGINE TERMINOLOGY:

code:
clc
%engine parameter inputs
bore=0.1;
stroke=0.1;
crl=0.15; %connecting rod length
cr=12; %compression ratio
%inputs
gamma=1.4;
p1=101325;
t1=500;
t3=2300;
%To find p1,p2,p3,p4 & v1,v2,v3,v4:
sv=(pi/4)*(bore^2)*stroke;
cv=sv/(cr-1);
v1=cv+sv;
v2=cv;
%p2v2^gamma=p1v1^gamma
p2=p1*(cr)^gamma;
constant=p1*v1^gamma;
v_compression=engine(bore,stroke,cr,crl,180,0);
p_compression=constant./v_compression.^gamma;
%p2v2/t2=p1v1/t1
t2=(p2*v2*t1)/(p1*v1);
v3=v2;
%p3v3/t3=p2v2t2
p3=p2*t3/t2;
constant_e=p3*v3^gamma;
v_expansion=engine(bore,stroke,cr,crl,0,180);
p_expansion=constant_e./v_expansion.^gamma;
v4=v1;
%p4v4^gamma=p3v3^gamma
p4=p3*(v3/v4)^gamma;
%efficiency
thermal_efficiency= (1-(1/(cr)^(gamma-1)))*100
%plot
figure(1)
hold on
plot(v1,p1,'*');
plot(v_compression,p_compression);
plot(v2,p2,'*');
line([v2 v3],[p2 p3],'linewidth',1);
plot(v3,p3,'*');
plot(v_expansion,p_expansion);
plot(v4,p4,'*');
line([v4 v1],[p4 p1],'linewidth',1);
xlabel('volume')
ylabel('pressure')
FUNCTION CODE:
function[v]=engine(bore , stroke ,cr, crl , start_angle, end_angle);
sv=(pi/4)*(bore^2)*stroke;
cv=sv/(cr-1);
a=stroke/2;
r=crl/a;
theta=linspace(start_angle,end_angle,180);
term1=0.5*(cr-1);
term2=r+1-cosd(theta);
term3=(r^2 - (sind(theta).^2)).^0.5;
v=[1 + term1 *(term2 -term3)].*cv
end
OUTPUT:


EXPLANATION:
-Inputs values are given, the input values are t1,t3,p1,bore,stroke,connecting rod length, compression ratio and gamma.
-With help of the formula, we can calculate the v1,v2,v3,v4,p2,p3,p4. After calculating this we can plot the four co-ordinate points but there is no curve in the graph.
-For incomplete graph purpose , we are using the function.In funtion, there is the code whenever we calling it gives the output. With help of volume formula ,theta values are changes 180 to 0 at state 2 & theta values are changes 0 to 180 at state .
-After calculating the volume compression , we need to calculate the pressure compresion (with help of formula).
-As the same procedure of compression, we need to calculate for volume expansion, pressure expansion.
-Plot the compression , expansion ,it will give the result as curve and join the co-ordinate points.
-With help of formula , we need to calculate the thermal efficiency.
FORMULA:

cv=clearance volume
sv=swept volume
cr=compression ratio
crl=connecting rod
a=crank radius
r=ratio of connecting rod
n=thermal efficiency
v=volume
p=pressure
t=temperature
Mention the errors faced while programming & overcome those errors:

CONCLUSION:
Finally , we created the pv graph for otto cyle.
&
Thermal efficiency of otto cycle is 63 percentage (round off value).