## Question

> x=seq (1 ,10)

> x

[1] 1 2 3 4 5 6 7 8 9 10

Typing 1:10 is a shorthand for seq(1,10) for integer arguments.

> x=1:10

> x

[1] 1 2 3 4 5 6 7 8 9 10

There are many other options: for instance, seq(0,1,length=10) makes a sequence of 10 numbers that are equally spaced between 0 and

1. Type 50 numbers which are equally spaced between negative π and positive π (symbol pi in R).

Question 1:

What is the number which is third in this sequence:

a. -2.757 b. -2.885 c. -3.013 d. None of above

2. We will now create some more sophisticated plots. The contour() function produces a contour plot in order to represent three- dimensional data; it is like a topographical map. It takes three arguments:

1. A vector of the x values (the first dimension),

2. A vector of the y values (the second dimension), and

3. A matrix whose elements correspond to the z value (the third dimension) for each pair of (x,y) coordinates. This function can be set up by using the command outer.

To learn more about the parameters of this command take a look at the help file by typing ?contour and ?outer. Plot contours for function f=cos(y)/(1+x^2). Save this file as in a jpeg format and increase to have all contours appear with the relevant values.

Question 2:

What is the value for the lower contour on the figure provided by R for y< -π/2?

a. -0.8 b. 0.2 c. -0.5 d. None of above

3. Plot the contours for f – transposed f (t(f) return the transposed matrix of f) divided by 2. Check what is a syntax of the parameter “levels”. Set levels = 15.

Question 3: What is the value for the upper contour on the figure provided by R for right quarter of the figure?

a. -0.5 b. 0.2 c. 0.5 d. None of above

4. The image() function works the same way as contour(), except that it produces a color-coded plot whose colors depend on the z value. This is known as a heatmap, and is sometimes used to plot temperature in weather forecasts.

Implement command image for the function defined in exercise 3. Compare it with the contour map from Exercise 3.

Question 4: What color corresponds to the lowest values of function fa?

a. red b. white c. yellow d. None of above

5. Alternatively, persp() can be used to produce a three-dimensional plot. The arguments theta and phi control the angles at which the plot is viewed. Implement command persp for the function defined in exercise 3 for angles theta and phi 30 and 40 degrees respectively. Compare it with the contour map from Exercise 3.

Question 5: What type of extremes of function fa can be observed in cross-section y for smallest value of y?

a. minimum b. inflection point c. maximum d. None of above

6. Indexing data. We often wish to examine part of a set of data. Suppose that our data is stored in the matrix A.

> A=matrix (1:16 ,4 ,4)

> A

[,1] [,2] [,3] [,4]

[1,] 1 5 9 13

[2,] 2 6 10 14

[3,] 3 7 11 15

[4,] 4 8 12 16

We can also select multiple rows and columns at a time, by providing vectors as the indices.

> A[1:3 ,2:4]

[,1] [,2] [,3]

[1,] 5 9 13

[2,] 6 10 14

[3,] 7 11 15

> A[1:2 ,]

[,1] [,2] [,3] [,4]

[1,] 1 5 9 13

[2,] 2 6 10 14

> A[ ,1:2]

[,1] [,2]

[1,] 1 5

[2,] 2 6

[3,] 3 7

[4,] 4 8

R treats a single row or column of a matrix as a vector:

> A[1,]

[1] 1 5 9 13

Create matrix 4x4 were elements are 16 values of standard normal distribution (use set.seed(5)). Print A vector , which is the third columns of this matrix.

Question 6:

What is the third element of this vector?

a. -0.285 b. 0.138 c. -0.802 d. None of above

7. The use of a negative sign - in the index tells R to keep all rows or columns except those indicated in the index.

> A[-c(1,3) ,]

[,1] [,2] [,3] [,4]

[1,] 2 6 10 14

[2,] 4 8 12 16

For the matrix created in exercise 3 print the 2x2 submatrix which is from the second and the third column and from the second and the third row of the initial 4x4 matrix.

Question 7:

What is the bottom left element of this 2x2 matrix?

a. 0.138 b. -0.603 c. 1.228 d. None of above

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

# 1.x <- seq(-pi, pi, length=50)

x[3]

# [1] -2.885136

# Question 1: b

# 2.

y <- seq(-pi, pi, length=50)

z <- function(x, y) { cos(y)/(1+x^2)}

jpeg('q1.jpg', width=350, height=350)

contour(x, y, outer(x, y, FUN=z))

dev.off()

# Question 2: a

# According to the plot, it's -0....

By purchasing this solution you'll be able to access the following files:

Solution.docx and Solution.R.