Header, Specification, Body Input Parameter List Output Parameter List

Slides:



Advertisements
Similar presentations
Simulation Examples in EXCEL Montana Going Green 2010.
Advertisements

User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.
Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops.
Problems and solutions Session 3. Introduction to MATLAB - Solutions 3 Problems 1. Write function Xn = mspolygon(X,x0,a) that scales the INPUT polygon.
Managerial Decision Making and Problem Solving Computer Lab Notes 1.
AB 11 22 33 44 55 66 77 88 99 10  20  19  18  17  16  15  14  13  12  11  21  22  23  24  25  26  27  28.
Simulation Operations -- Prof. Juran.
Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
Activity 1 Activity 2 Index Student Activity 1: Tossing a coin Student Activity 2: Tossing two coins.
Probability A Coin Toss Activity. Directions: Each group will toss a fair coin ten times. On the worksheet, they will record each toss as a heads or tails.
Random Variables and Expectation. Random Variables A random variable X is a mapping from a sample space S to a target set T, usually N or R. Example:
Randomisation in Coin Tosses We can’t predict the result of one coin toss with certainty but we have an expectation that with 10 tosses we will get about.
1. Variance of Probability Distribution 2. Spread 3. Standard Deviation 4. Unbiased Estimate 5. Sample Variance and Standard Deviation 6. Alternative Definitions.
COMP53311 Data Stream Prepared by Raymond Wong Presented by Raymond Wong
 Monday, 10/28/02, Slide #1 CS106 Introduction to CS1 Monday, 10/28/02  QUESTIONS on HW 03??  Today: Generating random numbers  Reading & exercises:
CSE 221: Probabilistic Analysis of Computer Systems Topics covered: Expectation of random variables Moments (Sec )
Diffusion Mechanisms for Active Queue Management Department of Electrical and Computer Engineering University of Delaware May 19th / 2004 Rafael Nunez.
UNR, MATH/STAT 352, Spring Random variable (rv) – central concept of probability theory Numerical description of experiment.
CSE 3504: Probabilistic Analysis of Computer Systems Topics covered: Moments and transforms of special distributions (Sec ,4.5.3,4.5.4,4.5.5,4.5.6)
SAMPLING DISTRIBUTIONS. SAMPLING VARIABILITY
Chapter 7 Expectation 7.1 Mathematical expectation.
Chapter 5 Probability Distributions
UNR, MATH/STAT 352, Spring Head Tail Tossing a symmetric coin You are paying $1 How much should you get to make the game fair?
Math – Getting Information from the Graph of a Function 1.
Simulation Examples ~ By Hand ~ Using Excel
© 2001 Prentice-Hall, Inc.Chap 5-1 BA 201 Lecture 7 The Probability Distribution for a Discrete Random Variable.
Aim: How do we use binomial probability? Complete worksheet.
Probability Simulation The Study of Randomness.  P all  P all.
Lesson Objective Understand how we can Simulate activities that have an element of chance using probabilities and random numbers Be able to use the random.
CALCULATE THE PROBABILITY OF AN EVENT. 1.ANSWER THIS QUESTION: IS THE EVENT POSSIBLE? STOP: DON’T CONTINUE. THE PROBABILITY OF THE EVENT IS O GO TO NUMBER.
Even more problems.. Mean (average) I need a program that calculates the average of student test scores. I need a program that calculates the average.
Introduction to the Essentials of Excel COMP 066.
COMPSCI 102 Introduction to Discrete Mathematics.
1 BA 555 Practical Business Analysis Linear Programming (LP) Sensitivity Analysis Simulation Agenda.
4.3 Functions. Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them.
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
Probability Lesson 32Power Up GPage 210. Probability.
Csci 418/618 Simulation Models Dr. Ken Nygard, IACC 262B
Tail Score 11Tail Score 8 Tail Score 9 Tail Score 10Tail Score 7 Tail Score 6.
Information Bottleneck versus Maximum Likelihood Felix Polyakov.
PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)
Math 1320 Chapter 7: Probability 7.3 Probability and Probability Models.
Probability. Today we will look at… 1.Quick Recap from last week 2.Terminology relating to events and outcomes 3.Use of sample spaces when dealing with.
Probability 100% 50% 0% ½ Will happen Won’t happen
Probability Tree for tossing a coin.
Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based on input values Some applications.
Experimental probability
Pettit 9-2 Notes D7 : Compute probabilities using tree diagrams
The Random Class and its Methods
كيــف تكتـب خطـة بحـث سيئـة ؟؟
الدكتـور/ عبدالناصـر محمـد عبدالحميـد
Value returning Functions
Functions, Part 2 of 2 Topics Functions That Return a Value
Area of a circle GIVENS: radius RESULTS: INTERMEDIATES:
History of Probability
كار همراه با آسودگي و امنيت
Probability: Test Tomorrow
Unit 8. Day 10..
ECO173: Applied Statistics II Introductory Concepts & Random Variables
Ab Initio Profile HMM Generation
Review for Test1.
Probability of TWO EVENTS
Probability: Test Tomorrow
Aggregate Functions.
Discrete Random Variables: Basics
Discrete Random Variables: Basics
Simulation Berlin Chen
Lecture 9 Randomized Algorithms
def-ining a function A function as an execution control structure
Discrete Random Variables: Basics
Presentation transcript:

