Report in R Markdown
Graphing a bivariate function and its second order approximation
Use R to graph a 3-d plot of the function 𝑓(𝑥1, 𝑥2) = cos(𝑥1𝑥2) and its second order Taylor approximation
ℎ(𝑥1, 𝑥2) = 1 − 𝜋2
8 𝑥1
Following are the requirements:
Write an R function that produces the function 𝑓.
Write an R function that produces the function ℎ.
Use the functions in (i) and (ii) to plot a 3d graph that contains the surfaces for both 𝑓 and ℎ on the same
Label the first axis 𝑥1and the second axis 𝑥2. Your graph should have the title “Taylor approximation of
cos(𝑥1𝑥2).” Make sure to show the plot from an angle with a good view of the function.
In a separate 3d plot, graph the absolute value error function 𝑒(𝑥1, 𝑥2) = |𝑓(𝑥1, 𝑥2) − ℎ(𝑥1, 𝑥2)|. Use the
same range and grid for 𝑥1 and 𝑥2 as described in (iii).
Label the first axis 𝑥1and the second axis 𝑥2. Title your plot “The error in second order Taylor expansion of
cos(𝑥1𝑥2).”
Plot the constant value contours for 𝑒(𝑥1, 𝑥2), and explain the magnitude of the error at various locations
in the context of the problem.
Problem 2: Given a 𝑝 × 1vector 𝝁 and a 𝑝 × 𝑝 positive definite matrix Σ, the pdf for a 𝑝-variate normal density at a point
𝒙 = (𝑥1, 𝑥2, … , 𝑥𝑝)𝑇can be written as
𝑓(𝒙) = (2𝜋)−𝑝
2 |Σ|−1/2 exp [− 1
2 (𝒙 − 𝝁)𝑇 Σ−1(𝒙 − 𝝁)] .
Now consider the bivariate normal random variable, where
𝒙 = (𝑥1
𝑥2) ,𝝁 = (𝜇1
𝜇2) ,Σ = (𝜎11 𝜎12
𝜎21 𝜎22).
Write the second order Taylor expansion for 𝑓(𝒙), for the bivariate normal density, around the point
𝒙𝟎 = (𝜇1
𝜇2).
Graph a 3-d plot of the function 𝑓(𝑥1, 𝑥2) and its second order Taylor expansion for the following parameters
(for each set of parameters, f and its approximation should be on the same frame):
(i)𝝁 = (0
0) ,Σ = ( 1 −0.3
−0.3 1 ) ;(ii)𝝁 = (0
0) ,Σ = ( 1 0.8
0.8 1 )
Note that the means and variances for each of the variables in the cases (i) and (ii) are 0 and 1 respectively. This should
guide you an idea for a reasonable range for 𝑥1 and 𝑥2 to consider for your graphs.
Graph the constant value contours for 𝑓(𝒙) for the cases (i) and (ii) in part (b). What is the shape of the
constant value contours? What is the center of the constant value contours?
Compute the eigenvalues and eigenvectors for each of the covariance matrices 𝛴 given in part (b).
Superimpose the eigenvectors on each of their corresponding constant value contours that you drew in part (c) and
explain how the eigenvectors and eigenvalues are related to the constant value contou