When i run this it only works for 1 car's 1 trip. Can anybody see how i can get all the trips.

1 次查看(过去 30 天)
so my issue is tripusage is only populated for 1 car's trip. i had expected it to do all trips and once it had done all trips it to move on to next car. likely issue with looping as there is 3 loops in this code. Thanks for any assistance.
QatarEV=174680;
% aveDemand=zeros(1,288);
tripusage=zeros(1,288);
% for iteration=1:100
% n = iteration;
time= 1:288;
sedanShare=0.46;
num.sedans = QatarEV * sedanShare;
num.sedans = round(num.sedans);
num.suvs = 174670 - num.sedans;
sedanTypes = ["Tesla3", "MercedesEQS", "TeslaS", "Hyundai"];
Sedan_Battery = [54;107.8;103;64]*1e3;
%sedan battery packs in kWh
suvTypes = ["Mercedes", "Nissan", "Toyota", "Renault", "VW"];
SUV_Battery = [28;40;40;41;35.8]*1e3;
charger=[3.6 7.2 22 50];
demand=zeros(1,288);
%DL driving licence, AP is active persons
% tripsedan= 0.357 +1.3681*DL + 2.4914*AP ;
% tripSUV= 0.5323 +0.9815*DL + 2.3961*AP;
tripsplit = [1 1; 1 2; 4 4; 8 5; 10 6; 9 8; 7 10; 7 7; 9 6; 7 9; 7 7; 8 8; 7 10; 8 7; 4 6; 2 4];
timeWindows2 = {[72 84], [84 96], [96 108],[108 120], [120 132],[132 144],[144 156],[156 168],[168 180],[180 192],[192 204],[204 216],[216 228],[240 252],[252 264],[264,276]};
tripmonday=[1,1,4,8,10,9,7,7,9,7,7,8,7,8,4,2];
timeWindows = {'6-7am', '7-8am', '8-9am', '9-10am', '10-11am', '11am-12pm', '12-1pm', '1-2pm', '2-3pm', '3-4pm', '4-5pm', '5-6pm', '6-7pm', '7-8pm', '8-9pm', '9pm-10pm'};
% Normalize the values in tripmonday
tripmonday_norm = tripmonday / sum(tripmonday);
% Compute the cumulative sum of the normalized probabilities
tripmonday_cum = cumsum(tripmonday_norm);
% temp details
t = readtable("qatarFIXED.csv",Range="A2:B745");
t.Properties.VariableNames = ["date","temp"];
t.date = datetime(t.date);
plot(0:23,reshape(t.temp,31,24)')
legend("July" + (1:31),Location="eastoutside")
ylabel("temperature")
xlabel("hour")
[g,hour] = findgroups(t.date.Hour);
meanTemp = splitapply(@mean,t.temp,g);
stdTemp = splitapply(@std,t.temp,g);
tempinfo=table(hour,meanTemp,stdTemp);
disp(tempinfo);
hold on
plot(0:23,meanTemp,'k:',LineWidth=3)
% Set the number of samples to generate for each hour
n_samples = 1;
% Pre-allocate a matrix to hold the generated temperatures
temperatures = zeros(n_samples, 24);
% Loop through each hour of the day
for i = 1:24
% Extract the mean and standard deviation for the current hour
mu = meanTemp(i);
sigma = stdTemp(i);
% Generate a list of temperatures for the current hour
temperatures(:, i) =(normrnd(mu, sigma, n_samples, 1));
temperaturesinterval= repelem(temperatures,12);
end
% Index 1: 1-2 hours/40-100km
index1_percent = 15.38;
% Index 2: 2-3 hours/90-180km
index2_percent = 34.07;
% Index 3: 3-4 hours/90-180km
index3_percent = 47.25;
% Index 4: over 4 hours/over 180km
index4_percent = 3.3;
%generate time windows for each sedan based on probability
for i = 1:num.sedans
chType=1+randi(3,1,1);%charger is Level 2 or fast
btType=randi(4,1,1);%indexfor battery size
sedanChosen = sedanTypes(btType);
SoCLevel=round(20 + (40-20).*rand(1,1));
DL = randi(2,1,1)-1;
AP = randi(3,1,1);
tripsedan= floor(0.357 +1.3681*DL + 2.4914*AP) ;
chosen_car_coeffs = coeffs_table(strcmp(coeffs_table.Vehicle, sedanChosen), :);
% Extract the p1, p2, p3 values for the chosen car
p1 = chosen_car_coeffs.p1;
p2 = chosen_car_coeffs.p2;
p3 = chosen_car_coeffs.p3;
rand_num = rand();
% Determine the range of hours and distance based on the random number
if rand_num < index1_percent/100
% Generate random number of 5-minute intervals between 1-2 hours
hours = randi([12,24])/12;
distance = randi([0,40]);
fivemindist= distance/(hours*12);
elseif rand_num < (index1_percent + index2_percent)/100
% Generate random number of 5-minute intervals between 2-3 hours
hours = randi([24,36])/12;
distance = randi([40,100]);
fivemindist= distance/(hours*12);
elseif rand_num < (index1_percent + index2_percent + index3_percent)/100
% Generate random number of 5-minute intervals between 3-4 hours
hours = randi([36,48])/12;
distance = randi([90,180]);
fivemindist= distance/(hours*12);
else
% Generate random number of 5-minute intervals over 4 hours
hours = randi([48,60])/12;
distance = randi([180,200]);
fivemindist= distance/(hours*12);
end
end
rand_num2 = rand * 0.33;
random_numbers = rand(1, tripsedan);
sum_of_numbers = sum(random_numbers);
thesplit = random_numbers / sum_of_numbers;
num_splits = length(thesplit);
tripdistances = zeros(1, num_splits);
for j= 1:tripsedan
% Determine the departure time for the trip
departure_time = find(tripmonday_cum >= rand_num2, 1);
% Get the corresponding time window from timeWindows2
departure_time_window = randi(timeWindows2{departure_time});
tripdistance = distance*thesplit(mod(j-1, length(thesplit)) + 1);
duration= tripdistance/fivemindist;
endtriptime= round(departure_time_window + duration);
for k= departure_time_window:endtriptime
x=temperaturesinterval(k);
tripusage(k)= (p1*x^2 + p2*x + p3)/12 *fivemindist ;
end
% UPDATE DEPARTURE TIME
departure_time_window=endtriptime + randi([6 36]);
end

回答(1 个)

Shushant
Shushant 2023-2-15
My understanding of your problem is that you are trying to get the data of all the trips of 1 car in the variable called “tripusage”. But when you execute the program, you get only 1 trip data i.e., 1 row and 288 columns. And after getting the correct data for the trip i.e., random number of rows and 288 columns, you wanted to extend your code in such a way that it gives “tripusage” data for “n” number of cars using “n” iterations.
While trying to reproduce your problem I was able to get the get the desired value of “tripusage” by modifying the line
tripusage(k)= (p1*x^2 + p2*x + p3)/12 *fivemindist ;
to
tripusage(j,k)= (p1*x^2 + p2*x + p3)/12 *fivemindist;
As the value in row 1 of tripusage was being overwriting again and again for each iteration of j instead of appending it on to the next row.
The main issue might be related to overwriting the variables in the for loops, review your code while keeping this in mind. And I hope this gives you a better idea on which parts you might be going wrong and helps you solve the problem.

类别

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

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by