Hi:

Here's one way:

f <- function(x) sin(x) + pi/4
g <- function(x) {
      sin(x) * (x >= 0 & x <= pi) + sin(x) * (x >= -2 * pi & x <= -pi) -
      (pi/4) * (x > -pi & x < 0) - pi/4 * (x > pi & x < 2 * pi)
     }
x <- seq(-2 * pi, 2 * pi, length = 200)
plot(x, f(x), type = 'l', col = 'blue', ylim = c(-1, 2))
lines(x, g(x), col = 'red')

HTH,
Dennis

On Mon, Feb 21, 2011 at 10:01 AM, Gloria_M <g-l-o-...@hotmail.com> wrote:

>
> To the more experienced R users,
>
> I am not a professional programmer so please excuse me if my questions seem
> naive.
> I have only begun using R to solve some problems and I'm already regretting
> it
>
> 1.
> I am trying to plot different functions in the same figure. The first
> function
> f(x)= sin(x)+(pi/4) , -2pi<=x<=2pi
> the second function
> g(x) = {sin(x), 0 <= x <= pi or -2pi <= x <= -pi
>         {-pi/4, elsewhere
>
> my code is:
> > x<-seq(from=-2*pi,to=2*pi,length=1000)
> > fx<- (sin(x)+(pi/4))
> > plot(x,fx,type="l",lty=1)
> > x2<- seq(from=0,to=pi)
> > x3<- seq(from=-2*pi,to=-pi)
> > gx<-(sin(x2 | x3) + (-pi/4))
> > lines(x,gx,lty=2)
> Error in xy.coords(x, y) : 'x' and 'y' lengths differ
>
>
> it plots the first function but when I try to plot the second function it
> displays the error x,y coordinates differ. I assume this is because I am
> using different values for x in the first and second functions?
> any ideas will be appreciated.
> --
> View this message in context:
> http://r.789695.n4.nabble.com/plot-numerous-functions-in-one-figure-with-different-values-of-x-per-function-tp3317784p3317784.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to