compare the fuel comsumption of two cars in the same report

2 views (last 30 days)
Hi everyone, How can I compare two cars in the same report, thank you very much!
type FossilFuel_M.m
%calculates the result of a 1km drag race for %a user define vehicle powered by an internal %combustion engine %2020 Porsche 718 Spyder (982) 4.0 (420 Hp) 2019, 2020 Specs clear cD=0.34; %car dragcoefficient %https://speedsdb.com/car/porsche/718/718-spyder-982-top-speed aF=2.43836; %Cr frontal area %https://www.auto-data.net/en/porsche-911-991-ii-gt3-rs-4.0-520hp-pdk-32740 CarOnlyMass=1430;% mass of car without driver kg %https://www.auto-data.net/en/porsche-911-991-ii-gt3-rs-4.0-520hp-pdk-32740 DriverMass=80;%kg rolResCoeff=0.012; %rolling resistance coefficient FuelLowerHeatingVal=0.01206; %lower heating value of fuel in kWh/g - equals 0.01206 for petrol%https://chemeng.queensu.ca/courses/CHEE332/files/ethanol_heating-values.pdf gearRatios=[%ratios of gears 1:5, you can add more if you have more than 5 forward gears%https://newsroom.porsche.com/dam/jcr:e00c2789-42c7-4dab-a818-a732003194b0/2019_911_GT3_RS_Technical_Specifications.pdf.PDF 3.75 2.381 1.7 1.344 1.114 0.957 0.844]; FinalDriveRatio=3.620; SpeedTorque=[0 50 750 127 1000 190 1250 228 1500 253 1750 271 2250 302 2750 335 3000 350 3250 364 4250 406 4500 413 5000 420 5250 420 5500 420 6000 420 6500 420 6800 420 7000 413 7500 394 1E50 0]; %[rpm nm] maxEngineRPM=6800; WheelRadius=0.508;%m%https://speedsdb.com/car/porsche/718/718-spyder-982-top-speed idleRPM=700; %The minimum engine speed before stalling, in this model we assume that all engine torque is transmitted at this speed, even when the clutch is slipping during start-up. Though not enirely true it makes little difference to this model %% %% CALCULATION SECTION: You do not need to alter anything here SpeedEfficiency=[700 0.309946464 1000 0.317506134 2000 0.325443787 3000 0.329563329 4000 0.325443787 5000 0.317506134 6000 0.295857988 7000 0.265668398 7500 0.250341375]; CarMass=CarOnlyMass+DriverMass; CarLinearVelocity=0; CarDisplacement=0; for i=1:100 drag=cD*aF*0.5*1.225*(CarLinearVelocity^2); rollingResistance=rolResCoeff*CarMass*9.81; WheelCircumference=pi()*2*WheelRadius; CarWheelRPM=60*(CarLinearVelocity/WheelCircumference); %RPM of car wheel numGears=size(gearRatios,1); for o=1:numGears CarEngineRPM=max(CarWheelRPM*FinalDriveRatio*gearRatios(o,1),idleRPM); if CarEngineRPM>maxEngineRPM %if calcs suggest that engine would have to rotate faster than it is permitted to in order to maintain the cars speed at the given gear... CarEngineTorque=0; TotalWheelTorque=0; %no or negative torque, as engine slows car down CarEngineEfficiency=interp1(SpeedEfficiency(:,1)',SpeedEfficiency(:,2)',maxEngineRPM); %engine efficiency based on max RPM; else CarEngineTorque=interp1(SpeedTorque(:,1)',SpeedTorque(:,2)',CarEngineRPM); %engine provides torque based on its actual RPM TotalWheelTorque=(CarEngineTorque*FinalDriveRatio*gearRatios(o,1)); %Nm CarEngineEfficiency=interp1(SpeedEfficiency(:,1)',SpeedEfficiency(:,2)',CarEngineRPM);%engine efficiency based on its actual RPM; end % calculate and store torque and efficiency, and ngine RPM for each gear CT(1,o)=TotalWheelTorque; %store torque measure at wheels for gear 'o' CT2(1,o)=CarEngineTorque; %store torque mesured at engine for efficiency calculations CE(1,o)=CarEngineEfficiency; %store efficiency in gear 'o' CR(1,o)=CarEngineRPM; end %determine which gear is producing the maximum wheel torque at the given vehicle %speed. Choose this gear, and save the number of the most effective gear ito the variable %'maxIndex' [~,maxIndex]=max(CT); %store the current gear at current timestep in variable gearNumber gearNumber(i,1)=maxIndex; %Assign the wheel torque and engine torque associated with the chosen gear TotalWheelTorque=CT(1,maxIndex); CarEngineTorque=CT2(1,maxIndex); %Assign the engine efficiency associated with the chosen gear CarEngineEfficiency=CE(1,maxIndex); CarEngineRPM=CR(1,maxIndex); EffLog(i,1)=CarEngineEfficiency; %Determine fuel consumption of the car CarEnginekWh=(CarEngineTorque*CarEngineRPM)/(9565*3600);%determine kWh of energy actually used for propulsion during the timestep, 3600 factor converts hourly value to seconds FuelkWh=CarEnginekWh/CarEngineEfficiency;%divide by engine efficiency to get kWh of fuel required GramsFuel(i,1)=FuelkWh/FuelLowerHeatingVal; %divide by lower heating value to get grams of fuel used per second %Determine actual propulsive force at the wheels WheelForce=TotalWheelTorque/WheelRadius; %calculate new acceleration (at the start of the 1 second timestep),and car %velocity and car displacement (at the end of the timestep) CarAcceleration=(WheelForce-drag-rollingResistance)/CarMass; CarLinearVelocity=CarLinearVelocity+CarAcceleration; CarDisplacement=CarDisplacement+CarLinearVelocity; AccelLog(i,1)=CarAcceleration; VelLog(i,1)=CarLinearVelocity; DispLog(i,1)=CarDisplacement; end FinishLine = size(DispLog( 1:find( DispLog > 1000, 1 ) ),1); FuelTotal=sum(GramsFuel(1:FinishLine,1)); if FinishLine>0 x1=sprintf('\nYour car crosses the finish line after %i seconds.\nThis requires %d litres of fuel.', FinishLine, FuelTotal/1000); disp(x1) else x1=sprintf('\nYour car fails to cross the finish line within the alloted time!'); disp(x1) end %% Produce output plots subplot(3,2,1) plot(DispLog) xlabel('Time (s)') ylabel('Car Displacement (m)') subplot(3,2,2) plot(VelLog) xlabel('Time (s)') ylabel('Car Velocity (m/s)') subplot(3,2,3) plot(AccelLog) xlabel('Time (s)') ylabel('Car Acceleration (m/s^2)') subplot(3,2,4) plot(gearNumber) ylim([0 6]) xlabel('Time (s)') ylabel('Gear Number') subplot(3,2,5) plot(GramsFuel) xlabel('Time (s)') ylabel('Fuel Consumption (g/s)') subplot(3,2,6) plot(EffLog) xlabel('Time (s)') ylabel('Engine Efficiency') %clear CarAcceleration CarLinearVelocity CE CR CT CT2 drag FinalDriveRatio FinishLine FuelTotal WheelForce TotalWheelTorueq FuelkWh i maxIndex o rollingResistance rolResCoeff WheelRadius WheelCircumference x1 idleRPM maxEngineRPM SpeedEfficiency CarDisplacement CarEngineEfficiency CarEngineRPM CarEngineTorque CarEnginekWh SpeedTorque TotalWheelTorque cD aF CarOnlyMass CarWheelRPM DriverMass FuelLowerHeatingVal gearRatios CarMass
openfig('FossilFuel_M_Output.fig');
type FossilFuel_T.m
%calculates the result of a 1km drag race for %a user define vehicle powered by an internal %combustion engine clear cD=0.33; %car dragcoefficient aF=1.78; %Cr frontal area CarOnlyMass=1220;% mass of car without driver kg DriverMass=80;%kg rolResCoeff=0.012; %rolling resistance coefficient FuelLowerHeatingVal=0.01206; %lower heating value of fuel in kWh/g - equals 0.01206 for petrol gearRatios=[2.860 %ratios of gears 1:5, you can add more if you have more than 5 forward gears 1.911 1.410 1.1 0.87]; FinalDriveRatio=3.620; SpeedTorque=[0 50 1000 70 2000 160 3000 160 4000 145 5000 125 6000 105 7000 60 7500 0 1E50 0]; %[rpm nm] maxEngineRPM=6500; WheelRadius=0.32;%m idleRPM=700; %The minimum engine speed before stalling, in this model we assume that all engine torque is transmitted at this speed, even when the clutch is slipping during start-up. Though not enirely true it makes little difference to this model %% %% CALCULATION SECTION: You do not need to alter anything here SpeedEfficiency=[700 0.309946464 1000 0.317506134 2000 0.325443787 3000 0.329563329 4000 0.325443787 5000 0.317506134 6000 0.295857988 7000 0.265668398 7500 0.250341375]; CarMass=CarOnlyMass+DriverMass; CarLinearVelocity=0; CarDisplacement=0; for i=1:100 drag=cD*aF*0.5*1.225*(CarLinearVelocity^2); rollingResistance=rolResCoeff*CarMass*9.81; WheelCircumference=pi()*2*WheelRadius; CarWheelRPM=60*(CarLinearVelocity/WheelCircumference); %RPM of car wheel numGears=size(gearRatios,1); for o=1:numGears CarEngineRPM=max(CarWheelRPM*FinalDriveRatio*gearRatios(o,1),idleRPM); if CarEngineRPM>maxEngineRPM %if calcs suggest that engine would have to rotate faster than it is permitted to in order to maintain the cars speed at the given gear... CarEngineTorque=0; TotalWheelTorque=0; %no or negative torque, as engine slows car down CarEngineEfficiency=interp1(SpeedEfficiency(:,1)',SpeedEfficiency(:,2)',maxEngineRPM); %engine efficiency based on max RPM; else CarEngineTorque=interp1(SpeedTorque(:,1)',SpeedTorque(:,2)',CarEngineRPM); %engine provides torque based on its actual RPM TotalWheelTorque=(CarEngineTorque*FinalDriveRatio*gearRatios(o,1)); %Nm CarEngineEfficiency=interp1(SpeedEfficiency(:,1)',SpeedEfficiency(:,2)',CarEngineRPM);%engine efficiency based on its actual RPM; end % calculate and store torque and efficiency, and ngine RPM for each gear CT(1,o)=TotalWheelTorque; %store torque measure at wheels for gear 'o' CT2(1,o)=CarEngineTorque; %store torque mesured at engine for efficiency calculations CE(1,o)=CarEngineEfficiency; %store efficiency in gear 'o' CR(1,o)=CarEngineRPM; end %determine which gear is producing the maximum wheel torque at the given vehicle %speed. Choose this gear, and save the number of the most effective gear ito the variable %'maxIndex' [~,maxIndex]=max(CT); %store the current gear at current timestep in variable gearNumber gearNumber(i,1)=maxIndex; %Assign the wheel torque and engine torque associated with the chosen gear TotalWheelTorque=CT(1,maxIndex); CarEngineTorque=CT2(1,maxIndex); %Assign the engine efficiency associated with the chosen gear CarEngineEfficiency=CE(1,maxIndex); CarEngineRPM=CR(1,maxIndex); EffLog(i,1)=CarEngineEfficiency; %Determine fuel consumption of the car CarEnginekWh=(CarEngineTorque*CarEngineRPM)/(9565*3600);%determine kWh of energy actually used for propulsion during the timestep, 3600 factor converts hourly value to seconds FuelkWh=CarEnginekWh/CarEngineEfficiency;%divide by engine efficiency to get kWh of fuel required GramsFuel(i,1)=FuelkWh/FuelLowerHeatingVal; %divide by lower heating value to get grams of fuel used per second %Determine actual propulsive force at the wheels WheelForce=TotalWheelTorque/WheelRadius; %calculate new acceleration (at the start of the 1 second timestep),and car %velocity and car displacement (at the end of the timestep) CarAcceleration=(WheelForce-drag-rollingResistance)/CarMass; CarLinearVelocity=CarLinearVelocity+CarAcceleration; CarDisplacement=CarDisplacement+CarLinearVelocity; AccelLog(i,1)=CarAcceleration; VelLog(i,1)=CarLinearVelocity; DispLog(i,1)=CarDisplacement; end FinishLine = size(DispLog( 1:find( DispLog > 1000, 1 ) ),1); FuelTotal=sum(GramsFuel(1:FinishLine,1)); if FinishLine>0 x1=sprintf('\nYour car crosses the finish line after %i seconds.\nThis requires %d litres of fuel.', FinishLine, FuelTotal/1000); disp(x1) else x1=sprintf('\nYour car fails to cross the finish line within the alloted time!'); disp(x1) end %% Produce output plots subplot(3,2,1) plot(DispLog) xlabel('Time (s)') ylabel('Car Displacement (m)') subplot(3,2,2) plot(VelLog) xlabel('Time (s)') ylabel('Car Velocity (m/s)') subplot(3,2,3) plot(AccelLog) xlabel('Time (s)') ylabel('Car Acceleration (m/s^2)') subplot(3,2,4) plot(gearNumber) ylim([0 6]) xlabel('Time (s)') ylabel('Gear Number') subplot(3,2,5) plot(GramsFuel) xlabel('Time (s)') ylabel('Fuel Consumption (g/s)') subplot(3,2,6) plot(EffLog) xlabel('Time (s)') ylabel('Engine Efficiency') %clear CarAcceleration CarLinearVelocity CE CR CT CT2 drag FinalDriveRatio FinishLine FuelTotal WheelForce TotalWheelTorueq FuelkWh i maxIndex o rollingResistance rolResCoeff WheelRadius WheelCircumference x1 idleRPM maxEngineRPM SpeedEfficiency CarDisplacement CarEngineEfficiency CarEngineRPM CarEngineTorque CarEnginekWh SpeedTorque TotalWheelTorque cD aF CarOnlyMass CarWheelRPM DriverMass FuelLowerHeatingVal gearRatios CarMass
openfig('FossilFuel_T_Output.fig');

