Matlab Code That Can Solve An Otto Cycle
Objective : Write a Code that can solve an otto cycle and make plots for it.
Problem Statement : Here are the requirements
- Your code should create a PV diagram
- You should output the thermal efficiency of the engine.
Solution Procedure :
It is thermodynamic cycle which is used in internal combustion engine to produce work.
There are different type of processes in Otto cycle :-
- Adiabatic compression (1-2)- air and fuel mixture is compressed isentropically inside the cylinder.
- Constant volume heat addition (2-3)- combustion take place inside the cylinder due to external source.
- Adiabatic expansion (3-4) - high pressure and high temperature gases expand isentropically inside the cylinder.
- Constant volume heat rejection (4-1):- gases come out from the cylinder and cycle is completed.
we have to give required inputs and by using basic thermodynamic formulas type in the code and save the required output. and to make a adiabatic curve by using the calling the function .
Function code is following :
function [V] = engine_kinematics(bore, stroke, con_rod, cr ,start_crank, end_crank)
a = stroke/2; %crank pin radius
R = con_rod/a; %ratio of con_rod and crank pin radius
%swept and clearance volume calculations
v_swept = pi/4*bore^2*stroke;
v_clearance = v_swept/(cr-1);
%creating array for different theta
theta = linspace(start_crank,end_crank,100);
% combustion volume calculation
term1 = 0.5*(cr-1);
term2 = R+1 - cosd(theta);
term3 = (R^2 - sind(theta).^2).^0.5;
V =(1 + term1*(term2-term3)).*v_clearance;
end
code Explanation :
In this above code i have created a function that can take input and assign value to the output array.
and simply calculated swept and clearance volume by using formula.
and created array for different theta values.
and finally written about combustion volume calculation.
Code for the P-V Diagram and Thermal efficiency:
clear all
close all
clc
%input
gamma = 1.4
t3 = 2300
%state variables
p1 = 101325
t1 = 500
%engine geomentric parameters
bore = 0.1;
stroke = 0.1;
con_rod = 0.15;
cr = 12;
%calculating the swept volume and the clearance volume
v_swept = (pi/4)*bore^2*stroke;
v_clearance = v_swept/(cr-1);
v1 = v_swept+v_clearance
v2 = v_clearance
%state variables at point 2
%p2v2^gamma = p1v1^gamma
%p2 = p1*(v1/v2)^gamma = p1*cr^gamma
p2 = p1 *cr^gamma;
%p1v1/t1 = p2v2/t2 || t2 = p2*v2*t1/(p1*v1)
t2 = p2*v2*t1/(p1*v1);
constant_c =p1*v1^gamma;
V_compression = engine_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 = engine_kinematics(bore,stroke,con_rod,cr,0,180)
p_expansion = constant_c./V_expansion.^gamma;
%state variable at state point 4
v4 = v1
%p3v3^gamma = p4v4^gamma || p4 = p3(v3/v4)^gamma
p4 = p3*(v3/v4)^gamma;
%thermal efficiency
thermal_efficiency = (1-(1/cr^(gamma - 1)))*100
hold on
plot(V_compression,p_compression)
plot([v2,v3],[p2,p3])
plot(V_expansion,p_expansion)
plot([v4,v1],[p4,p1])
xlabel('Volume')
ylabel('Pressure')
legend('isentropic compression','constant volume heat addition','isentropic expansion','constant volume heat rejection')
Code Explanation :
Input the values for gamma and temp. for point3 .
define state variables
define engine geomentric parameters
calculating the swept volume and the clearance volume.
calculating pressure and volume at point 2 using p2v2^gamma = p1v1^gamma and p1v1/t1 = p2v2/t2.
then calculating V_compression by calling the function. and p_compression using constant_c =p1*v1^gamma.
calculating pressure and volume at point 3 using v3=v2 and p3v3/t3 = p2v2/t2 .
then calculating V_compression by calling the function. and p_compression using constant_c =p3*v3^gamma.
calculating pressure and volume at point 4 using v4=v1 and p3v3^gamma = p4v4^gamma .
then calculating thermal_efficiency by (1-(1/cr^(gamma - 1)))*100.
then using plot command plotting the P-V Diagram.
Errors Faced while prgrammimg:
Basic typo errors , which is further solved by observing the code.
Results :
Output :
Thermal eFficiency :
Plot :
P-V Diagram :