CS 100 M Lecture 10 - 10. More on Functions Header,...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
10. More on Functions Header, Specification, Body Input Parameter List Output Parameter List Built-Ins: randn, imag, real,max, min, ginput
Background image of page 1

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

View Full DocumentRight Arrow Icon
Eg. 1: “Gap N” Keep tossing a fair coin until | Heads – Tails | == N Score = total # tosses Write a function Gap(N) that returns the score and estimate the average value.
Background image of page 2
The Packaging… Heads = 0; Tails = 0; nTosses = 0; while abs(Heads-Tails) < N nTosses = nTosses + 1; if rand <.5 Heads = Heads + 1; else Tails = Tails + 1; end end function nTosses = Gap(N)
Background image of page 3

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

View Full DocumentRight Arrow Icon
The Header… function nTosses = Gap(N) output parameter list input parameter list
Background image of page 4
The Body Heads = 0; Tails = 0; nTosses = 0; while abs(Heads-Tails) < N nTosses = nTosses + 1; if rand <.5 Heads = Heads + 1; else Tails = Tails + 1; end end The necessary output value is computed.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Local Variables Heads = 0; Tails = 0; nTosses = 0; while abs( Heads - Tails ) < N nTosses = nTosses + 1; if rand <.5 Heads = Heads + 1; else Tails = Tails + 1; end end
Background image of page 6
A Helpful Style Heads = 0; Tails = 0; n = 0; while abs(Heads-Tails) < N n = n + 1; if rand <.5 Heads = Heads + 1; else Tails = Tails + 1; end end nTosses = n; Explicitly assign output value at the end.
Background image of page 7

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

View Full DocumentRight Arrow Icon
The Specification… % Simulates a game where you % keep tossing a fair coin % until |Heads - Tails| == N. % N is a positive integer and % nTosses is the number of % tosses needed. function nTosses = Gap(N)
Background image of page 8
Estimate Expected Value of Gap(N) Strategy: Play “Gap N” a large number of times. Compute the average “score.” That estimates the expected value.
Background image of page 9

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

View Full DocumentRight Arrow Icon
Solution… N = input('Enter N:'); nGames = 10000; s = 0; for k=1:nGames s = s + Gap(N); end ave = s/nGames; A very common methodology for the estimation of expected value.
Background image of page 10
Sample Outputs N = 10 Expected Value = 98.67 N = 20 Expected Value = 395.64 N = 30 Expected Value = 889.11
Background image of page 11

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

View Full DocumentRight Arrow Icon
N = input('Enter N:'); nGames = 10000;
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/23/2008 for the course CS 100 taught by Professor Fan/vanloan during the Spring '07 term at Cornell University (Engineering School).

Page1 / 45

CS 100 M Lecture 10 - 10. More on Functions Header,...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online