Home > Mobile >  Contour plots of a function of several variables
Contour plots of a function of several variables

Time:01-07

I have a function of several variables H(x,y,z,w)=2xy 3zw-x^2 W^3. I need to draw the contour plot of the function H with respect to x and y when z=3 and w=5. Any help on drawing this contour plot using R would be greatly appreciated. Thank you!

CodePudding user response:

You can create a sequence of x and y and compute the function:

library(ggplot2)

#Create the function
H <- function(x, y, z = 3, w = 5) 2*x*y   3*z*w - x^2   w^3

#Sequence of x and y
s <- seq(-100, 100, length.out = 100)
#Grid
g <- expand.grid(x = s, y = s)

#Compute for each value of x and y the function H
g$value <- with(g, H(x, y))

#Plot
ggplot(g)  
  aes(x = x, y = y, z = value)  
  geom_contour()

#With some customization 
library(geomtextpath)
ggplot(g)  
  aes(x = x, y = y, z = value)  
  geom_contour_filled(show.legend = FALSE)  
  geom_textcontour(size = 2)  
  theme_bw()

enter image description here

  • Related