[R] defining a function by branches

Vincent Goulet vincent.goulet at act.ulaval.ca
Thu Oct 14 22:39:32 CEST 2004


Le 14 Octobre 2004 16:24, Pedro Rodrigues de Almeida a écrit :
> Hi there,
>
> How can I plot a function in R which is defined by conditional
> branches?, e.g.:
>
> u(x) = 1, Ed<x<2Ed (where Ed is a positive constant, say, 25);
> u(x)=0.5*x, x>2Ed
>
> curve(u(x), ....)
>
> So far I have had to use curve(..., add=TRUE) with two function define
> by segments, i.e., two branches or intervals for the variable x, say,
> 25,50 and then from 50,... and I would appreciate a more elegant
> solution if possible.
>
> Thanks a lot,
>
> Pedro Rodrigues de Almeida
> Helsinki - Finland

Have you tried something along the lines of:

u <- function(x, Ed)  ifelse(x > 2 * Ed, 0.5 * x, 1)
curve(u(x), ...)

(Note that this does take the case x <= Ed into account, which is not covered 
in your definition.)

HTH

-- 
  Vincent Goulet, Associate Professor
  École d'actuariat
  Université Laval, Québec 
  Vincent.Goulet at act.ulaval.ca   http://vgoulet.act.ulaval.ca




More information about the R-help mailing list