gogilan
gogilan

Reputation: 163

How to plot multivariate function in R

I am trying to plot the following function:enter image description here

This is what I have currently tried:

curve(7*x*y/( e^(x^2+y^2)))

But I get the following error:

enter image description here

Upvotes: 0

Views: 2503

Answers (3)

Vlad
Vlad

Reputation: 999

You can create a contour plot like this:

library(tidyverse)

tibble(x = seq(0, 10, 0.1),             # define the drawing grid
       y = seq(0, 10, 0.1)
  ) %>% 
cross_df() %>%                          # create all possible combinations of x and y
mutate(z = 7*x*y/(exp(x^2+y^2)) ) %>%   # add your function
ggplot(aes(x = x, y = y, z = z)) +      # create the plot
  geom_contour()

contour plot

Upvotes: 1

Cole
Cole

Reputation: 11255

One way to plot is using the contour() function. Also, as @Sang won kim noted, exp() is the function for e^(...)

x <- seq(from = 0.01, to = 2.1, by = 0.01)
y <- x

multi_var_fx <- function (x, y) {
  7 * x * y / (exp(x^2 + y^2))
}

z <- outer(x, y, multi_var_fx)

contour(x, y, z, xlab = 'x', ylab = 'y')

Created on 2019-10-27 by the reprex package (v0.3.0)

Upvotes: 1

Sang won kim
Sang won kim

Reputation: 522

Your e means exponential function. In the r, exponential function code is exp(). So you can revise this code.

curve(7*x*y/(exp(x^2+y^2)))

Upvotes: 1

Related Questions