ps7solnsSp06

ps7solnsSp06 - %Engineering 6 Spring 2006 Problem 7.1...

This preview shows pages 1–2. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: %Engineering 6, Spring 2006, Problem 7.1 Solution %(Three six-sided dice simulation) %Suppress extra lines in output and set fixed short display format compact ; format short ; clc; %Clear command window and variables clear; %Set the random number generator to its initial state rand( 'state' ,0); %Get die rolls for 6-sided dice (500 rolls) die1 = floor(6*rand(1,500) + 1); die2 = floor(6*rand(1,500) + 1); die3 = floor(6*rand(1,500) + 1); result = die1 + die2 + die3; %Histogram computation and display in figure window 1 figure(1); xc = 3:18; %Set the histogram centers at 3,4,...18 absfreqs = hist(result,xc); %Calculate rel freq for each bin relfreqs = absfreqs/length(result); bar(xc,relfreqs), ylabel( 'Fractional number of results' ), ... xlabel( 'Number rolled (3-18)' ), ... title( '500 rolls of Three 6-Sided Dice' ); disp( 'For 500 rolls:' ) [maxfreq,index] = max(absfreqs); disp( 'The most frequent three-die number rolled was:' ); disp(index+2); disp( 'The number of times it was rolled was:' ); disp(maxfreq); [minfreq,index] = min(absfreqs); disp( 'The least frequent three-die number rolled was:' ); disp(index+2); disp( 'The number of times it was rolled was:' ); disp(minfreq); %Note that we have to add 2 to index (which gives the bin %number), because the first bin represents a roll of 3, %the second bin is a roll of 4, etc. %Reset the random number generator to its initial state and %repeat the simulation for 5,000 rolls, putting the plot %in figure window 2 figure(2); rand( 'state' ,0); die1 = floor(6*rand(1,5000) + 1); die2 = floor(6*rand(1,5000) + 1); die3 = floor(6*rand(1,5000) + 1); result = die1 + die2 + die3; %Histogram computation and display xc = 3:18; %Set the histogram centers at 3,4,...18%Set the histogram centers at 3,4,....
View Full Document

{[ snackBarMessage ]}

Page1 / 7

ps7solnsSp06 - %Engineering 6 Spring 2006 Problem 7.1...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online