NonMCAreas - s SSize <- as.numeric(ANSWER)...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
#Draws a function y=x^2 from 0 to 1 # FUNC <- function(title,loco) F { op <- par(bg = "white") x <- seq(0,1, len=61) x y <- seq(0,1, len=61) #R needs to know bounds of graphic. plot(x,x^2, type="l", main=title,ylab="Y",xlab="X") p par(op) }#Calculates an Area Using Darts # BULLSEYE<-function() B { cat("\n") cat("*****BULLS EYE*****","\n") cat("The Goal: The area beneath y=x^2 from 0 to 1","\n") a cat(" ","\n") cat("\n") #draw the circle FUNC("Y equal X squared",0) X #Determine the number of darts thrown ANSWER <- readline("What is the sample size? ")
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: s SSize &lt;- as.numeric(ANSWER) cat(&quot;\n&quot;) ANSWER &lt;- readline(&quot;Fire darts one at a time? &quot;) if (substr(ANSWER, 1, 1) == &quot;y&quot;) { cnt &lt;- 0 for (i in 1:SSize) { ANSWER &lt;- readline(&quot;Hit ENTER to FIRE WHEN READY!&quot;) A cat(&quot;\n&quot;) cat(&quot;FIRE!!!\n&quot;) c cat(&quot;\n&quot;) x=runif(1) y=runif(1) points(x,y) if (y&lt;x^2) cnt=cnt+1 } pct &lt;- cnt/SSize } else { xdarts&lt;-runif(SSize) ydarts&lt;-runif(SSize) points(xdarts,ydarts) p #darts cnt &lt;- 0 for (i in 1:SSize) { if (ydarts[i]&lt;xdarts[i]^2) cnt=cnt+1 c } pct&lt;-cnt/SSize } return(pct) r }BULLSEYE()...
View Full Document

This note was uploaded on 11/16/2010 for the course STAT 340 taught by Professor Xu(sunny)wang during the Spring '09 term at Waterloo.

Ask a homework question - tutors are online