k.dkhk
k.dkhk

Reputation: 530

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

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 ... }]

Upvotes: 0

Views: 5613

Answers (2)

Dale Kube
Dale Kube

Reputation: 1460

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)

Upvotes: 1

k.dkhk
k.dkhk

Reputation: 530

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

Upvotes: 1

Related Questions