Bartłomiej Drążczyk
2013-May-05 14:16 UTC
[R] custom function that plots other functions- problem
Hi everyone! Could you please help me with the following assignment? My aim is to write a custom function that draws the plots of functions submitted by the user. The catch is, that we do not know the exact number of functions the user will select to draw. The program is relatively easy with one function, f.e sin: mf<- function(f,range,quality){ x<- seq(range[1],range[2], quality); y<- f(x); plot(x,y); } mp(sin, c(0,2*pi), .02); but I encountered some problems while trying to provide more than one- the following structure generates mistakes: plotter<- function(f,howmany,range,quality){ x<-seq(range[1],range[2],quality) y<- matrix(f,how_many) plot(x,y[1,]) for(i in 2:how_many){ lines(x,y[i,]) } } plotter<-(c(sin(x),cos(x)),2,c(0,2*pi),.02) I would really appreciate your help with this program. Regards, Bartek [[alternative HTML version deleted]]
Rui Barradas
2013-May-05 17:53 UTC
[R] custom function that plots other functions- problem
Hello, Try the following. Note that argument 'how_many' is not needed, it can be derived from the length of the functions vector. plotter<- function(f, range, quality){ x <- seq(range[1], range[2], quality) y <- sapply(seq_along(f), function(.i) f[[.i]](x)) plot(x, y[, 1]) for(i in seq_along(f)[-1]){ lines(x, y[, i], col = i) # argument 'col' added } } plotter(c(sin, cos), c(0,2*pi), .02) Hope this helps, Rui Barradas Em 05-05-2013 15:16, Bart?omiej Dr??czyk escreveu:> Hi everyone! > > Could you please help me with the following assignment? My aim is to write > a custom function that draws the plots of functions submitted by the user. > The catch is, that we do not know the exact number of functions the user > will select to draw. > > The program is relatively easy with one function, f.e sin: > > mf<- function(f,range,quality){ > > x<- seq(range[1],range[2], quality); > > y<- f(x); > > plot(x,y); > } > > mp(sin, c(0,2*pi), .02); > > but I encountered some problems while trying to provide more than one- the > following structure generates mistakes: > > plotter<- function(f,howmany,range,quality){ > x<-seq(range[1],range[2],quality) > y<- matrix(f,how_many) > plot(x,y[1,]) > for(i in 2:how_many){ > lines(x,y[i,]) > } > } > > plotter<-(c(sin(x),cos(x)),2,c(0,2*pi),.02) > > I would really appreciate your help with this program. > > Regards, > Bartek > > [[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. >
Jeff Newmiller
2013-May-05 17:55 UTC
[R] custom function that plots other functions- problem
This request is clearly off-topic according to the Posting Guide. Please use the study assistance provided in the context of your assignment. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. "Bart?omiej Dr??czyk" <bartek.drazczyk at gmail.com> wrote:>Hi everyone! > >Could you please help me with the following assignment? My aim is to >write >a custom function that draws the plots of functions submitted by the >user. >The catch is, that we do not know the exact number of functions the >user >will select to draw. > >The program is relatively easy with one function, f.e sin: > >mf<- function(f,range,quality){ > > x<- seq(range[1],range[2], quality); > > y<- f(x); > > plot(x,y); >} > >mp(sin, c(0,2*pi), .02); > >but I encountered some problems while trying to provide more than one- >the >following structure generates mistakes: > >plotter<- function(f,howmany,range,quality){ > x<-seq(range[1],range[2],quality) > y<- matrix(f,how_many) > plot(x,y[1,]) > for(i in 2:how_many){ > lines(x,y[i,]) > } >} > >plotter<-(c(sin(x),cos(x)),2,c(0,2*pi),.02) > >I would really appreciate your help with this program. > >Regards, >Bartek > > [[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.