Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. # Estimating the area under the curve x^2 using random points
  2.  
  3. # Given function
  4. parabola <- function(x)
  5. {
  6. return(x^2)
  7. }
  8. plot(parabola)
  9.  
  10. # Definite integral over the interval [a,b]
  11. areaintegrparab <- function(a,b)
  12. {
  13. return(b^3/3-a^3/3)
  14. }
  15.  
  16. # Simulation approach to the calculation of the area
  17. # a is the lower bound of [a,b]
  18. # b is the upper bound of [a,b]
  19. # c is the number of random points to be used
  20. # e is the maximum allowed error (in percentage points)
  21. areamt <- function(a,b,c,e)
  22. {
  23. randx<- runif(c,a,b)
  24. randy<- runif(c,min(parabola(a),parabola(b)),max(parabola(a),parabola(b)))
  25. c <- c(0)
  26. h = 1
  27. n = 0
  28. m = 1
  29. o = 1
  30. vecy = c(0)
  31. vecx = c(0)
  32. for(number in randy)
  33. {
  34. if(number< parabola(randx[h]))
  35. {
  36. vecy[m]<-number
  37. vecx[o]<-randx[h]
  38. n = n + 1
  39. h = h + 1
  40. m = m + 1
  41. o = o + 1
  42. }else
  43. {
  44. h = h + 1
  45. }
  46. }
  47.  
  48. # Points ratio
  49. pr=n/length(randx)
  50.  
  51. # Estimated area
  52. mca = n/length(randx)*abs(b-a)*abs(max(parabola(a),parabola(b))-min(parabola(a),parabola(b)))
  53.  
  54. # Exact area
  55. aint = areaintegrparab(a,b)
  56.  
  57. # Error
  58. change = (mca-aint)/aint*100
  59.  
  60. # Vector containing the summary of the entire process
  61. data = c(pr,mca,aint,change)
  62.  
  63. # Let's put some labels
  64. if(change>=0)
  65. {
  66. namechange = "% Overestimated"
  67. }else
  68. {
  69. namechange = "% Underestimated"
  70. }
  71. names(data)=c("Points ratio","Estimated area","Real area",namechange)
  72.  
  73. # Check if error is compatible with the maximum error allowed
  74. # If TRUE, data is printed and plotted, else the process is reapeted with more points
  75. if(abs(change) < e)
  76. {
  77. print(data)#Estimating the area under the curve x^2 using random points
  78.  
  79. # Given function
  80. parabola <- function(x)
  81. {
  82. return(x^2)
  83. }
  84. plot(parabola)
  85.  
  86. # Definite integral over the interval [a,b]
  87. areaintegrparab <- function(a,b)
  88. {
  89. return(b^3/3-a^3/3)
  90. }
  91.  
  92. # Simulation approach to the calculation of the area
  93. # a is the lower bound of [a,b]
  94. # b is the upper bound of [a,b]
  95. # c is the number of random points to be used
  96. # e is the maximum allowed error (in percentage points)
  97. areamt <- function(a,b,c,e)
  98. {
  99. randx<- runif(c,a,b)
  100. randy<- runif(c,min(parabola(a),parabola(b)),max(parabola(a),parabola(b)))
  101. c <- c(0)
  102. h = 1
  103. n = 0
  104. m = 1
  105. o = 1
  106. vecy = c(0)
  107. vecx = c(0)
  108. for(number in randy)
  109. {
  110. if(number< parabola(randx[h]))
  111. {
  112. vecy[m]<-number
  113. vecx[o]<-randx[h]
  114. n = n + 1
  115. h = h + 1
  116. m = m + 1
  117. o = o + 1
  118. }else
  119. {
  120. h = h + 1
  121. }
  122. }
  123.  
  124. # Points ratio
  125. pr=n/length(randx)
  126.  
  127. # Estimated area
  128. mca = n/length(randx)*abs(b-a)*abs(max(parabola(a),parabola(b))-min(parabola(a),parabola(b)))
  129.  
  130. # Exact area
  131. aint = areaintegrparab(a,b)
  132.  
  133. # Error
  134. change = (mca-aint)/aint*100
  135.  
  136. # Vector containing the summary of the entire process
  137. data = c(pr,mca,aint,change)
  138.  
  139. # Let's put some labels
  140. if(change>=0)
  141. {
  142. namechange = "% Overestimated"
  143. }else
  144. {
  145. namechange = "% Underestimated"
  146. }
  147.  
  148. names(data)=c("Points ratio","Estimated area","Real area",namechange)
  149.  
  150. # Check if error is compatible with the maximum error allowed
  151. # If TRUE, data is printed and plotted, else the process is reapeted with more points
  152. if(abs(change) < e)
  153. {
  154. print(data)
  155. plot(parabola,xlim=c(a,b),ylim=c(parabola(a),parabola(b)),col="red",main="Estimating the area under the curve")
  156. points(vecx,vecy,col="green")
  157. return(n/length(randx)*abs(b-a)*abs(max(parabola(a),parabola(b))-min(parabola(a),parabola(b))))
  158. }else
  159. {
  160. return(areamt(a,b,c+1000,e))
  161. }
  162. }
  163.  
  164. # Calculate the area under the curve between 0 and 2 with 30000 points
  165. # and a maximum allowed error of 2%
  166. areamt(0,3,30000,2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement