Docsity

Lecture 11: Probability and Random Numbers in MATLAB, Study notes of Computer Science

Partial preview of the text

Download Lecture 11: Probability and Random Numbers in MATLAB and more Study notes Computer Science in PDF only on Docsity!

„^ Previous Lecture:^ „

User-defined functions^ „^

Examples with varying numbers of input and output parameters „ Local memory space

„^ Today’s Lecture:^ „

Probability and random numbers „ 1-d array—vector „ More MATLAB graphics

„^ Announcement:^ „

Prelim 1 will be returned at end of lecture. If your paperisn’t here, pick it up from CS1112 consultants in ACCELduring consulting hrs (today after 4pm) „ Read from

Insight

February 24, 2009

Lecture 11

Random numbers „^ Pseudorandom

numbers in programming

„^ Function

rand(…)

generates random real

numbers in the interval (0,1). All numbers in theinterval (0,1) are equally likely to occur—uniformprobability distribution. „ Examples:

rand(1)

one random # in (0,1)

6*rand(1)

one random # in (0,6)

6*rand(1)+

one random # in (1,7)

February 24, 2009

Lecture 11

Sanity check: rand and randn >>^

n=^

>>^

x=^

rand(n,1);

>>^

ave=

sum(x)/n

ave

>>^

y=^

randn(n,1);

>>^

ave=

sum(y)/n

ave

>>^

stdDev=

std(y)

stdDev

February 24, 2009

Lecture 11

Random noise „^ Plot y=sin(x) across [0,

π] and add random noise

if „ Say, noise is .1*randn if x is in above range „ Two versions: vectorized vs. non-vectorized

π^

≤^ x

February 24, 2009

Lecture 11

1-d array:

vector

„^ An array is a named collection of like dataorganized into rows or columns „^ A 1-d array is a row or a column, called a

vector

„^ An

index

identifies the position of a value in a

vector

1

2

3

4

5

6

score

February 24, 2009

Lecture 11

Array index starts at 1

Let k be the index of vector x, then „^ k must be a positive integer „^ 1<= k <= length(x) „^ To access the k

th^ element: x(k)

.^

-4^ -

7

x

1 2

3

4 5

6

February 24, 2009

Lecture 11

(rand(1)6)*

February 24, 2009

Lecture 11

round(rand(1)6)*

February 24, 2009

Lecture 11

round(rand(1)6)*

(rand(1)6)*

February 24, 2009

Lecture 11

round(rand(1)6)*

ceil(rand(1)6)* 2

February 24, 2009

Lecture 11

Possible outcomes from rolling a fair 6-sided die^1

February 24, 2009

Lecture 11

Simulation^1

February 24, 2009

Lecture 11

Simulation^1

February 24, 2009

Lecture 11 3

Simulation^1