Code doesn't run through whole array

1 visualización (últimos 30 días)
Tim Vandenabeele
Tim Vandenabeele el 19 de Mayo de 2022
Comentada: David Hill el 19 de Mayo de 2022
Hey,
I am an electromechanical engineering student and for a school assignment I have to calculate the total fuel consumption and power at each point and total emissions of a car travelling 2 different tracks using Matlab. The file 'Subaru2.0l_w_tracks.mat' contains all the specific track data for track 1 & track 2, including X Y Z points, max velocity at each point (V, the maximum velocity that the car may not exceed at that point) and engine speed in RPM (Omega), Specific fuel Consumption in g/kWh (SFC) and engine torque (T) in Nm. T and SFC are both in function of Omega. The actual velocities at each point for both tracks were calculated using a different matlab code and are given in the files 'V_kmh_Track1.mat' and V_kmh_Track2.mat'. For the power and fuel consumption part, I wrote a code for both tracks, It works perfectly fine for the first track but for the second track in line 90 which is supposed to find the right matching index number for the corresponding torque with the current engine speed in the tracks files I keep getting an error which keeps telling me that the left and right sides of the equation have a different number of elements. I already checked multiple times in the workspace and in fact they do have the same number of elements. The code for second track is in fact identical the one for the first track with the necessary scripts of data input adapted. I think the base of the problem is that for an unknown reason the for loop which calculates the wheel's angular velocity (line 83) only goes up to index array 31208 and leaves all the following zero. I already tried changing both data arrays to real values but that didn't work out either. I really have no idea why it works perfectly for the first track and not for the second. Can anyone help me? You can find all the necessary Matlab files and codes in the attachment. Thanks in advance! If anything is not clear to you, please let me know.
Kind Regards
Tim Vandenabeele

Respuesta aceptada

David Hill
David Hill el 19 de Mayo de 2022
With a couple changes the code runs.
v.V_kmh=real(v.V_kmh);%needs to be real (I inserted this line here)
for i=1:tracksize
if v.V_kmh(i)/3.6>0 && v.V_kmh(i)/3.6<vmax_gears(1)*3.6
Chosen_gear(i)=1;
gear_ratio(i)=gear_ratios(1);
% ....
for i=1:tracksize
w_wheel(i)=(v.V_kmh(i)/3.6)/Rw;
car.w(i)=round((w_wheel(i)/(2*pi))*gear_ratio(i)*diff*60);
if car.w(i)<=500
car.w(i)=500;
end
index(i)=min(car.w(i)-499,5501);%index is going above 5501 (I chanaged this line)
car.T(i)=engine.T(index(i));
car.P(i)=car.w(i)*((2*pi)/60)*car.T(i)/1000;
end
  2 comentarios
Tim Vandenabeele
Tim Vandenabeele el 19 de Mayo de 2022
Thank you very much! What does "index(i)=min(car.w(i)-499,5501);" exactly do? And does my original code work for the first track and not for the second?
David Hill
David Hill el 19 de Mayo de 2022
index(i)=find(500:5501==car.w(i));
if isempty(index(i))
index(i)=5501
end
%forgot what your variable was but it was just 500:5501, so find just
%gives car.w(i)-499 (same thing), but if car.w(i) >5501 the find command
%will give an empty answer. The above does the same as below.
index(i)=min(car.w(i)-499,5501);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by