Ivana
Ivana

Reputation: 705

How to plot function with parameter in R

I would like to plot a function several times, each time changing a parameter (a constant) in the function. How can i do this?

fun1 <- function(x, b) abs(x^2 - b^2)

plot(fun1(b=0.1),-1, 1)

plot(fun1(b=0.2),-1, 1, add=TRUE)

Upvotes: 0

Views: 269

Answers (2)

agstudy
agstudy

Reputation: 121568

Same approach just generalized in a for-loop:

  for (b in c(0.1,0.2))curve(abs(x^2 - b^2),add=TRUE,col='red')

Upvotes: 2

grrgrrbla
grrgrrbla

Reputation: 2589

try this:

curve(abs(x^2 - 0.1^2), -1, 1)
curve(abs(x^2 - 0.2^2), -1, 1, add = TRUE)

Upvotes: 1

Related Questions