0% found this document useful (0 votes)
60 views26 pages

ECE673 - Week1 - Lecture - With Figures

The document discusses using Matlab for computer simulation. It covers creating vectors and matrices in Matlab, basic functions like addition and plotting, simulating random events, and estimating characteristics from random data. Specific topics include generating random numbers, determining the probability density function and average value of random variables, and using histograms and the mean function to analyze random data in Matlab.

Uploaded by

蒋宇博
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views26 pages

ECE673 - Week1 - Lecture - With Figures

The document discusses using Matlab for computer simulation. It covers creating vectors and matrices in Matlab, basic functions like addition and plotting, simulating random events, and estimating characteristics from random data. Specific topics include generating random numbers, determining the probability density function and average value of random variables, and using histograms and the mean function to analyze random data in Matlab.

Uploaded by

蒋宇博
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Slide #0

ECE673 – Week 2
Computer Simulation
Learning Objectives
– Create vectors and matrices in the widely-used computer program
called Matlab, and perform numerical operations on them
– Become familiar with basic Matlab functions
– Plot functions and data in Matlab
– Understand how to simulate random events in Matlab
– Estimate characteristics such as average values and histograms in
Matlab
Slide #HOME

Week 1 – Lessons
Lesson 1.1Introduction to Matlab Lesson 1.2
Computer Simulation of Random Phenomen
a
Slide #1.1

Lesson 1.1

Introduction to Matlab

Note: NJIT students can download


Matlab from NJIT’s website.
Slide #1.2

Introduction to Matlab
• Overview of Matlab
– In MATLAB we can perform operations on scalars,
vectors and matrices.
– Examples of making scalars, vectors and matrices in
Matlab:
>> x=-2 >> x=[1;0;-3] >> x=[4,2,3;1,-1,0]

x= x= x=

-2 1 4 2 3
0 1 -1 0
-3
Slide #1.3

Overview of Matlab

• Addition, Multiplication, Transpose


>> x=[3,-1;8,0] >> z=x+y >> 3*x
y=[-2,9;7,2]
z= ans =
x=
1 8 9 -3
3 -1 15 2 24 0
8 0
>> z=x*y >> x'

y= z= ans =

-2 9 -13 25 3 8
7 2 -16 72 -1 0
Slide #1.4

Overview of Matlab
• Using colon “:” to specify intervening values
>> x=[3:6] >> a=[4:0.5:6]' >> b=[2:7;6:-2:-4]'

x= a= b=

3 4 5 6 4.0000 2 6
4.5000 3 4
>> y=[3:6]' 5.0000 4 2
5.5000 5 0
y= 6.0000 6 -2
7 -4
3
4
5
6
Slide #1.5

Overview of Matlab
• Element-by-element operation using the dot symbol
“.” before the operation
>> b=[-1:3] >> x=[3,-1;8,0] >> z=x./y

b= x= z=

-1 0 1 2 3 3 -1 -3.0000 -0.2500
8 0 4.0000 0
>> a=b.^2
>> y=[-1,4;2,5] >> t=x.*y
a=
y= t=
1 0 1 4 9
-1 4 -3 -4
2 5 16 0
Slide #1.6

Overview of Matlab
• Making a “for” loop:
– Using for loop to create a column vector where the i-th element is
equal to i 2

>> for i=1:5 >> x


x(i,1)=i^2;
end x =

1
4
9
16
25
Slide #1.7

Overview of Matlab
• Use of the “if” statement:
– Randomly simulate the digits 3 and -3 using the if statement and store
in a variable called x :
a=rand;
if a>0.5
x=3;
else
x=-3;
end
– In Matlab “rand” generates randomly a number between 0 and 1.
– The above code simulates the random variable x such that x is either
3 or -3, with a probability of 0.5.
Slide #1.8

Overview of Matlab
Slide #1.9

Useful MATLAB Functions


Slide #1.10

Plotting in Matlab
Slide #1.11

Plotting in Matlab

• Example:
9

clear all 8

x=[-3:0.5:3]; 7

y=x.^2; 6

plot(x,y) 5

2
y=x
xlabel('x') 4

ylabel('y=x^2') 3

grid 2

0
-3 -2 -1 0 1 2 3
x
Slide #End of Lesson

End of Lesson
Click here to return to the main menu.
Slide #2.1

Lesson 1.1

Computer Simulation of Random


Phenomena
Slide #2.2

Computer Simulation of Random Phenomena

• Why Use Computer Simulations?

– To build intuition and find new results.

– To understand and analyze complex problems that cannot be


handled mathematically and analytically.
Slide #2.3

Computer Simulation of Random Phenomena


• Basics of Monte Carlo (Random) Computer Simulations in
Matlab
Example: If the possible values of x are {-1.6,4,9} with probabilities {0.2,0.1,0.7},
then write a Matlab code to simulate 10 values of x (see a similar code in page 18 of
the textbook).
for i=1:10
u=rand; % generates a random number between 0 and 1
if u<=0.2
x(i,1)=-1.6;
elseif u>0.2 & u<=0.3
x(i,1)=4;
elseif u>0.3
x(i,1)=9;
end
end
Slide #2.4

Computer Simulation of Random Phenomena

• Determining Characteristics of Random Variables

– Probability Density Function

– Average Value
Slide #2.5

Determining Characteristics of Random


Variables
• Probability Density Function

Roughly speaking, the probability density function (PDF)


of a random variable is a function which shows the
chance of the random variable taking different values.
Example: PDF of a Gaussian or normal random
variable (which will be discussed thoroughly in week 3):
Slide #2.6

Probability Density Function (continued)


• Plotting the Gaussian or normal PDF between - 4 and 4
x=[-4:0.01:4];
plot(x,(1/sqrt(2*pi))*exp(-0.5*x.^2))
0.4

0.35

0.3

0.25

0.2

0.15

0.1

0.05

0
-4 -3 -2 -1 0 1 2 3 4
Slide #2.7

Probability Density Function (continued)


• The Matlab “randn” command generates Gaussian or normal
random numbers whose PDF is:
• To estimate the PDF of random 3000
data, one can use the “hist”
command in Matlab, which shows 2500
the histogram of data: 2000
y=randn(10000,1); % generates
10000 normal random numbers 1500

hist(y); % creates the histogram


of the data in y 1000

• Note: PDF graph in the previous 500


slide and the histogram here have
almost the same bell shape. 0
-4 -3 -2 -1 0 1 2 3 4
Slide #2.8

Determining Characteristics of Random Variables

• Average Value

– Suppose we have 6 observations x = [x1, x2, …, x6] and would


like to find their average.
– This is the formula for calculating the average:

1 6
average   xi
6 i 1
Slide #2.9

Average Value (continued)


• Finding the average in Matlab
– This code can be used to find the average of x1, x2, …, x6 :
x=[1 2 -4 0 3.7 11];
y=0;
for i=1:6
y=y+x(i);
end
average=y/6

– Or, we can directly use the “mean” function in Matlab:


x=[1 2 -4 0 3.7 11];
average=mean(x)
Slide #End of Lesson

End of Lesson
Click here to return to the main menu.
Reference

Kay, S. (2006). Intuitive probability and random


processes using Matlab. Springer.

You might also like