How to sum the fuel consumed at idle (vehicle speed=0)?

2 次查看(过去 30 天)
I have some vectors of data for time, vehicle fuel consumption, and vehicle speed for a certain driving trip. I am confused about how to tell Matlab to sum up the fuel used for all time when vehicle speed=0...any help with this would be sincerely and greatly appreciated! Here is what I have right now:
[ZeroSpeed, IdleFuelIndex] = min(VSPD);
IdleFuelTime = time(IdleFuelIndex);
IdleFuel=0;
for n=1:length(time)-1
IdleFuel(n+1) = Fuel_Consumed(IdleFuelTime)(n+1) + IdleFuel(n);
end
min(VSPD)=0, so IdleFuelTime should return the times when VSPD=0. (VSPD is the variable for vehicle speed). The problem is, Matlab is only returning for the first time that vehicle speed is zero! Also, I'm not sure about the for loop...perhaps I'm on the right track though? IdleFuel (the fuel consumed while the vehihcle is idling) is supposed to be equal to the summation of the fuel consumed by the vehicle at IdleFuelTime (whenever VSPD=0).
Thank you so much for your help with this, in advance! I'm really stuck on this.
  2 个评论
Kelsey
Kelsey 2014-7-15
Some modifications to my code...it still won't run, but hopefully this can help better clarify the logic I'm trying to achieve:
[ZeroSpeed, IdleFuelIndex] = min(VSPD); %index for when VSPD=0 (idle)
IdleFuelTime = time(IdleFuelIndex); %make the index be time
for n=1:length(time)-1
if t(n+1) = IdleFuelTime
IdleFuel(n+1) = Fuel_Consumed_Method2_L(n+1) + IdleFuel(n);
elseif t(n+1) ~= IdleFuelTime
IdleFuel(n+1) = 0 + IdleFuel(n);
end
end

请先登录,再进行评论。

回答(2 个)

Yoav Livneh
Yoav Livneh 2014-7-15
In order to get all indeces of speed=0 you need to do:
IdleFuelIndex = find(VSPD == 0);
Then you can add them all up:
IdleFuel = sum(Fuel_Consumed(IdleFuelIndex));
  2 个评论
Kelsey
Kelsey 2014-7-15
Thank you for your reponse! When I run just that code, however, I get NaN returned...any idea how to fix this?
Yoav Livneh
Yoav Livneh 2014-7-15
It's hard to know exactly where the problem is without seeing the data. Make sure all the data is correct and valid. You can also use cumsum instead of sum to see where the NaN appears.

请先登录,再进行评论。


Kelsey
Kelsey 2014-7-15
This code seems to come close:
IdleFuelIndex = find(VSPD == 0); %(find indexes where VSPD=0)
IdleFuel = 0; %fuel consumed when car is at idle (L)
for n = IdleFuelIndex
IdleFuel(n) = Fuel_Consumed_Method2_L(n);
end
IdleFuel = transpose(IdleFuel);
IdleFuelTime = time(IdleFuelIndex);
IdleFuel = interp1(IdleFuelTime,IdleFuel,time);
...it returns the vector IdleFuel with seemingly legitimate data, but for some reason is one entry less than the rest of my data. I tried to solve this issue by interpolating using time as the index, but I get the error message, "The grid vectors do not define a grid of points that match the given values. Error in interp1 F = griddedInterpolant(X,V(:,1),method);"

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by