Consildation of two program

3 views (last 30 days)
ann
ann on 23 Jul 2022
Answered: Star Strider on 23 Jul 2022
Hi everyone, May I ask how to combine the two programs,and then the data of the two programs can be displayed in one report, thank you for your help!
  1 Comment
dpb
dpb on 23 Jul 2022
In general without actually studying either, write a top-level script of function that calls each and returns the desired results.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 23 Jul 2022
I would duplicate them as function files with the appropriate input arguments and outputs, and then call them from a specifc script wirtten to do just that. Do whatever is necessary in the calling script to get the information you want from them. It would likely be easiest to return the vector arguments of the plot calls (inculding the appropriate time vectors that are not currently used in any of them) to the calling script, and then do the actual plotting there, rather than in the functions.
I am not certain what else you would be interested in returning to the calling script, however if it is numeric data, either create table arrays in the functions and return them as outputs, or create the table arrays in the calling script from the data returned by the functions.
url1 = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1074905/Electriccar_M.m';
url2 = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1074910/FossilFuel_M.m';
opts = weboptions('ContentType','text');
W1 = webread(url1, opts)
W1 =
'%calculates the result of a 1km drag race for %a user define vehicle powered by an electric %motor %Porsche Taycan Sport Turismo (Y1A) Turbo S 93.4 kWh (761 Hp) clear cD=0.22; %car dragcoefficient%https://en.wikipedia.org/wiki/Porsche_Taycan#:~:text=panel's%20power%20threshold.-,Aerodynamics,of%20any%20current%20Porsche%20model. aF=2.66; %Cr frontal area %https://zeperfs.com/en/fiche8119-porsche-taycan-turbo-s.htm CarOnlyMass=2295;%mass of car without driver kg%https://zeperfs.com/en/fiche8119-porsche-taycan-turbo-s.htm DriverMass=80;%kg rolResCoeff=0.012; %rolling resistance coefficient BattPowerRating=58; %Battery maximum operating power (kW); BattEnergyCapacity=83.7; %Battery Energy Capcity (kWh) FinalDriveRatio=7.9377; %the gear ratio of the gear that steps down motor speed to wheel speed SpeedTorque=[0 1050 %https://zeperfs.com/en/fiche8119-porsche-taycan-turbo-s.htm#Specs 1500 1050 4000 1050 5000 971 5750 929 6500 855 7000 763 7750 689 13750 388 15500 362 10000 0]; %[rpm nm] maxEngineRPM=15500; %https://zeperfs.com/en/fiche8119-porsche-taycan-turbo-s.htm %% CALCULATION SECTION: You do not need to alter anything here SpeedEfficiency=[0 0.86 1000 0.87 2000 0.90 3000 0.93 4000 0.94 5000 0.95 6000 0.95 7000 0.95 8000 0.91 9000 0.88 10000 0.87 10500 0.86]; % Adjust torque for situations in which the battery is not powerful enough to provide torque at a given RPM for i=1:size(SpeedTorque,1) if (SpeedTorque(i,1)*SpeedTorque(i,2))/9565 > BattPowerRating SpeedTorque(i,2)=(BattPowerRating*SpeedEfficiency(i,2))*9565/SpeedTorque(i,1); SpeedPowerCurve(i,1)=SpeedTorque(i,1); SpeedPowerCurve(i,2)=BattPowerRating; else SpeedPowerCurve(i,1)=SpeedTorque(i,1); SpeedPowerCurve(i,2)=(SpeedTorque(i,1)*SpeedTorque(i,2))/9565; end end WheelRadius=0.32;%m idleRPM=0; %Electric vehicles can start from zero motor speed, so idle RPM=0 CarMass=CarOnlyMass+DriverMass; CarLinearVelocity=0; CarDisplacement=0; RemainingEnergy=BattEnergyCapacity; 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 CarEngineRPM=max(CarWheelRPM*FinalDriveRatio,idleRPM); if or(CarEngineRPM>maxEngineRPM,0>=RemainingEnergy) %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); %Nm CarEngineEfficiency=interp1(SpeedEfficiency(:,1)',SpeedEfficiency(:,2)',CarEngineRPM);%engine efficiency based on its actual RPM; end %Assign the engine efficiency associated with the chosen gear 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 BatterykWh=CarEnginekWh/CarEngineEfficiency;%divide by engine efficiency to get kWh of fuel required if isnan(BatterykWh) TotalkWh(i,1)=0; else TotalkWh(i,1)=BatterykWh; end %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; RemainingEnergy=max(RemainingEnergy-BatterykWh,0); AccelLog(i,1)=CarAcceleration; VelLog(i,1)=CarLinearVelocity; DispLog(i,1)=CarDisplacement; RemainLog(i,1)=RemainingEnergy; end FinishLine = size(DispLog( 1:find( DispLog > 1000, 1 ) ),1); kWhTotal=sum(TotalkWh(1:FinishLine,1)); if FinishLine>0 x1=sprintf('\nYour car crosses the finish line after %i seconds.\nThis requires %d kWh of battery energy.', FinishLine, kWhTotal); disp(x1) else x1=sprintf('\nYour car fails to cross the finish line within the alloted time!'); disp(x1) end 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(TotalkWh) xlabel('Time (s)') ylabel({'Battery Energy Reqired','per second (kWh/s)'}) subplot(3,2,5) plot(EffLog) xlabel('Time (s)') ylabel('Engine Efficiency') subplot(3,2,6) plot(RemainLog) xlabel('Time (s)') ylabel('Remaining Battery Energy') 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 '
W2 = webread(url2, opts)
W2 =
'%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=[ 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=7500; 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 '
That is how I would approach it.
.

Categories

Find more on MATLAB Report Generator 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!