0

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)
1
  • do you want to save the result? or just look at it? so you want to vary b while x stays fixated? Commented Jun 10, 2015 at 16:34

2 Answers 2

2

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')
Sign up to request clarification or add additional context in comments.

3 Comments

funny I just now wanted to add the same thing using lapply
@grrgrrbla do it:) but maybe you should use invisible(lapply(...))
good tip with the invisible, but I wont do it, its basically the same thing, I would have done: invisible(lapply(seq(0.1,0.2,0.1), function(b) curve(abs(x^2 - b^2), -1, 1, add = TRUE)))
1

try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.