I want to plot a function in R studio which has 2 variables: x1 and x2. This is what I have:
f_ejer1 <- function(x) {sqrt(144+x[1]^2) + sqrt(25+(x[2]-x[1])^2) + sqrt(4+(7-x[2])^2)}
x1_ejer1 <- seq(-1, 1, len=50)
x2_ejer1 <- seq(-1, 1, len=50)
z_ejer1 <- outer(x_ejer1,x2_ejer1, f_ejer1)
persp(x_ejer1, x2_ejer1,z_ejer1, theta=-30, phi=15, ticktype="detailed", col="lightblue", shade=0.2)
image(x_ejer1, x2_ejer1,z_ejer1)
contour(x_ejer1, x2_ejer1, z_ejer1, nlevels=15, col="black", add=T)
It shows this error: Error in FUN(X, Y, ...): unused argument (Y)
How can I fix it?
The following changes solve the problem.
outer
will pass it.x_ejer1
you have created x1_ejer1
.And the code is almost the posted in the question.
f_ejer1 <- function(x, y) {
sqrt(144 + x^2) + sqrt(25+(y - x)^2) + sqrt(4 + (7 - y)^2)
}
x1_ejer1 <- seq(-1, 1, len=50)
x2_ejer1 <- seq(-1, 1, len=50)
z_ejer1 <- outer(x1_ejer1, x2_ejer1, f_ejer1)
persp(x1_ejer1, x2_ejer1,z_ejer1, theta=-30, phi=15, ticktype="detailed", col="lightblue", shade=0.2)
image(x1_ejer1, x2_ejer1,z_ejer1)
contour(x1_ejer1, x2_ejer1, z_ejer1, nlevels=15, col="black", add=T)
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.