Home > database >  Directly Plotting Mathematical Functions in R
Directly Plotting Mathematical Functions in R

Time:03-02

I am working with the R programming language.

In a previous question that I asked (enter image description here

I am trying to use this same approach to plot the following function (enter image description here

I first defined the function:

  my_function <- function(x,y) {
    
    final_value = (1 - x)^2   100*((y - x^2)^2)
     
    }

Then, I defined the "grid":

input_1 <- seq(-1.5, 1.5,0.1)
input_2 <- seq(-1.5, 1.5,0.1)


my_grid <- data.frame(input_1, input_2)
my_grid$final_value =   (1 - input_1)^2   100*((input_2 - input_1^2)^2)

Then, I tried to plot this function:

 x <- my_grid$input_1
y <- my_grid$input_2

z <- matrix(my_grid$final_value, nrow = length(x), ncol = length(y)) # proper matrix & dimensions

plot_ly(x = x, y = y, z = z) %>% add_surface()

My Problem: The final result does not look similar to the result from the Wikipedia page:

enter image description here

Can someone please show me what I am doing wrong? Is there an easier way to do this?

Thanks!

CodePudding user response:

Your problem is that you are not actually creating a grid, you are creating a single vector of equal x, y points and running your formula on that, so your matrix is wrong (every column will be the same due to it being repeated). The easiest fix is to run outer on your function to evaluate it at every pair of input 1 and input 2:

z <- outer(input_1, input_2, my_function)

plot_ly(x = input_1, y = input_2, z = z) %>% add_surface()

enter image description here

  • Related