26
ESE 303: MATLAB tutorial Luiz Chamon and Kate Tolstaya August 31, 2017 Luiz and Kate ESE 303: MATLAB tutorial 1

ESE 303: MATLAB tutorial - Penn Engineering · Luiz and Kate ESE 303: MATLAB tutorial 3. The drunkard monks I Monks copy manuscripts Manuscripts are long # manuscripts/day ˘ Poisson

  • Upload
    doannhi

  • View
    223

  • Download
    2

Embed Size (px)

Citation preview

ESE 303: MATLAB tutorial

Luiz Chamon and Kate Tolstaya

August 31, 2017

Luiz and Kate ESE 303: MATLAB tutorial 1

https://alliance.seas.upenn.edu/∼ese303/wiki/index.php

Luiz and Kate ESE 303: MATLAB tutorial 2

Whirlwind tour of MATLAB

%% Some column vectors

x = (1:2:99)’;

y = zeros(50,1);

y(10) = 1;

%% Some matrices

A = [1 2 3 ; 4 5 6 ; 7 8 9];

I = eye(5);

%% Inner product: m = xTym = x’*y;

%% Counting/logical indexing

count = sum(q <= 0.3 & q >= 0.1);

Luiz and Kate ESE 303: MATLAB tutorial 3

The drunkard monks

I Monks copy manuscripts

Manuscripts are long# manuscripts/day ∼ Poisson

I Monks get drunk

Drunk monks don’t copymanuscripts

Luiz and Kate ESE 303: MATLAB tutorial 4

The drunkard monks

I Monks copy manuscripts

Manuscripts are long# manuscripts/day ∼ Poisson

I Monks get drunk

Drunk monks don’t copymanuscripts

Luiz and Kate ESE 303: MATLAB tutorial 4

The drunkard monks

I Monks copy manuscripts

Manuscripts are long# manuscripts/day ∼ Poisson

I Monks get drunk

Drunk monks don’t copymanuscripts

Luiz and Kate ESE 303: MATLAB tutorial 4

The drunkard monks: hidden Markov chain

Sober DrunkState-of-mind

Manuscriptoutput

Luiz and Kate ESE 303: MATLAB tutorial 5

The drunkard monks: hidden Markov chain

Sober DrunkState-of-mind

Manuscriptoutput

Luiz and Kate ESE 303: MATLAB tutorial 5

The drunkard monks: hidden Markov chain

Sober Drunk

0

State-of-mind

Manuscriptoutput

Luiz and Kate ESE 303: MATLAB tutorial 5

The drunkard monks: hidden Markov chain

Sober Drunk

0

State-of-mind

Manuscriptoutput

Luiz and Kate ESE 303: MATLAB tutorial 6

The drunkard monks: hidden Markov chain

Sober Drunk

0

State-of-mind

Manuscriptoutput

Luiz and Kate ESE 303: MATLAB tutorial 6

The drunkard monks: simulation

I Simulate 1 years (365 days)

I λs = 1 manuscript/day

I Initial state: sober

I Transition matrix

Sober DrunkSober 0.6 0.4Drunk 0.3 0.7

Luiz and Kate ESE 303: MATLAB tutorial 7

The drunkard monks: simulation

clear all

%% Setup

REAL = 365; % # of days

lambda_s = 1; % Avg # of manuscripts/day

% Transition matrix

P = [0.6 0.4 ; 0.3 0.7];

%% Simulation

state = zeros(REAL,1); % Sober (1) or drunk (2)

books = zeros(REAL,1); % # of manuscripts/day

state(1) = 1; % Initial state: sober (1)

Luiz and Kate ESE 303: MATLAB tutorial 8

The drunkard monks: simulation

for i = 1:REAL-1

% Monks manuscript output

% Monk state transition

end

Luiz and Kate ESE 303: MATLAB tutorial 9

The drunkard monks: simulation

% Monks manuscript output

if state(i) == 1

books(i) = poissrnd(lambda_s);

elseif state(i) == 2

books(i) = 0;

end

Luiz and Kate ESE 303: MATLAB tutorial 10

The drunkard monks: simulation

% Monk state transition

if state(i) == 1

% Monks are currently sober

p = rand(1);

if p <= P(1,1)

state(i+1) = 1; % Monks stay sober

else

state(i+1) = 2; % Monks decide to drink

end

elseif state(i) == 2

% Monks are currently drunk

...

end

Luiz and Kate ESE 303: MATLAB tutorial 11

The drunkard monks: simulation

I Sanity check: has any state not been updated?

I How many manuscripts in a year? How many manuscripts perday on average?

I Probability of being drunk?

Luiz and Kate ESE 303: MATLAB tutorial 12

The drunkard monks: simulation

I Sanity check: has any state not been updated?

sanity_check = sum(state == 0);

fprintf(’Errors: %d\n’, sanity_check);

Luiz and Kate ESE 303: MATLAB tutorial 13

The drunkard monks: simulation

I How many manuscripts in a year? How many manuscripts perday on average?

books_in_year = sum(books);

books_per_day = mean(books);

fprintf(’Total manuscripts: %d\n’, ...

books_in_year);

fprintf(’Average manuscripts/day: %.2f\n’, ...

books_per_day);

Luiz and Kate ESE 303: MATLAB tutorial 14

The drunkard monks: simulation

I Probability of being drunk?

p_drunk = sum(state == 2);

fprintf(’Pr[drunk] = %.2f\n’, p_drunk);

Luiz and Kate ESE 303: MATLAB tutorial 15

The drunkard monks: results

Day

50 100 150 200 250 300 350

Num

ber

of m

anuscripts

0

0.5

1

1.5

2

2.5

3

3.5

4Number of manuscripts per day

Manuscripts

Monks state

Luiz and Kate ESE 303: MATLAB tutorial 16

The drunkard monks: results

Day

50 60 70 80 90

Num

ber

of m

anuscripts

0

0.2

0.4

0.6

0.8

1

1.2Number of manuscripts per day

Luiz and Kate ESE 303: MATLAB tutorial 17

The drunkard monks: results

figure();

stem(1:REAL, books);

hold on;

stairs(1:REAL, state-1);

grid;

xlim([1 REAL]);

legend(’Manuscripts’, ’Monks state’);

xlabel(’Day’);

ylabel(’Number of manuscripts’);

title(’Number of manuscripts per day’);

Luiz and Kate ESE 303: MATLAB tutorial 18

The drunkard monks: results

Luiz and Kate ESE 303: MATLAB tutorial 19

The drunkard monks: results

figure();

histogram(books, ’BinMethod’, ’fd’, ...

’Normalization’, ’probability’);

hold on;

plot(0:6, poisson, ’--’, ’LineWidth’, 2);

plot(0:6, poisson_mix, ’LineWidth’, 2);

legend(’Histogram’, ’Poisson model’, ...

’Mixture model’);

xlabel(’Number of manuscripts per day’);

ylabel(’Probability’);

title(’Histogram of manuscripts per day’);

Luiz and Kate ESE 303: MATLAB tutorial 20

ESE 303: MATLAB tutorial

Luiz Chamon and Kate Tolstaya

https://alliance.seas.upenn.edu/∼ese303/wiki/index.php

Luiz and Kate ESE 303: MATLAB tutorial 21