0

I made this derivative script and I wanted to plot the graph of the result of the derivative along with the function. But I am not able to make the graph in the multivariate case f(x,y), the result of the derivative is taken as a value and not as a function. See the example below:

DD7<-function(x,r,contador=1){

dy<- substitute(x)
if(contador<1) {

stop("Grau de derivada menor que 1")
}
if(contador==1) {

der<-D(dy,c(r))

print(der)
x<- y <- seq(-3,3,length=50)
z<- outer(x,y,der)
persp(x,y,z)
}
else {
der2<- DD(D(dy,r),r,contador-1)
print(der2)
x2<- y2 <- seq(-3,3,length=50)
z2<- outer(x,y,der2)
persp(x,y,z2)

}
}

DD7(x*y^2,"y",1)

Error in get(as.character(FUN), mode = "function", envir = envir) : 
object 'der' of mode 'function' was not found
6
  • Typo? You define DD7 and call DD8. Not reproducible: we don't have your x or y values. Commented Jul 11, 2022 at 19:42
  • Sorry, I corrected the typo. x and y are variables of the function. The function (DD7) finds the derivative of the typed function. In this example the derivative of (x*(y^2)) with respect to y will answer: (2*xy). I would like to plot the graph of the derivative (2*xy). Commented Jul 11, 2022 at 20:11
  • (I see, sorry for the false-alarm, I understand that now.) Commented Jul 11, 2022 at 20:13
  • The third argument for base::outer needs to be a function, whereas der is a call. I don't know why you need outer there, if you call eval(der) in place of that, you get a vector the length of x and y (which, in this example, is the value from y^2, the derivative I think you're targeting). What makes you believe you need to outer the x/y vectors? That'll produce a vector length(x)*length(y) long, fyi. Commented Jul 11, 2022 at 20:16
  • eval (der) generates a vector, I don't want a vector, I need to turn (der) into a function for me to graph the function. Commented Jul 11, 2022 at 20:35

1 Answer 1

3

As I said in my comments, outer's third argument needs to be a function. If you use eval(der), you may infer (correctly) that it is evaluating the expression (y^2 in this example) based on the objects found in the calling environment (the function's environment, to be specific). Since x and y are both found, then eval(der) uses those variables, and returns a vector of the appropriate length.

The purpose of outer is to do an outer-join of the values, expanding into length(x)*length(y) combinations. If you look at the function as it is called by outer, its first argument is length(x)*length(y) elements long, all from the original x; the second argument is also that length, from the original y. The order of the values are such that all values from x are paired with all values of y.

Given this what we need to do is have eval(der) operate in that environment.

DD7 <- function(x, r, contador=1) {
  # ...
  z2 <- outer(x, y, function(x, y) eval(der))
  persp(x, y, z2)
  # ...
}

DD7(x*y^2, "y", 1)
# x * (2 * y)

persp plot

In that example, it might be confusing which x and y are referenced when; it's straight-forward: inside the anon-function passed to outer, we also name them the same names so that the evaluated der will find the relevant expanded values.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.