Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Contour plots of a function of several variables

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!

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading