Mslab All 2982
Mslab All 2982
1. Write a program to
implement a random number
generator.
2. Write a program to
implement linear
congruential generator
3. Write a program to
implement testing of
Random numbers (Chi
Square)
4. Write a program to
implement Monte Carlo
Simulation
5. Write a program to
implement simulation of
LAG model
6. Write a program to
implement simulation of
Single Queue Server System
7. Write a program to
implement simulation of
Multiple Queue Server
System
8. Write a program to
implement simulation of a
conveyor belt system
9. Write a program to
implement simulation of
inventory system
10. Write a program to
implement COBWEB model
CourseName:Modelling & Simulation Lab Course code: CSP-443
Experiment:1
Description:
Rand:- Uniformly distributed random numbers
Randn:- Normally distributed random numbers
Randi:- Uniformly distributed pseudorandom integers
Pseudo code/Algorithms/Flowchart/Steps:
Rand:
X = rand returns a random scalar drawn from the uniform distribution in the interval (0,1). X = rand(n) returns
an n-by-n matrix of uniformly distributed random numbers.
X =rand(sz1,...,szN) returns an sz1-by-...-by-szN array of random numbers
where sz1,...,szN indicate the size of each dimension. For example, rand(3,4) returns a 3-by-4 matrix.
X = rand(sz) returns an array of random numbers where size vector sz defines size(X). For example, rand([3
4]) returns a 3-by-4 matrix.
X=rand( ,typename) returns an array of random numbers of data type
typename. The typename input can be either "single" or "double".
Randn:
X = randn returns a random scalar drawn from the standard normal distribution. X = randn(n) returns an n-by-
n matrix of normally distributed random numbers.
X = randn( ,typename) returns an array of random numbers of data type typename.
The typename input can be either "single" or "double". You can use any of the input arguments in the previous
syntaxes.
Experiment:2
Description:
Formula for the linear congruential generator is
X(i+1)= mod(a*(x(i)+c),m)
Where,
X(i+i) is the random numbers generated a is the multiplier
x(i) is Seed (previous random number generated)
c is incremental value m is the modulus conditions: m>0 a< m x
Pseudo code/Algorithms/Flowchart/Steps:
a = 25; c = 37;
m = 200;
x = zeros(1,200);
x(1) = 7;
for i=2:numel(x)
x(i) = mod(a*x(i-1)+c,m)
end
Experiment: 3
Description:
h = chi2gof(x) returns a test decision for the null hypothesis that the data in vector x comes from a normal
distribution with a mean and variance estimated from x, using the chi-square goodness of-fit test. The
alternative hypothesis is that the data does not come from such a distribution. The result h is 1 if the test
rejects the null hypothesis at the 5% significance level, and 0 otherwise. h = chi2gof(x,Name,Value) returns
a test decision for the chi-square goodness-of-fit test with additional options specified by one or more
name-value pair arguments. For example, you can test for a distribution other than normal, or change the
significance level of the test.
Pseudo code/Algorithms/Flowchart/Steps:
PROGRAM:
h = chi2gof(x)
h = chi2gof(x,Name,Value)
Experiment: 4
Description:
Monte Carlo simulation is a method for exploring the sensitivity of a complex system by
varying parameterswithin statistical constraints. These systems can include financial,
physical, and mathematical models that are simulated in a loop, with statistical uncertainty
between simulations. The results from the simulation are analysed to determine the
characteristics of the system.
Pseudo code/Algorithms/Flowchart/Steps:
N=10000;
head=0;
tail=0;
for
i=1:N
if rand<=0.5
head=head+1;
plot(head,tail,'
g.'); else
tail=tail+1;
plot(head,tail,'
r.'); end
hold
on; end
hold off;
prob_t=head
/N;
prob_h=tail
/N;
Experiment:5
Pseudo code/Algorithms/Flowchart/Steps:
• Generate matrix Y
• Set Lag X , that you want to introduce
• Use lagmatrix(Y,X) to use
• Print output
Output:
Experiment:6
Aim:
Write a program to implement simulation of Single Queue Server System.
Software Required:
MATLAB R2013A, Personal computer, Simulink.
Theory:
This example shows how to model a single-queue single-server system with a single traffic source and an
infinite storage capacity. In the notation, the M stands for Markovian; M/M/1 means that the system has a
Poisson arrival process, an exponential service time distribution, and one server. Queuing theory provides
exact theoretical results for some performance measures of an M/M/1 queuing system and this model makes
it easy to compare empirical results with the corresponding theoretical results.
Time Based Entity Generator block: It models a Poisson arrival process by generating entities (also known
as "customers" in queuing theory).
Exponential Interarrival Time Distribution subsystem: It creates a signal representing the interarrival
times for the generated entities. The interarrival time of a Poisson arrival process is an exponential random
variable.
FIFO Queue block: It stores entities that have yet to be served.
Single Server block: It models a server whose service time has an exponential distribution.
Code:
Output:
Experiment:7
Aim:
Write a program to implement simulation of Multiple Queue Server System.
Software Required:
MATLAB R2013A, Personal computer, Simulink.
Theory:
The purpose of this LiveScript is to simulate M/M/N queuing system, where N is the number of servers and
queues with unlimited space for customers. There are 4 variables that needs to be defined by user:
N = number of servers and queues ( N of servers = N of queues)
Tk = time duration of simulation in minutes
mi0 = arrival rate in minutes/customer arrival, i.e. mi0 = 0,5 -> customer arrives every 30 sec
mi1 = service rate in minutes/customer served, i.e. mi1 = 2,5 -> customer is served in 2 minutes and 30 sec
The arrival and service rates are not static, but they are distributed by an exponential distribution. The
random seed of rand in exponential distribution is managed by rng(44) at the start of the script.
The script is divided into sections by their purpose. The first section serves to set parameters by user. This is
the only section where user should edit variables. Then there is pre-allocation section, with approximation
pre-allocation of some vectors. Third is simulation section. Fourth is vector edit section. Vectors that have
been approximately pre-allocated are here trimmed. Fifth section contains text outputs. Sixth section
contains graph outputs with gpu parallelization enabled.
Variable names in text outputs:
ppc(i) - average number of customers in queue(i) at any time of simulation
maxf(i) - maximum number of customers in queue(i) at time
vetappz - average number of customers in the whole system at any time
cpz - total number of customers in system
maxpz - maximum number of customers in system at time
pvl(i) - average workload of server(i) in %
pcc(i) - average waiting time in queue(i)
NC - total number of served customers
Here are some examples of ways to combine FIFO Queue and Single Server blocks to model different
situations:
• Serial Queue-Server Pairs
Output:
Experiment No: 8
DESCRIPTION: -
This example shows how to build a simple inventory management system for a retail store. This example
includes:
• Random customer arrivals to the store with the number of products requested by each customer also
randomly distributed
• Tracking available inventory at the end of the day
• Tracking and disposal of expired products
• Placing periodic orders for fresh products
• Store profitability analysis
FLOWCHART: -
IMPLEMENTATION: -
• Transportation: The Transportation block represents the delay, which is the duration between a
product's dispatch from the warehouse and its arrival at the store. The default delay is set to two
hours.
• Store Tag: This area models the part of the retail store that receives products from the warehouse
and applies the 'Product' and 'Expiry' tags on them. These tags allow us to search for products later
on.
• Customers Entry: Represents customers entering the store to pick up products from the shelves
and their departure from the store. This is modeled using the 'Entity Find' block, which looks for
entities in the system that have the 'Product' tag associated with them.
• Store Shelf: This area contains a Queue where the products are stored. Customers pick up products
from here. An 'Entity Gate' that is perpetually closed ensures that products don't flow out of the
store.
• Expired Products Removal: This area models the periodic removal of expired products from the
store's shelves. This is modeled using the 'Entity Find' block. The find block is triggered periodically
to perform a search for entities that have the 'Product' tag associated with them. It then looks for
products that have exceeded the shelf life and discards them.
• Accounting: This area models the investigation of profitability of the retail store for the duration
between consecutive product ordering points. The profit is calculated as a function of the product
procurement price, product holding cost and the product selling price. The profit also plays a role
in determining the number of products that the retail store orders. If the store is profitable in the
current period, then the new quantity to be ordered is the sum of the previously ordered quantity
plus any unfulfilled orders. This is also adjusted for expired and unsold products.
• Periodic Order Placement: This area models periodic order placement by the retail store. An order
is placed with the Warehouse for supplying a fresh batch of products to the retail store. You can
specify the period by setting the value of the reorder point.
SAMPLE OUTPUT: -
The model is simulated for 60 days. One unit of simulation time represents 1 minute of wall clock time. Based
on the model parameters set, plots are generated showing the number of products sold, the number of
customers who arrived at the store, the product order size, number of expired products in the store and the
store's profitability. Observe that for each period, the optimal store order quantity is around 85 for the given
customer arrival rate.
Experiment No: 9
Online Compiler
DISCUSSION: -
Lost calls are any inbound calls that do not result in the caller being connected to either an advisor or an
answering service. This happens when the customer hangs up or is disconnected by the centre. The centre
may disconnect a call deliberately, because of timeout – the call was taking too long to reach an agent – or
through error.
Categories of Lost Call
• Abandoned – The customer terminates the call before it is answered. The industry standard suggests
that an abandon rate of 2-5% is commonplace.
• Missed – The call is deliberately disconnected by the centre. This usually occurs when an incoming
call reaches the maximum threshold for waiting time set by the ACD.
• Dropped – The call is accidentally disconnected due to a technical error. The dropped call rate for
landline calls is below 0.01%. The rate for mobile phone calls is slightly higher, but a dropped call
rate of even a few per cent would warrant further investigation.
STEPS: -
1. Scan the potential event.
2. Select the activity that is to cause the event.
3. Test whether the potential event that can be executed.
4. Change records to reflect the event.
5. Gather the statistics of simulation output.
IMPLEMENTATION:
clear all;
Tracking available inventory at the end of the dayhome;
C = 100; %Number of calls made ( 100 taken for ease of comparision) mIAT = input('Enter
mean inter arrival time (mins):');
callInt =exprnd(mIAT,[C-1,1]); %Inter arrival time for 100 calls
SAMPLE OUTPUT:-
Simulation of a trunked communication network with N channels. Total of 100 calls are considered for
simulation. The network is a Lost Calls Cleared type system
Experiment No: 10
DESCRIPTION: -
The cobweb model or cobweb theory is an economic model that explains why prices might
be subject to periodic fluctuations in certain types of markets . It describes cyclical supply
and demand in a market where the amount produced must be chosen before prices are
observed. The cobweb model is generally based on a time lag between supply and demand
decisions. Agricultural markets are a context where the cobweb model might apply, since
there is a lag between planting and harvesting (gives two agricultural examples: rubber
and corn). Suppose for example that as a result of unexpectedly bad weather, farmers go
to market with an unusually small crop of strawberries. This shortage, equivalent to a
leftward shift in the market's supply curve, results in high prices. If farmers expect these
high price conditions to continue, then in the following year, they will raise their
production of strawberries relative to other crops. Therefore, when they go to market the
supply will be high, resulting in low prices. If they then expect low prices to continue,
they will decrease their production of strawberries for the next year, resulting in high
prices again.
The cobweb model can have two types of outcomes:
• If the supply curve is steeper than the demand curve, then the fluctuations decrease in
magnitude with each cycle, so a plot of the prices and quantities over time would look like an inward
spiral, as shown in the first diagram. This is called the stable or convergent case.
• If the demand curve is steeper than the supply curve, then the fluctuations increase in
magnitude with each cycle, so that prices and quantities spiral outwards. This is called the unstable or
divergent case.
IMPLEMENTATION: -
function cobweb(f,a,b,x0,N)
x=linspace(a,b,N);
y=f(x);
hold on;
plot(x,y,'k');
line([x(1),x(1)],[0,f(x(1))]);
line([x(1),f(x(1))],[f(x(1)),f(x(1))]); for i=1:N
x(i+1)=f(x(i));
line([x(i+1),x(i+1)],[x(i+1),f(x(i+1))]);
line([x(i+1),f(x(i+1))],[f(x(i+1)),f(x(i+1))])
end
hold off;
function
ret=f(x)
ret=2*x.*(1-x);
SAMPLE OUTPUT: -
The sample output graph will be COBWEB which will depend upon the input function and values
that we chose.