Answers (1)

Tushar
Tushar on 15 Dec 2023
Hi,
I understand that you are trying to compare the fuel consumption of two cars. One approach is to make a function that calls each script and returns the desired results, as shared in the below MATLAB Answer.
Another approach of comparing the plots in a single figure would be to output the plots as CSV files and using subplot to compare them side-by-side. The following code will compare the ‘DispLog plots of both the files FossilFuel_M.m and FossilFuel_T.m’.
% Run the first script and save the output(s)
FossilFuel_M;
writematrix(DispLog,"Disp_M.csv");
% Run the second script and save the output(s)
FossilFuel_T;
writematrix(DispLog,"Disp_T.csv");
% Load the data from the CSV files
a = readmatrix('Disp_M.csv');
b = readmatrix('Disp_T.csv');
% Create a new figure for plotting
new_figure = figure;
% Plot the first car's data in a subplot
subplot(1,2,1);
plot(a);
title("CAR_M");
% Plot the second car's data in a subplot
subplot(1,2,2);
plot(b);
title("CAR_T");
You can modify the code to plot all 6 plots for each car with appropriate titles and axes labels. To know more about 'writemmatrix' and 'readmatrix' functions, see the below documentations.
I hope this will be helpful.
Best,
Tushar

Categories

Find more on 2-D and 3-D Plots 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!