Header, Specification, Body Input Parameter List Output Parameter List 12. More on Functions Header, Specification, Body Input Parameter List Output Parameter List Built-Ins: randn, imag, real,max, min, ginput Insight Through Computing

Keep tossing a fair coin until | Heads – Tails | == N 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. Insight Through Computing

function nTosses = Gap(N) The Packaging… function nTosses = Gap(N) 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 Insight Through Computing

function nTosses = Gap(N) The Header… function nTosses = Gap(N) output parameter list input parameter list Insight Through Computing

The necessary output value is computed. 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 The necessary output value is computed. Insight Through Computing

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 Insight Through Computing

Explicitly assign output value at the end. 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 nTosses = n; Explicitly assign output value at the end. Insight Through Computing

function nTosses = Gap(N) The Specification… function nTosses = Gap(N) % 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. Insight Through Computing

Estimate Expected Value of Gap(N) Strategy: Play “Gap N” a large number of times. Compute the average “score.” That estimates the expected value. Insight Through Computing

A very common methodology for the estimation of expected value. 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. Insight Through Computing

Sample Outputs N = 10 Expected Value = 98.67 Insight Through Computing

Solution… N = input('Enter N:'); nGames = 10000; s = 0; for k=1:nGames s = s + Gap(N); end ave = s/nGames; Program development is made easier by having a function that handles a single game. Insight Through Computing

What if the Game Was Not “ Packaged”? for k=1:nGames score = Gap(N) s = s + score; end ave = s/nGames; Insight Through Computing

A more cumbersome implementation for k=1:nGames score = Gap(N) s = s + score; end ave = s/nGames; 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 score = nTosses; A more cumbersome implementation Insight Through Computing

Is there a Pattern? N = 10 Expected Value = 98.67 Insight Through Computing

New Problem Estimate expected value of Gap(N) for a range of N-values, say, N = 1:30 Insight Through Computing

Estimate expected value of Gap(N) Display the estimate. end Pseudocode for N=1:30 Estimate expected value of Gap(N) Display the estimate. end Insight Through Computing

Estimate expected value of Gap(N) Display the estimate. end Pseudocode for N=1:30 Estimate expected value of Gap(N) Display the estimate. end Refine this! Insight Through Computing

Done that.. nGames = 10000; s = 0; for k=1:nGames s = s + Gap(N); end ave = s/nGames; Insight Through Computing

Sol’n Involves a Nested Loop for N = 1:30 % Estimate the expected value of Gap(N) s = 0; for k=1:nGames s = s + Gap(N); end ave = s/nGames; disp(sprintf('%3d %16.3f',N,ave)) Insight Through Computing

Sol’n Involves a Nested Loop for N = 1:30 % Estimate the expected value of Gap(N) s = 0; for k=1:nGames s = s + Gap(N); end ave = s/nGames; disp(sprintf('%3d %16.3f',N,ave)) But during derivation, we never had to reason about more than one loop. Insight Through Computing

Maybe increase nTrials to solidify conjecture. Output N Expected Value of Gap(N) -------------------------------- 1 1.000 2 4.009 3 8.985 4 16.094 28 775.710 29 838.537 30 885.672 Looks like N2. Maybe increase nTrials to solidify conjecture. Insight Through Computing

Generate random quadratic q(x) = ax2 + bx + c Eg. 2: Random Quadratics Generate random quadratic q(x) = ax2 + bx + c If it has real roots, then plot q(x) and highlight the roots. Insight Through Computing

Sample Output Insight Through Computing

Built-In Function: randn % Uniform for k=1:1000 x = rand; end % Normal x = randn; Insight Through Computing

Built-In Functions: imag and real x = 3 + 4*sqrt(-1); y = real(x) z = imag(x) Assigns 3 to y. Assigns 4 to z. Insight Through Computing

Built-In Functions: min and max a = 3, b = 4; y = min(a,b) z = max(a,b) Assigns 3 to y. Assigns 4 to z. Insight Through Computing

Packaging the Coefficient Computation function [a,b,c] = randomQuadratic % a, b, and c are random numbers, % normally distributed. a = randn; b = randn; c = randn; Insight Through Computing

Input & Output Parameters function [a,b,c] = randomQuadratic A function can have more than one output parameter. Syntax: [v1,v2,… ] A function can have no input parameters. Syntax: Nothing Insight Through Computing

Computing the Roots function [r1,r2] = rootsQuadratic(a,b,c) % a, b, and c are real. % r1 and r2 are roots of % ax^2 + bx +c = 0. r1 = (-b - sqrt(b^2 - 4*a*c))/(2*a); r2 = (-b + sqrt(b^2 - 4*a*c))/(2*a); Insight Through Computing

