Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw more than one function curves in the same plot [duplicate]

Tags:

plot

r

A way to draw the curve corresponding to a given function is this:

fun1 <- function(x) sin(cos(x)*exp(-x/2))
plot (fun1, -8, 5)

How can I add another function's curve (e.g. fun2, which is also defined by its mathematical formula) in the same plot?

like image 204
Brani Avatar asked Oct 29 '10 07:10

Brani


People also ask

How do you plot multiple curves on the same graph?

To draw multiple curves in one plot, different functions are created separately and the curve() function is called repeatedly for each curve function. The call for every other curve() function except for the first one should have added an attribute set to TRUE so that multiple curves can be added to the same plot.

How do you plot two curves on the same graph in R?

Learn, how to plot two graphs in a same plot in R Language. To plot the two graphs in the same plot, we can use the par() function in R language. Similarly, you can also use the lines() or points() function to add the graph to an existing plot.

How do you plot multiple functions on the same plot in Matlab?

To create a plot that spans multiple rows or columns, specify the span argument when you call nexttile . For example, create a 2-by-2 layout. Plot into the first two tiles. Then create a plot that spans one row and two columns.


2 Answers

plot (fun2, -8, 5, add=TRUE) 

Check also help page for curve.

like image 121
Marek Avatar answered Sep 19 '22 19:09

Marek


Using matplot:

fun1<-function(x) sin(cos(x)*exp(-x/2))
fun2<-function(x) sin(cos(x)*exp(-x/4))
x<-seq(0,2*pi,0.01)
matplot(x,cbind(fun1(x),fun2(x)),type="l",col=c("blue","red"))
like image 45
mbq Avatar answered Sep 22 '22 19:09

mbq