Hi, all I'm trying to solve an optimization problem with two variables, in wich Iuse the following functions(they all are working perfectly): fxx1=function(x){ calc=((2.5/3)*((x/3)^(2.5-1))*exp(-(x/3)^2.5)) return(calc)} fxx2=function(x){ calc1=((5.3/19.3)*((x/19.3)^(5.3-1))*exp(-(x/19.3)^5.3)) return(calc1)} fxMixed=function(x){ calc2=(0.12*fxx1(x)+((1-0.12)*fxx2(x))) return(calc2)} FxMixed=function(x){ calc3=integrate(fxMixed,0,x) return(calc3$value)} RxMixed=function(x){ calc4=(1-FxMixed(x)) return(calc4)} SUM=function(x){ T1=x[1] M1=x[2] n=M1-1 calc5=1:n for (i in 0:n){ calc5[i]=RxMixed(i*T1)} sum(calc5)} V=function(x){ T1=x[1] M1=x[2] y=T1*M1 calc6=T1*SUM(x)+0.001*RxMixed(y)+0.00463*FxMixed(y) return(calc6)} However, when I try to use the function U, there is an error message and I don't understand what's wrong with my code: U=function(x){ T1=x[1] M1=x[2] y=M1*T1 calc7=integrate(RxMixed,0,y) return(calc7$value)}> U(c(2,5))Error in integrate(RxMixed, 0, y) : evaluation of function gave a result of wrong length I think that this might happen becouse my fucntion RxMixed is already an previous integration of the function fxMixed. Any help will be welcome! [[alternative HTML version deleted]]
Your basic problem seems to be that the function RxMixed does not return a value of the same length as its input parameter. I just used the 'debug' library and here is what I got:> mtrace(RxMixed) > integrate(RxMixed,0,10)D(3)> [1] 0.8826402 D(3)> x [1] 5.00000000 0.13046736 9.86953264 0.67468317 9.32531683 1.60295216 8.39704784 2.83302303 7.16697697 [10] 4.25562831 5.74437169 0.02171418 9.97828582 0.34921254 9.65078746 1.09591137 8.90408863 2.18621433 [19] 7.81378567 3.52803569 6.47196431 D(3)> [1] 0.8826402 D(3)> NULL Error in integrate(RxMixed, 0, 10) : evaluation of function gave a result of wrong length Enter a frame number, or 0 to exit 1: integrate(RxMixed, 0, 10) Selection: 0 you can see that you are returning a value of length one (0.8826) when the input parameter 'x' was of length 21. So you need to look at the functions you are creating and what 'integrate' is expecting. On Wed, Feb 9, 2011 at 9:29 AM, Claudio Carneiro <claudio_carneiro123 at hotmail.com> wrote:> > Hi, all > > I'm trying to solve an optimization problem with two > variables, in wich Iuse the following functions(they all are working > perfectly): > > fxx1=function(x){ > calc=((2.5/3)*((x/3)^(2.5-1))*exp(-(x/3)^2.5)) > return(calc)} > > fxx2=function(x){ > calc1=((5.3/19.3)*((x/19.3)^(5.3-1))*exp(-(x/19.3)^5.3)) > return(calc1)} > > fxMixed=function(x){ > calc2=(0.12*fxx1(x)+((1-0.12)*fxx2(x))) > return(calc2)} > > > FxMixed=function(x){ > calc3=integrate(fxMixed,0,x) > return(calc3$value)} > > RxMixed=function(x){ > calc4=(1-FxMixed(x)) > return(calc4)} > > SUM=function(x){ > T1=x[1] > M1=x[2] > n=M1-1 > calc5=1:n > for (i in 0:n){ > calc5[i]=RxMixed(i*T1)} > sum(calc5)} > > V=function(x){ > T1=x[1] > M1=x[2] > y=T1*M1 > calc6=T1*SUM(x)+0.001*RxMixed(y)+0.00463*FxMixed(y) > return(calc6)} > > However, when I try to use the function U, there is an error message and I don't understand what's wrong with my code: > > U=function(x){ > T1=x[1] > M1=x[2] > y=M1*T1 > calc7=integrate(RxMixed,0,y) > return(calc7$value)} > >> U(c(2,5)) > Error in integrate(RxMixed, 0, y) : > ?evaluation of function gave a result of wrong length > > > > I think that this might happen becouse my fucntion RxMixed is already an previous integration of the function fxMixed. > > Any help will be welcome! > > > ? ? ? ?[[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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
Hi: On Wed, Feb 9, 2011 at 6:29 AM, Claudio Carneiro < claudio_carneiro123@hotmail.com> wrote:> > Hi, all > > I'm trying to solve an optimization problem with two > variables, in wich Iuse the following functions(they all are working > perfectly): > > fxx1=function(x){ > calc=((2.5/3)*((x/3)^(2.5-1))*exp(-(x/3)^2.5)) > return(calc)} > > fxx2=function(x){ > calc1=((5.3/19.3)*((x/19.3)^(5.3-1))*exp(-(x/19.3)^5.3)) > return(calc1)} > > fxMixed=function(x){ > calc2=(0.12*fxx1(x)+((1-0.12)*fxx2(x))) > return(calc2)} > > > FxMixed=function(x){ > calc3=integrate(fxMixed,0,x) > return(calc3$value)} > > RxMixed=function(x){ > calc4=(1-FxMixed(x)) > return(calc4)} > > SUM=function(x){ > T1=x[1] > M1=x[2] > n=M1-1 > calc5=1:n > for (i in 0:n){ > calc5[i]=RxMixed(i*T1)} > sum(calc5)} > > V=function(x){ > T1=x[1] > M1=x[2] > y=T1*M1 > calc6=T1*SUM(x)+0.001*RxMixed(y)+0.00463*FxMixed(y) > return(calc6)} > > However, when I try to use the function U, there is an error message and I > don't understand what's wrong with my code: > > U=function(x){ > T1=x[1] > M1=x[2] > y=M1*T1 > calc7=integrate(RxMixed,0,y) > return(calc7$value)}> > U(c(2,5)) > Error in integrate(RxMixed, 0, y) : > evaluation of function gave a result of wrong length > > > > I think that this might happen becouse my fucntion RxMixed is already an > previous integration of the function fxMixed. >I concur. RxMixed is a number, not a function - integrate() requires the latter. It appears that RxMixed is the survival function of a mixture distribution - you need to either get a symbolic representation of that function or generate a function numerically by computing RxMixed over a suitable range of x values and then using something like approxfun() or splinefun() to estimate the survival function. You could then integrate the resulting function in U. HTH, Dennis> > Any help will be welcome! > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]