IC Engine Simulation, Help with Loops

11 views (last 30 days)
Michael
Michael on 20 Oct 2012
Answered: Steve Miller on 6 Dec 2022
Goal of the code is to create a simulation to gain a PV diagram of an internal combustion engine. I have code to generate a PV diagram using only air, piston location and engine parameters, (bore, connecting rod length, etc.).
Next step is to add valves.
Assuming: Piston starts at bottom dead center, THETA = 0 Pa = 101 (atmospheric pressure) 4 stroke engine, I tried using this loop:
P = Pa.*Vbdc./Vd;
Pa = 101
for i=length(THETA)
if(0 < THETA(i) && THETA(i) <2*pi)
Pcyl = P;
elseif(2*pi < THETA(i) && THETA(i) < 4*pi)
Pcyl = Pa;
end
end
plot(THETA,Pcyl)
Am I on the right track of thinking? This does not give a plot, but I'm not sure how else to go about it.
Thank you for your help, MW
  2 Comments
kjetil87
kjetil87 on 22 Oct 2012
i didnt check other parts of your code. But the loop will never run more then one time, regardless of what length(THETA) is. Should be for i=1:length(THETA). Also if u want Pcyl to keep one value for each loop counter u need to index it. Pcyl(i)=something. ( if length(THETA) is a big number you should initiate the vector Pcyl before running the loop: Pcyl=zeros(length(THETA),1); <-- this is not a must, but will speed up your loop significantly )
Test this to see what happens to the loop index.
test_vector=[1,2,3,4,5];
for i=length(test_vector) disp(i); end
for i=1:length(test_vector) disp(i) Pcyl(i)=i; end
kjetil87
kjetil87 on 22 Oct 2012
should offcourse be ..disp(i);Pcyl(i)=i;end
(missing semicolon)

Sign in to comment.

Answers (1)

Steve Miller
Steve Miller on 6 Dec 2022
Take a look at the documentation for this Simscape Driveline block, Generic Engine.
This block will produce a plot similar to what you are looking for.
--Steve

Categories

Find more on Engines & Motors in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!