products = {'M1', 'M2', 'M3', 'M4', 'M5'};
yearly_demand = [10000, 15000, 12000, 8000, 5000];
service_times = [15, 20, 25, 30, 35;
total_sim_time = 3600 * 24 * 365;
inter_arrival_times = total_sim_time ./ yearly_demand;
product_counters = zeros(1, length(products));
while current_time < total_sim_time
[min_time, next_product] = min(inter_arrival_times);
product_counters(next_product) = product_counters(next_product) + 1;
entities = [entities; struct('time', current_time, ...
'product', products{next_product}, ...
'service_times', service_times(:, next_product))];
current_time = current_time + min_time;
inter_arrival_times(next_product) = inter_arrival_times(next_product) + total_sim_time / yearly_demand(next_product);
disp('Simulating Manufacturing Line...');
station_count = size(service_times, 1);
station_timers = zeros(1, station_count);
for i = 1:length(entities)
disp(['Processing ', entity.product, ' at time ', num2str(entity.time), 's']);
for station = 1:station_count
station_timers(station) = max(station_timers(station), entity.time) + entity.service_times(station);
disp([' Station ', num2str(station), ' finishes at ', num2str(station_timers(station)), 's']);