I am trying to solve the integration equation, for different values of K from 4 to 25, the integration is with respect to u, Here is the equation: gamma(k/2) / ( sqrt(k-1)*gamma((k-1)/2) ) * integrate(f= (1+u^2/k-1)^(-k/2), lower=0, upper= sqrt(a^2*k/(k+1-a^2)) ) = the similar expression as te left hand except k becomes k+1 my code is below, I don't know why R keep telling me the syntax wrong but I am not clear, thanks. f<-function(u,k){(1+u^2/(k-1))^(-k/2)} g<-function(u,k){(1+u^2/k)^(-(k+1)/2)} for(k in 4:25){ out<-uniroot(function(a){ m<-exp(lgamma(k/2)-lgamma((k-1)/2))*1/sqrt(k-1) n<-exp(lgamma((k+1)/2)-lgamma(k/2)*1/sqrt(k) m*integrate(f,lower=0,upper=sqrt(a^2*(k-1)/(k-a^2)),rel.tol=.Machine$double.eps^0.25) -n*integrate(g,lower=0,upper=sqrt(a^2*k/(k+1-a^2)),rel.tol=.Machine$double.eps^0.25) } lower=1.4, upper=2) print(unlist(out))} -- View this message in context: http://www.nabble.com/help-me-to-debug-this-part-of-code--tp16763482p16763482.html Sent from the R help mailing list archive at Nabble.com.
On Fri, 18 Apr 2008, jinjin wrote:> > I am trying to solve the integration equation, for different values of K from > 4 to 25, the integration is with respect to u, > > Here is the equation: gamma(k/2) / ( sqrt(k-1)*gamma((k-1)/2) ) * > integrate(f= (1+u^2/k-1)^(-k/2), lower=0, upper= sqrt(a^2*k/(k+1-a^2)) ) > the similar expression as te left hand except k becomes k+1 > > > my code is below, I don't know why R keep telling me the syntax wrong but I > am not clear, thanks.First, you should format your code so that it is more readable. Use spaces and indentation. Second, it is recommended that use an editor that features syntax highlighting to prepare your code. It will show you which parentheses and braces match. And if it features automatic indentation, this will give you clues when the nesting of an expression or arglist has gone awry. Third, read the error messages that R provides and try to understand why R is telling you "unexpected symbol", etc. Often, an error immediately preceeded the point at which R got confused. If all this fails, break apart your code into single expressions (or pieces you ***think*** are valid expressions) and try to evaluate them. For example, m<-exp(lgamma(k/2)-lgamma((k-1)/2))*1/sqrt(k-1) is correctly parsed. It only fails for me because I tried to evaluate it without providing a value for 'k'. HTH, Chuck> > > f<-function(u,k){(1+u^2/(k-1))^(-k/2)} > g<-function(u,k){(1+u^2/k)^(-(k+1)/2)} > > for(k in 4:25){ > out<-uniroot(function(a){ > m<-exp(lgamma(k/2)-lgamma((k-1)/2))*1/sqrt(k-1) > n<-exp(lgamma((k+1)/2)-lgamma(k/2)*1/sqrt(k) > m*integrate(f,lower=0,upper=sqrt(a^2*(k-1)/(k-a^2)),rel.tol=.Machine$double.eps^0.25) > -n*integrate(g,lower=0,upper=sqrt(a^2*k/(k+1-a^2)),rel.tol=.Machine$double.eps^0.25) > } > lower=1.4, upper=2) > print(unlist(out))} > > > -- > View this message in context: http://www.nabble.com/help-me-to-debug-this-part-of-code--tp16763482p16763482.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 > and provide commented, minimal, self-contained, reproducible code. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
There are a lot of syntax errors such as unclosed parentheses, missing commas etc; Example: exp@(@lgamma((k+1)/2)-lgamma(k/2)*1/sqrt(k), where the parenthesis between the @s is not closed. I suggest you get an editor such as "Tinn" that can highlight those for you. In addition I notice that you use two functions: f and g, in a loop but you didn't pass any arguments to them. jinjin wrote:> > I am trying to solve the integration equation, for different values of K > from 4 to 25, the integration is with respect to u, > Here is the equation: gamma(k/2) / ( sqrt(k-1)*gamma((k-1)/2) ) * > integrate(f= (1+u^2/k-1)^(-k/2), lower=0, upper= sqrt(a^2*k/(k+1-a^2)) ) = > the similar expression as te left hand except k becomes k+1 > my code is below, I don't know why R keep telling me the syntax wrong but > I am not clear, thanks. > f<-function(u,k){(1+u^2/(k-1))^(-k/2)} > g<-function(u,k){(1+u^2/k)^(-(k+1)/2)} > for(k in 4:25){ > out<-uniroot(function(a){ > m<-exp(lgamma(k/2)-lgamma((k-1)/2))*1/sqrt(k-1) > n<-exp(lgamma((k+1)/2)-lgamma(k/2)*1/sqrt(k) > m*integrate(f,lower=0,upper=sqrt(a^2*(k-1)/(k-a^2)),rel.tol=.Machine$double.eps^0.25) > -n*integrate(g,lower=0,upper=sqrt(a^2*k/(k+1-a^2)),rel.tol=.Machine$double.eps^0.25) > } > lower=1.4, upper=2) > print(unlist(out))} >-- View this message in context: http://www.nabble.com/help-me-to-debug-this-part-of-code--tp16763482p16765605.html Sent from the R help mailing list archive at Nabble.com.
jinjin wrote:> > I am trying to solve the integration equation, for different values of K > from 4 to 25, the integration is with respect to u, > Here is the equation: gamma(k/2) / ( sqrt(k-1)*gamma((k-1)/2) ) * > integrate(f= (1+u^2/k-1)^(-k/2), lower=0, upper= sqrt(a^2*k/(k+1-a^2)) ) = > the similar expression as te left hand except k becomes k+1 > my code is below... >I don't see that your expression at least as you wrote it above can be integrated between the k values of 4 to 25, however if your k value is negative, the expression (and the integral) has an analytical solution: -((((a^2*k)/(1 - a^2 + k))^((1 - k)/2)*gamma(k/2))/-((-1 + k)^1.5*(1/k)^(k/2)*gamma((-1 + k)/2))) That's all I can help with, sorry. maybe someone with more mathematical skills can help you out better. -- View this message in context: http://www.nabble.com/help-me-to-debug-this-part-of-code--tp16763482p16799564.html Sent from the R help mailing list archive at Nabble.com.