简体   繁体   中英

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. 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 . 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) .

Is there any way where I can plot the function without defining new.function ?

Normally I use Mathematica and in Mathematica it would be:

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 <- 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:

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

I believe this is the best way of those provided in here

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.

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