diff --git a/lab_2/SlottedAloha.svg b/lab_2/SlottedAloha.svg new file mode 100644 index 0000000..064cf42 --- /dev/null +++ b/lab_2/SlottedAloha.svg @@ -0,0 +1,290 @@ + + +Qt SVG Document +MATLAB, The MathWorks, Inc. Version 26.1.0.3203278 R2026a + + + + + + + + + + + + + +access probability + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 + + + +0.1 + + + +0.2 + + + +0.3 + + + +0.4 + + + +0.5 + + + +0.6 + + + +0.7 + + + +0.8 + + + +0.9 + + + +1 + + + +throughput + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 + + + +0.05 + + + +0.1 + + + +0.15 + + + +0.2 + + + +0.25 + + + +0.3 + + + +0.35 + + + +0.4 + + + +0.45 + + + + + + + + + + + + + + + + + + + + + + + +theoretical + + + +simulation + + + + + + + diff --git a/lab_2/Slotted_Aloha_Report.md b/lab_2/Slotted_Aloha_Report.md new file mode 100644 index 0000000..22f0f4d --- /dev/null +++ b/lab_2/Slotted_Aloha_Report.md @@ -0,0 +1,67 @@ +# Simulation of Slotted ALOHA Throughput Report + +**Timo Niemann** + +## Goal + +The goal of this simulation is to analyze the throughput behavior of Slotted ALOHA for a fixed number of senders. For every access probability `p`, each sender independently decides whether to transmit in the current time slot. A slot is successful only if exactly one sender transmits. Empty slots and collisions do not contribute to the throughput. + +## Simulation Setup + +| Parameter | Value | +|---|---| +| `number_of_senders` | `4` | +| `number_of_timeslots` | `1000` | +| `access_probability` | `0:0.01:1` | +| slot result | `1 = successful transmission`, `0 = no successful transmission` | + +## Throughput Formula + +For `n` independent senders, the theoretical throughput is the probability that exactly one sender transmits in a slot: + +```text +throughput = np(1 - p)^(n - 1) +``` + +With `n = 4`, this becomes: + +```text +throughput = 4p(1 - p)^3 +``` + +The theoretical maximum is reached at: + +```text +p = 1 / n = 0.25 +``` + +Therefore, the maximum theoretical throughput is: + +```text +throughput(0.25) = 4 * 0.25 * 0.75^3 = 0.421875 +``` + +## Throughput Curve Plot + +![Simulated and theoretical throughput curve for four senders](SlottedAloha.svg) + +**Figure 1:** Simulated and theoretical throughput curve for four senders. + +## Maximum Coordinates + +| Curve | Maximum coordinate | +|---|---| +| Theoretical | `p = 0.25`, throughput `= 0.421875` | +| Simulation | approximately `p = 0.28` to `0.30`, throughput `≈ 0.43` in the shown run | + +## Comparison with the Theoretical Formula + +The theoretical curve is smooth because it is calculated directly from the probability formula. The simulated curve follows the same overall shape, but it is not perfectly smooth because only `1000` time slots are simulated for each probability value. Therefore, random variation is still visible. + +The important behavior matches the theory. At low access probabilities, the throughput is low because most slots are empty. As `p` increases, successful transmissions become more likely and the throughput rises. Around `p = 0.25`, the system reaches its best operating region. After that point, too many senders transmit in the same slot, so collisions become more frequent and the throughput decreases. + +The simulated maximum is slightly shifted and slightly higher than the theoretical maximum in the shown run. This is expected for a finite simulation. With more time slots per probability value, the simulated curve should become smoother and the maximum should move closer to the theoretical coordinate `p = 0.25`, `S = 0.421875`. + +## Conclusion + +The simulation correctly reproduces the characteristic Slotted ALOHA throughput curve. The best throughput is obtained when the access probability is close to `1 / n`. For four senders, this is `p = 0.25`. Below this value, the channel is underused. Above this value, collisions dominate increasingly. The comparison shows that the implementation is consistent with the theoretical formula, while the visible deviations are caused by finite random sampling. diff --git a/lab_2/Slotted_Aloha_Report.pdf b/lab_2/Slotted_Aloha_Report.pdf new file mode 100644 index 0000000..78d0204 Binary files /dev/null and b/lab_2/Slotted_Aloha_Report.pdf differ diff --git a/lab_2/lab_2.asv b/lab_2/lab_2.asv index 764d029..e95330b 100644 --- a/lab_2/lab_2.asv +++ b/lab_2/lab_2.asv @@ -4,25 +4,34 @@ access_probability = 0:0.01:1; access_probability_length = length(access_probability); -sender_rands = zeros(number_of_senders); -% | slot1 | slot -% prob1 | +sender_access = zeros(number_of_senders); + +% | slot1 | slot2 | ... +% prob1 | | | ... +% prob2 | | | ... +% ... | ... | ...  | ... result = zeros(access_probability_length, number_of_timeslots); % simulate all time slots on each probability for probability_idx = 1 : access_probability_length - current_result = zeros(access_probability_length); + current_result = zeros(1, number_of_timeslots); probability = access_probability(probability_idx); - for i = 1 : number_of_timeslots - % generate random sender accesses - for j = 1 : number_of_senders - sender_rands(j) = rand(); + for time_slot = 1 : number_of_timeslots + % generate random sender accesses by probability + sender_access = zeros(1, number_of_senders); + for sender = 1 : number_of_senders + if rand() < access_probability(probability_idx) + sender_access(1, sender) = 1; + end end - + current_result(1, time_slot) = sum(sender_access(1, :)) == 1; end result(probability_idx, :) = current_result; -end \ No newline at end of file +end + +disp(result(floor(access_probability_length / 2), 1:100)) +plot(result); \ No newline at end of file diff --git a/lab_2/lab_2.m b/lab_2/lab_2.m index 52828ef..322b871 100644 --- a/lab_2/lab_2.m +++ b/lab_2/lab_2.m @@ -6,11 +6,8 @@ access_probability_length = length(access_probability); sender_access = zeros(number_of_senders); -% | slot1 | slot2 | ... -% prob1 | | | ... -% prob2 | | | ... -% ... | ... | ...  | ... -result = zeros(access_probability_length, number_of_timeslots); +simulated_throughput = zeros(1, access_probability_length); +estimated_throughput = zeros(1, access_probability_length); % simulate all time slots on each probability for probability_idx = 1 : access_probability_length @@ -29,8 +26,14 @@ for probability_idx = 1 : access_probability_length current_result(1, time_slot) = sum(sender_access(1, :)) == 1; end - - result(probability_idx, :) = current_result; + + simulated_throughput(probability_idx) = mean(current_result); + estimated_throughput(probability_idx) = number_of_senders * probability * (1 - probability)^(number_of_senders - 1); end -disp(result(floor(access_probability_length / 2), 1:100)) \ No newline at end of file +plot(access_probability, estimated_throughput) +hold on +plot(access_probability, simulated_throughput) +legend("theoretical", "simulation") +xlabel("access probability") +ylabel("throughput") \ No newline at end of file