Re: [R] plot a function with a vector as argument

From: jim holtman <jholtman_at_gmail.com>
Date: Sun, 18 May 2008 09:10:11 -0400

In the call to the function 'fr1', the argument 'x' comes in as a vector and you get a vector result. Here is what it looks like:

> fr2(seq(0,1,.1))
 [1] 1.00 1.31 1.64 1.99 2.36 2.75 3.16 3.59 4.04 4.51 5.00

because fr2 is sending a vector of length 10 to fr1 which will return a vector of length 10. When plotting a function, it will generate (by default) values 0 to 1, like above, and call for the plot of 10 'y' and 10 'x' values.

Now lets take a look at your second set of functions.

 Fr2 <- function(x){
 Fr1(c(x,3))
 }

 This will call Fr1 with a vector that is the concatenation of 'x' and 3. In the plot, you will get a sequence of:

 c(0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1,3)

 This is probably what you are not expecting since

 x[1] = 0
 x[2] = .1

 and you get just a single value returned:

 > Fr2(seq(0,1,.1))
 [1] 1

Now what you probably want is a 'list' for the parameters:

Fr1 <- function(x){
 x1 <- x[[1]]
 x2 <- x[[2]]

 x1^2+x1*x2+1
 }

 Fr2 <- function(x){
 Fr1(list(x,3))
 }

 plot(Fr2)

 > Fr2(seq(0,1,0.1))
 [1] 1.00 1.31 1.64 1.99 2.36 2.75 3.16 3.59 4.04 4.51 5.00

On Sat, May 17, 2008 at 11:54 PM, chaij <azraelchai_at_hotmail.com> wrote:

>
> I tried to define a function using another function I defined before, and
> the
> previous function has a vector as an argument, when I tried to get the
> graph
> of the new function, there was something going wrong. Here is a simple
> example to explain how it happend:
>
> fr1 <- function(x,y){
> x^2+x*y+1
> }
>
> fr2 <- function(x){
> fr1(x,3)
> }
>
> plot(fr2)
>
> In this case, it worked just fine. But when I changed it to the following
> form:
> Fr1 <- function(x){
> x1 <- x[1]
> x2 <- x[2]
> x1^2+x1*x2+1
> }
>
> Fr2 <- function(x){
> Fr1(c(x,3))
> }
>
> plot(Fr2)
> it gives:
> Error in xy.coords(x, y, xlabel, ylabel, log) :
> 'x' and 'y' lengths differ
>
> Could anyone help me with this problem? Thanks.
> --
> View this message in context:
> http://www.nabble.com/plot-a-function-with-a-vector-as-argument-tp17299015p17299015.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help_at_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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

	[[alternative HTML version deleted]]

______________________________________________
R-help_at_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.
Received on Sun 18 May 2008 - 13:14:20 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 18 May 2008 - 13:30:38 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive