# Simulations in Matlab

Matlab → Basics → Scripts, Functions → Programming → Graphs → Simulations

The behavior of many real-life system or phenomenon, whether biological, economic, physical, or even sociological, are often described in mathematical terms. The mathematical description of a system of phenomenon is called a mathematical model. And, simulation is a computer program which attempts to represent the real world based on that mathematical model. The accuracy of the simulation depends on the precision of the model.

Plotting Random Numbers
We know that the Matlab command

`U=randn(10,1)`

generates 10 normally (or Gaussian) distributed random numbers. In the next example, we are going to generate 1000 normally distributed random numbers and will plot them.

```U=randn(500,1); plot(U,'o')``` The above command plotted the random numbers. Note that another way of writing the above program is

```n=500; U=randn(n,1); plot(U,'o')```

In the next example, we will generate 10,000 normally distributed random numbers and will plot then in a histogram.

```n=10000; U=randn(n,1); bars = 30; hist(U,bars)``` Simulating a Dice
The `randi` command is used to return a random integer, and the command `randi(6)` returns a random integer between 1 and 6, inclusive. Similarly, the command

`>> dice=randi(6,10,1)`

returns a 10×1 matrix (10 rows and 1 column) of random integers between 1 and 6, inclusive.

Now, roll two dices and calculate sum of the numbers return. Simulate the experiment for 1000 times and plot a histogram. Show that the distribution is normal. For this, the Matlab code looks like,

```dice1=randi(6,1000,1); dice2=randi(6,1000,1); dice=dice1+dice2; bars=11; hist(dice,bars)``` NOTE: For the sum of two dices, the total outcomes are 11 (i.e. 2, 3, 4, …, 12). So, the histogram is divided in 11 bins (or bars).

Posted