see FAQ 7.31 and read "What Scientists should know about floating point numbers"
On 7/5/07, James Foadi <[EMAIL PROTECTED]> wrote: > > True. > I've corrected my function in this way: > > ####################################### > sinca <- function(N,th) > > { > > mod_th <- th%%(2*pi) > > if (mod_th == 0) > > { > > ff <- 2*N+1 > > } > > else > > { > > ff <- sin((N+0.5)*th)/sin(0.5*th) > > } > > return(ff) > > } > > ####################################### > > The function is equal to 2*N+1 at 0, 2Pi, 4Pi, etc. > > Still having the same problem. > > J > > ----- Original Message ----- > From: jim holtman > To: James Foadi > Cc: r-help@stat.math.ethz.ch > Sent: Thursday, July 05, 2007 8:44 PM > Subject: Re: [R] unexpected result in function valuation > > > Very simple; it is your function. You need to step through and see that > you are evaluating close to zero: > > > x[701] > [1] 6.283185 > > sin((4.5*x[701])) > [1] -1.666142e-14 > > sin(.5*x[701]) > [1] -1.653896e-15 > > sin((4.5*x[701]))/sin(.5*x[701]) > [1] 10.07404 > > > > With numbers that small you might be losing significance. See the FAQ on > floating point numbers. > > > On 7/5/07, James Foadi <[EMAIL PROTECTED]> wrote: > Dear all, > I have a very small script to plot a function. Here it is: > > ########################################## > sinca <- function(N,th) > > { > > return(sin((N+0.5)*th)/sin(0.5*th)) > > } > > plot_sinca <- function(N) > > { > > x <- seq(-5*pi,5*pi,by=pi/100) > > y <- rep(0,length=length(x)) > > for (i in 1:length(x))y[i] <- sinca(N,x[i]) > > plot(x,y,type="l",ylim=c(0,2*N+4)) > > return(c(x,y)) > > } > > ########################################## > > When I load the script and run the function like this: > > ########################################### > > data <- plot_sinca(4) > > y <- data[1002:2002] > ########################################### > > I notice a spike on the plot which should not be there. > In fact I have checked and: > ########################################### > > y[701] > [1] 10.07404 > > sinca(4,2*pi) > [1] 9 > ########################################### > > The second result is the correct one. Why, then do > I get the y[701]=10.07404? This function is not supposed > to be higher than 9... > > Any help is greatly appreciated. > > Regards, > > J > > Dr James Foadi > Membrane Protein Laboratory > Diamond Light Source Ltd > Chilton, Didcot > Oxfordshire OX11 0DE > --- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch 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. > > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem you are trying to solve? > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch 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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]] ______________________________________________ R-help@stat.math.ethz.ch 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.