繁体   English   中英

如何通过初始化除一个变量以外的所有变量来绘制R中多个变量的函数

[英]How to plot function of multiple variables in R by initializing all variables but one

通过给某些输入参数一个值,在2D图形中绘制多个变量的函数的最简单方法是什么。 让我们考虑一个简单的例子

my.function<-function(a,b,x){a*x^2+b}

现在,我要绘制简单抛物线,其中a=1b=0 因此,我定义了一个新函数: new.function<-function(x){my.function(1,0,x)}; plot(new.function) new.function<-function(x){my.function(1,0,x)}; plot(new.function)

有什么方法可以在不定义new.function情况下绘制函数?

通常我使用Mathematica,而在Mathematica中,它将是:

Plot[my.function[1,0,x],{x ... }]

您无需定义新功能。 您可以使用原始函数my.function ,并传入x值1到10来绘制抛物线:

my.function <- function(a,b,x){a*x^2+b}
x <- 1:10
y <- my.function(a=1,b=0,x=x)
plot(y~x)

对于将来的读者,我在这里分享@ G5W的评论作为答案:

plot(function(x) { my.function(1,0,x) })

我相信这是这里提供的最好方法

暂无
暂无

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

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