Evaluating and Plotting Functions Over a Grid

I am working with the R programming language. I have the following function :


my_function_b <- function(input_1, input_2, input_3, input_4) {

final_value = sin(input_1) + cos(input_2) + input_3 + input_4

Question :

  • For "my_function_b", I am trying to evaluate "final_value" for different values of "input_1", "input_2", "input_3" and "input_4" . E.g. input_1, input_2, input_3, input_4 from 1 to 100 at increments of 0.1.
  • Then, I want to make a 3 Dimensional plot with "input_1", "input_2" and "input_3".
  • Next, I want to a fit a 3 Dimensional surface over this plot
  • Finally, I want to "color" this 3 Dimensional surface according to the values of "final_input"

What I tried so far:

I figured out how to make a "grid frame" for the second function and then evaluate "final_value" using this "grid frame", e.g.

#create grid and evaluate function
input_1 <- seq(0,100,0.1)
input_2 <- seq(0,100,0.1)
input_3 <- seq(0,100,0.1)
input_4 <- seq(0,100,0.1)

my_grid <- data.frame(input_1, input_2, input_3, input_4)
my_grid$final_value = sin(input_1) + cos(input_2) + input_3 + input_4

But I am not sure if this is the best way to solve this question. This is now creating problems when I try to plot the results, e.g.

#make a 3d plot for two of the inputs and the output, and fit surface over the plot

persp(my_grid$input_1, my_grid$input_2, my_grid$final_value)

Error in persp.default(my_grid$input_1, my_grid$input_2, my_grid$final_value) : 
  invalid 'z' argument

Alternative #2: Does Not Work


a = my_grid[,c(1,2,5)]
fig <- plot_ly(a = ~as.matrix(a))
fig <- fig %>% add_surface()

Error: Must supply `z` attribute

Alternative #3 : Does Not Work - Creates an Empty Plot

plot_ly() %>% 
    add_trace(data = my_grid,  x=my_grid$input_1, y=my_grid$input_2, z=my_grid$final_value, type="mesh3d" ) 

Problem: Can someone please show me how to do this? Can this be done using the "lattice" or "rsm" libraries? Or can it be done using the ways I suggested?


The first problem is that your grid of values is not really a grid. Each value of input_1 appears with only one value of every other variable and the same is true for every other variable. In a true grid, every value of input_1 would appear with every other possible value of the other variables. This is illustrated in the first part of the code below where I have only two variables, each with three values. The grid as you defined it has three rows but a complete grid has nine rows. The expand.grid() function is very handy for making complete grids. In the case of your data, where each variable has 1000 values, the complete grid will be far too large to be practical.
The second part of the code below shows a simple example of making a plot with persp(). The persp() function takes a matrix of z values, so I used the exapnd.grid function to make a data frame with the complete grid of x and y values, calculated z, and loaded the z values into a matrix.

#How to make a grid
X <- seq(1,3,1)
Y <- seq(1,3,1)
IncompleteGrid <- data.frame(X,Y)
#>   X Y
#> 1 1 1
#> 2 2 2
#> 3 3 3
CompleteGrid <- expand.grid(X,Y)
#>   Var1 Var2
#> 1    1    1
#> 2    2    1
#> 3    3    1
#> 4    1    2
#> 5    2    2
#> 6    3    2
#> 7    1    3
#> 8    2    3
#> 9    3    3

#Make a simple persp plot
X <- seq(0,3.1,0.1)
Y <- seq(0,3.1,0.1)
DF <- expand.grid(X,Y)
#> [1] 1024

#Compute Z
DF$Z <- sin(DF$Var1) + cos(DF$Var2)

#persp takes a matrix of Z values
Mat <- matrix(DF$Z,nrow = 32)


Created on 2021-07-24 by the reprex package (v0.3.0)

1 Like

Thank you so much for your answer! Is there a way to color the plot according to values of "final_value"? Thank you so much for your help!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.