0

I seem to currently be banging my head against a brick wall as try as I might, I can not see my error here.

I am attempting to write a for loop in MATLAB that uses the equation below (adiabatic compression) to calculate the new pressure after one degree of crankshaft rotation in a four stroke engine cycle.

P2 = P1 * (V2 / V1) ^2

I am using the calculated volume from the crank-slider model as an input. I have tried this is Excel and it works as expected and gives the overall max output correctly.

The for loop in question is below;

Cyl_P = ones(720,1)

for i = (2:1:length(Cyl_V))'

    Cyl_P(i,:) = Cyl_P(i-1,:) .* (Cyl_V(i,:) ./ Cyl_V(i-1,:)).^1.35

end

my aim is to use the first element of the vector Cyl_P which is equal to one, as an input to the equation above, and multiply it by the second element of Cyl_V divided by the first, and multiply the volume terms by 1.35. that should calculate the second element of Cyl_P. I would then like to feed that value back in to the same equation to calculate the third element and so on.

What am I missing?

I've put the full code below

Theta = deg2rad(1:1:720)'

Stroke = 82 / 1000
R = Stroke / 2
L = 90.5 / 1000
Bore = 71.9 / 1000

d_h = (R+L) - (R.*cos(Theta)) - sqrt(L.^2 - (R.*sin(Theta)).^2)
Pist_h = d_h

figure
plot(Pist_h)

Bore_A = (pi*Bore^2)/4
Swept_V = (Pist_h .* Bore_A)
Clear_V = max(Swept_V) / 10
Total_V = max(Swept_V) + Clear_V

Cyl_V = (Swept_V + Clear_V)

figure
plot(Cyl_V)

for ii = (2:1:length(Cyl_V))'
    div_V(ii,:) = (Cyl_V(ii) ./ Cyl_V(ii-1,:)).^1.35
end

Cyl_P = ones(720,1)

for i = (2:1:length(Cyl_V))'

    Cyl_P(i,:) = Cyl_P(i-1,:) .* (Cyl_V(i,:) ./ Cyl_V(i-1,:)).^1.35

end

figure
plot(Cyl_P)
Adriaan
  • 17,741
  • 7
  • 42
  • 75
Ross Hanna
  • 25
  • 4

1 Answers1

2

Your problem is transposing the arrays you feed as argument to for loops. MATLAB reads for arguments per row, thus only the first iteration will be used when you feed it a column. General comments:

  • ' is the complex transpose, .' is the regular transpose.
  • i is the imaginary unit in MATLAB, it's common practise not to use it as a variable name.
  • 2:1:4 does the same as 2:4, as 1 is the default step size.
  • Please use semi-colons, ;, after each row, so as to prevent MATLAB from echoing the result of each line to the command window. This makes the script easier to run, and if you have matrices with >1M entries, echoing the contents might even crash the program all together. Even in this case, you are echoing 720 entries of Cyl_P 720 times. For checking variable contents, just break the script where necessary (or run it in parts) and examine the content where warranted (e.g. Cyl_P(1:3) would suffice here to check whether the loop fills the vector as intended).
Adriaan
  • 17,741
  • 7
  • 42
  • 75