Matlab 1 Microsoft Word Document 1 PDF
Matlab 1 Microsoft Word Document 1 PDF
A computer simulation is a computer program which attempts to represent the real world based
on a model. The accuracy of the simulation depends on the precision of the model. Suppose that
the probability of heads in a coin toss experiment is unknown. We can perform the experiment of
tossing the coin n times repetitively to approximate the probability of heads.
P(H) = (Number of times heads observed/ Number of times the experiment executed).
However, for many practical problems it is not possible to determine the probabilities by
executing experiments a large number of times. With today’s computers processing capabilities,
we only need a high-level language, such as MATLAB, which can generate random numbers, to
deal with these problems. In this chapter, we present basic methods of generating random
variables and simulate probabilistic systems. The provided algorithms are general and can be
implemented in any computer language. However, to have concrete examples, we provide the
actual codes in MATLAB. If you are unfamiliar with MATLAB, you should still be able to
understand the algorithms.
Generate Discrete Uniform Random Numbers
Pick a random sample of 10 from a list of 553 items.
rng default; % for reproducibility
numbers = unidrnd(553,1,10)
numbers =
451 501 71 506 350 54 155 303 530 534
1 1 1 1 1
v = 1×5
1 4 9 16 25
Compute and Plot the Normal Distribution pdf
Compute the pdf of a standard normal distribution, with parameters μ equal to 0 and σ equal to 1.
x = [-3:.1:3];
norm = normpdf(x,0,1);
Plot the pdf.
figure;
plot(x,norm)
Multinomial Probability Distribution Objects
This example shows how to generate random numbers, compute and plot the pdf, and compute
descriptive statistics of a multinomial distribution using probability distribution objects.
Probabilities:
0.5000 0.3333 0.1667
Each element in the resulting matrix is the outcome of one trial. The columns correspond to the five
trials in each experiment, and the rows correspond to the eight experiments. For example, in the first
experiment (corresponding to the first row), one of the five trials resulted in outcome 1, one of the
five trials resulted in outcome 2, and three of the five trials resulted in outcome 3.
The plot shows the probability mass for each k possible outcome. For this distribution, the pdf value
for any x other than 1, 2, or 3 is 0.
The x-axis of the plot shows the number of items drawn that are of the desired type. The y-axis
shows the corresponding cdf values.
Linear regression models can be useful for the study of relations between two data series. Matlab
provides different commands to estimate linear regression coefficients and corresponding
statistics.
Linear regression and correlation coefficient
Least squares fit can be performed by the command regress. Assume a linear system
randn('seed',1);
x = [1:10:500]';
y = 2.5 * x + 80 + 50 * randn(50,1);
plot(x,y,'.')
and estimate the linear fit and the confidence intervals within 95% with
78.1034
2.4832
bint =
50.3461 105.8608
2.3859 2.5805
In order to get not only an estimate for the regression coefficient, but also the offset, we added a vector
consisting of ones in the second argument of regress. The result b yields the offset 85 and the
regression coefficient 2.5, the corresponding confidence intervals are stored in bint.
hold on
plot(1:500, b(2) * [1:500] + b(1))
end
end
a1=(n*sumxy-sumx*sumy)/(n*sumx2-sumx*sumx)%calculate the b
a0=ym-a1*xm%calculate the a
r2=(sumst-sumsr)/sumst
syx=(sumsr/(n-2))^0.5;
r2=(sumst-sumsr)/sumst