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

2 views (last 30 days)
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 Comments
Kelsey
Kelsey on 15 Jul 2014
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

Sign in to comment.

Answers (2)

Yoav Livneh
Yoav Livneh on 15 Jul 2014
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 Comments
Kelsey
Kelsey on 15 Jul 2014
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 on 15 Jul 2014
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.

Sign in to comment.


Kelsey
Kelsey on 15 Jul 2014
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);"

Categories

Find more on Mathematics 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!