[r2,r1] = rootsQuadratic(c,b,a); r1 = r1 Question Time function [r1,r2] = rootsQuadratic(a,b,c) r1 = (-b - sqrt(b^2 - 4*a*c))/(2*a); r2 = (-b + sqrt(b^2 - 4*a*c))/(2*a); a = 4; b = 0; c = -1; [r2,r1] = rootsQuadratic(c,b,a); r1 = r1 Output? A. 2 B. -2 C. .5 D. -.5 Insight Through Computing

Answer is B. We are asking rootsQuadratic to solve -x2 + 4 = 0 roots = +2 and -2 Since the function ca ll is equivalent to [r2,r1] = rootsQuadratic(-1,0,4); Script variable r1 is assigned the value that rootsQuadratic returns through output parameter r2. That value is -2 Insight Through Computing

Generate a random quadratic Compute its roots If the roots are real Script Pseudocode for k = 1:10 Generate a random quadratic Compute its roots If the roots are real then plot the quadratic and show roots end Insight Through Computing

Generate a random quadratic Compute its roots If the roots are real Script Pseudocode for k = 1:10 Generate a random quadratic Compute its roots If the roots are real then plot the quadratic and show roots end [a,b,c] = randomQuadratic; Insight Through Computing

[a,b,c] = randomQuadratic; Compute its roots If the roots are real Script Pseudocode for k = 1:10 [a,b,c] = randomQuadratic; Compute its roots If the roots are real then plot the quadratic and show roots end [r1,r2] = rootsQuadratic(a,b,c); Insight Through Computing

[a,b,c] = randomQuadratic; [r1,r2] = rootsQuadratic(a,b,c); Script Pseudocode for k = 1:10 [a,b,c] = randomQuadratic; [r1,r2] = rootsQuadratic(a,b,c); If the roots are real then plot the quadratic and show roots end if imag(r1)==0 && imag(r2)===0 Insight Through Computing

[a,b,c] = randomQuadratic; [r1,r2] = rootsQuadratic(a,b,c); Script Pseudocode for k = 1:10 [a,b,c] = randomQuadratic; [r1,r2] = rootsQuadratic(a,b,c); if imag(r1)==0 && imag(r2)==0 then plot the quadratic and show roots end Insight Through Computing

Plot the Quadratic and Show the Roots m = min(r1,r2); M = max(r1,r2); x = linspace(m-1,M+1,100); y = a*x.^2 + b*x + c; plot(x,y,x,0*y,':k',r1,0,'or',r2,0,'or') Insight Through Computing

Plot the Quadratic and Show the Roots m = min(r1,r2); M = max(r1,r2); x = linspace(m-1,M+1,100); y = a*x.^2 + b*x + c; plot(x,y,x,0*y,':k',r1,0,'or',r2,0,'or') This determines a nice range of x-values. Insight Through Computing

Plot the Quadratic and Show the Roots m = min(r1,r2); M = max(r1,r2); x = linspace(m-1,M+1,100); y = a*x.^2 + b*x + c; plot(x,y,x,0*y,':k',r1,0,'or',r2,0,'or') Array ops get the y-values. Insight Through Computing

Plot the Quadratic and Show the Roots m = min(r1,r2); M = max(r1,r2); x = linspace(m-1,M+1,100); y = a*x.^2 + b*x + c; plot(x,y,x,0*y,':k',r1,0,'or',r2,0,'or') Graphs the quadratic. Insight Through Computing

Plot the Quadratic and Show the Roots m = min(r1,r2); M = max(r1,r2); x = linspace(m-1,M+1,100); y = a*x.^2 + b*x + c; plot(x,y,x,0*y,':k',r1,0,'or',r2,0,'or') A black, dashed line x-axis. Insight Through Computing

Plot the Quadratic and Show the Roots m = min(r1,r2); M = max(r1,r2); x = linspace(m-1,M+1,100); y = a*x.^2 + b*x + c; plot(x,y,x,0*y,':k',r1,0,'or',r2,0,'or') Highlight the root r1 with red circle. Insight Through Computing

Plot the Quadratic and Show the Roots m = min(r1,r2); M = max(r1,r2); x = linspace(m-1,M+1,100); y = a*x.^2 + b*x + c; plot(x,y,x,0*y,':k',r1,0,'or',r2,0,'or') Highlight the root r2 with red circle. Insight Through Computing

Complete Solution for k=1:10 [a,b,c] = randomQuadratic; [r1,r2] = rootsQuadratic(a,b,c); if imag(r1)==0 m = min(r1,r2); M = max(r1,r2); x = linspace(m-1,M+1,100); y = a*x.^2 + b*x + c; plot(x,y,x,0*y,':k',r1,0,'or',r2,0,'or') shg pause(1) end Insight Through Computing