! pip install numpy
! pip install sympy
! pip install scipy
! pip install matplotlib

```
Requirement already satisfied: numpy in /shared-libs/python3.7/py/lib/python3.7/site-packages (1.19.5)
WARNING: You are using pip version 20.1.1; however, version 21.0.1 is available.
You should consider upgrading via the '/root/venv/bin/python -m pip install --upgrade pip' command.
Requirement already satisfied: sympy in /shared-libs/python3.7/py/lib/python3.7/site-packages (1.7.1)
Requirement already satisfied: mpmath>=0.19 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from sympy) (1.2.1)
WARNING: You are using pip version 20.1.1; however, version 21.0.1 is available.
You should consider upgrading via the '/root/venv/bin/python -m pip install --upgrade pip' command.
Requirement already satisfied: scipy in /shared-libs/python3.7/py/lib/python3.7/site-packages (1.6.1)
Requirement already satisfied: numpy>=1.16.5 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from scipy) (1.19.5)
WARNING: You are using pip version 20.1.1; however, version 21.0.1 is available.
You should consider upgrading via the '/root/venv/bin/python -m pip install --upgrade pip' command.
Requirement already satisfied: matplotlib in /shared-libs/python3.7/py/lib/python3.7/site-packages (3.3.4)
Requirement already satisfied: pillow>=6.2.0 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from matplotlib) (8.1.2)
Requirement already satisfied: python-dateutil>=2.1 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from matplotlib) (2.8.1)
Requirement already satisfied: kiwisolver>=1.0.1 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from matplotlib) (1.3.1)
Requirement already satisfied: numpy>=1.15 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from matplotlib) (1.19.5)
Requirement already satisfied: cycler>=0.10 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from matplotlib) (0.10.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from matplotlib) (2.4.7)
Requirement already satisfied: six>=1.5 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from python-dateutil>=2.1->matplotlib) (1.15.0)
WARNING: You are using pip version 20.1.1; however, version 21.0.1 is available.
You should consider upgrading via the '/root/venv/bin/python -m pip install --upgrade pip' command.
```

import sympy as sp
import numpy as np
import scipy.stats as ss
import matplotlib.pyplot as plt
import queue
import copy
from matplotlib import style
style.use('bmh')
#Not to show warning messages (to keep the notebook clean)
import warnings
warnings.filterwarnings('ignore')

# 1a)
#Plotting using numpy
plt.figure(figsize=(14,8))
plt.rcParams.update({'font.size': 18})
#
AR =1
SR = 2
UT = AR/SR
num_K = np.linspace(0, 10 , 100)
num_blockrate = ((1 - UT)*UT**num_K)/(1 - UT**(num_K+1))
#
plt.plot(num_K, num_blockrate,'-',label='M/M/K')
#
plt.xlabel("$K$")
plt.ylabel("Block rate")
plt.legend()
plt.show()

# 3
# Visualization for sympy
sp.init_printing()
#Define a varible in sympy
G1 = sp.symbols('G')
G2 = sp.symbols('G')
S1 = G1 * sp.exp(-2*G1)
S1

S2 = G2 * sp.exp(-1*G2)
S2

diff_S1= sp.diff(S1,G1)
diff_S2= sp.diff(S2,G2)
opt_G1 = sp.solve(diff_S1, G1)
opt_G1

opt_G2 = sp.solve(diff_S2, G2)
opt_G2

opt_S1 = S1.subs(G1,opt_G1[0])
opt_S1

opt_S2 = S2.subs(G2,opt_G2[0])
opt_S2

#Plotting using numpy
plt.figure(figsize=(14,8))
plt.rcParams.update({'font.size': 18})
#
num_G1 = np.linspace(0, 3 , 100)
num_G2 = np.linspace(0, 3 , 100)
num_S1 = num_G1 * np.exp(-2*num_G1)
num_S2 = num_G2 * np.exp(-1*num_G2)
opt_G1num = 1/2
opt_G2num = 1
opt_S1num = 1/2 * np.exp(-1)
opt_S2num = np.exp(-1)
#
plt.plot(num_G1, num_S1,'-',label='Pure Aloha')
plt.plot(num_G2, num_S2,'-',label='Slotted Aloha')
plt.plot(opt_G1num, opt_S1num,'o',label='Optimal')
plt.plot(opt_G2num, opt_S2num,'o',label='Optimal')
#
plt.xlabel("$G$")
plt.ylabel("$S$")
plt.legend()
plt.show()