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

A part of lecture notes from a university course on matlab and numerical computing. It covers the topics of probability and random numbers, including uniform probability distribution, pseudorandom numbers, and generating random integers using matlab's rand and round functions. The document also includes examples and exercises.

Typology: Study notes

Pre 2010

Uploaded on 08/31/2009

koofers-user-3qn-1
koofers-user-3qn-1 🇺🇸

10 documents

1 / 39

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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 paper
isn’t here, pick it up from CS1112 consultants in ACCEL
during consulting hrs (today after 4pm)
Read from Insight
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

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