Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to draw two curves in one plot / graph

I have two functions, with a range-specific argument. I can’t get them on one chart in any way.

f1 = function(x1){
  return (sqrt(64-x1^2))
  }

f2 = function(x2){
  return (cos(x2) - x2)
}

plot(f1,-5, 1) 
plot(f2,-pi/2, pi/2)

I just started to learn this language, and I do not quite understand how this process can be performed.

If I execute the code, I get the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here
enter image description here

I need these curves to be on the same graph

>Solution :

You can try the lines() argument to add to an existing plot:

f1 = function(x1){
  return (sqrt(64-x1^2))
}

f2 = function(x2){
  return (cos(x2) - x2)
}


x <- c(-5:5) # choose your x value range here
y1 <- mapply(FUN = f1,x1 = x) # lets get the y values before plotting
y2 <- mapply(FUN = f2,x2 = x) # lets get the y values before plotting


plot(x,y1, type = "l", col = "red", ylim = c(-5,10))
lines(x, y2, col = "blue", type = "l")

Giving you this:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading