简体   繁体   中英

How to plot multivariate function in R

I am trying to plot the following function: 在此处输入图像描述

This is what I have currently tried:

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

But I get the following error:

在此处输入图像描述

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)))

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)

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()

等高线图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM