简体   繁体   English

如何 plot 一个 function 的 2 个变量

[英]how to plot a function of 2 variables

I want to plot a function in R studio which has 2 variables: x1 and x2.我想在 R 工作室中 plot 和 function 有 2 个变量:x1 和 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)它显示此错误: FUN(X,Y,...)中的错误:未使用的参数(Y)

How can I fix it?我该如何解决?

The following changes solve the problem.以下更改解决了该问题。

  1. Change the function to a function of two variables, since that's what outer will pass it.将 function 更改为两个变量的 function ,因为这是outer将传递的内容。
  2. Correct the typo, instead of x_ejer1 you have created x1_ejer1 .更正错字,而不是x_ejer1您创建了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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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