STAT 540 Homework 3 - Fall 2019 (see NOTES at bottom) 1. (a) Using simulation techniques, write an R program to maximize the following function of one variable: h(x) = 0.25 + cos(x)*(1/(sqrt(x)*(1+sqrt(x))^2) + 0.08*exp(-0.5*(x-5)^2)) over the interval [2, 8], i.e., for 2 <= x <= 8. (b) Plot this function in R as a smooth curve. Write commands to have R put text within your plot that writes both the location of the maximum and the maximum value of the function. Write R code to draw an arrow pointing at the peak where the maximum occurs. (c) Using simulation techniques, evaluate the definite integral of h(x) = 0.25 + cos(x)*(1/(sqrt(x)*(1+sqrt(x))^2) + 0.08*exp(-0.5*(x-5)^2)) over the region of integration from x=2 to x=8. For a variety of choices of n (say, n=1000, 10000, 100000), try both the hit-and-miss method and the classical Monte Carlo integration method. Which one appears to converge faster to the eventual answer? 2. (a) Using simulation techniques, write an R program to maximize the following function of two variables, where x = (x1,x2) is a vector of length 2: h = 3 - 3*x[1]/((x[1]-0.2)^2 + x[2]^2 +1) over the region: x1 in [-2, 2], x2 in [-2, 2]. Report both the maximum value of h(x1, x2) and where (i.e., the (x1, x2) location) it occurs. (b) Write R code to do a 3-D plot of this function of x1 and x2 (this response surface). Hint: Look carefully at the first example at the bottom of the help file for the 'persp' function. You can adapt that example code to plot this function as a 3-D plot. Also: Try the ticktype="detailed" argument to the 'persp' function. (c) Using the simulation technique of your choice, evaluate the definite integral of the function from problem 2(a), over the region of integration: x1 from -2 to 2, x2 from -2 to 2. Use a variety of choices of n (use n=1000, 10000, 100000). Do the results appear to converge toward some answer? 3. (a) Write your own R function that will accept a vector as its input and will return as its output the product of powers of the elements of the input vector. That is, given an input exponent, the function should raise each element of the input vector to that exponent, and then multiply together the resulting numbers to yield the output, which is a single number. Your function should have two arguments: (1) the object that is the input vector, and (2) the object that is the input exponent. Do not provide a default value for the first argument, but make the default value of the input exponent be 2. HINT: The built-in 'prod' function in R may be useful to you! (b) Use your function to return the product of the squared elements of the vector: c(2, 5, 3, -7) (c) Use your function to return the product of the cubed elements of the vector: c(2, 9, -4) NOTE: You MUST intersperse comments (lines that start with #) in your code to explain what your R statements are supposed to be doing. Please be generous with your comments, since you will be graded not only on the correctness of the code, but partially on the clarity of comments. NOTE: PLEASE put WITHIN COMMENTS (in lines that start with #) any text (i.e., if you choose to include problem numbers, problem description, your personal comments) in your file that is not actual R code. This will make it easier and faster to grade. The grader should be able to copy and paste your entire file into R and have it run correctly. NOTE: Please DO NOT INCLUDE the R output that your code produces in the text file that you submit. ONLY have the text file include your R code (and your explanatory comment lines, preceded by # symbols). NOTE: Please save your code as a plain text file (.txt file). Submit your solution code via uploading it in Blackboard (instructions are on the course web page). You should use the Chrome or Firefox browser when uploading, not Safari!