[英]How to plot function of multiple variables in R by initializing all variables but one
What's the easiest way to plot a function of multiple variables in a 2D graph by giving some of the input parameters a value. 通过给某些输入参数一个值,在2D图形中绘制多个变量的函数的最简单方法是什么。 Let's consider a simple example
让我们考虑一个简单的例子
my.function<-function(a,b,x){a*x^2+b}
Now I want to plot the simple parabola where a=1
an b=0
. 现在,我要绘制简单抛物线,其中
a=1
和b=0
。 So I define a new function: 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<-function(x){my.function(1,0,x)}; plot(new.function)
. new.function<-function(x){my.function(1,0,x)}; plot(new.function)
。
Is there any way where I can plot the function without defining new.function
? 有什么方法可以在不定义
new.function
情况下绘制函数?
Normally I use Mathematica and in Mathematica it would be: 通常我使用Mathematica,而在Mathematica中,它将是:
Plot[my.function[1,0,x],{x ... }]
You should not need to define a new function. 您无需定义新功能。 You can use the original function,
my.function
, and pass in x-values 1 through 10 to plot the parabola: 您可以使用原始函数
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)
For future readers, I am sharing @G5W 's comment as answer in here: 对于将来的读者,我在这里分享@ G5W的评论作为答案:
plot(function(x) { my.function(1,0,x) })
I believe this is the best way of those provided in here 我相信这是这里提供的最好方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.