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
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.
The above command plotted the random numbers. Note that another way of writing the above program is
In the next example, we will generate 10,000 normally distributed random numbers and will plot then in a histogram.
bars = 30;
Simulating a Dice
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
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,
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).