Re: NMinimize Error In Evaluation
- To: mathgroup at smc.vnet.net
- Subject: [mg85079] Re: NMinimize Error In Evaluation
- From: Mark Fisher <particlefilter at gmail.com>
- Date: Sun, 27 Jan 2008 05:47:11 -0500 (EST)
- References: <200801150813.DAA11621@smc.vnet.net> <fnet62$iea$1@smc.vnet.net>
On Jan 26, 4:06 am, "Jason S. Kong" <kon... at gmail.com> wrote: > Hi, I am asking again since I have not received a response on this > issue and would really desire some counsel in regards to this issue. > > Thanks, > > -Jason S. Kong > > On Jan 15, 2008 3:13 AM, Jason S. Kong <kon... at gmail.com> wrote: > > > > > > > Hi, > > > I have been having an issue with NMinimize in an attempt to try to do > > some optimization routines. > > > First, here is the background: > > > I want to simulate some data that was experimentally determined, and > > there are a large variety of variables involved. I do not believe > > there is a simple, analytical solution to obtain my results, so I > > decided to optimize it numerically, that is, NMinimize. Below I have > > some data and the method by which I attempted to solve this. At the > > end, you will see the error I get, preventing me from being able to > > utilize NMinimize. Any help on this issue would be greatly > > appreciated, as this is not the first time I have had to abandon a > > problem due to the same error! > > > At the end, I wanted to see if NMinimize had problems with functions > > inside of NMinimize by writing a quick "roar" program, but it > > functioned fine. > > > Thanks, > > > -Jason S. Kong > > > --- > > > xvals = {6.`, 12.`, 18.`, 24.`, 36.`, 48.`, 60.`, 75.`, 90.`, 120.`, > > 150.`, 180.`, 210.`, 240.`, 270.`, 300.`, 390.`, 480.`, 570.`, > > 660.`, 750.`, 840.`, 912.`, 930.`, 1020.`, 1110.`, 1200.`, 1350.`, > > 1524.`, 1800.`, 2442.`}; > > > yvals = {0.03049534752960375`, 0.030612029204239437`, > > 0.03088344092567462`, 0.03106269103453524`, 0.03132902963968192`, > > 0.031313810290816396`, 0.03167484706668189`, 0.031816894322760116`, > > 0.03173995205905108`, 0.031799138415750335`, 0.03183126815224423`, > > 0.03200882722234201`, 0.03196570573388969`, 0.032041802478217314`, > > 0.03191751112914886`, 0.03214072824584322`, 0.03222950778089211`, > > 0.03230898660274541`, 0.03230306796707548`, 0.03227516582748868`, > > 0.032589699037376185`, 0.03284927348747152`, 0.03289831361159377`, > > 0.03300231249550818`, 0.03331853674415852`, 0.033540908341471456`, > > 0.03361869612456192`, 0.033647443783530136`, 0.033538371783327206`, > > 0.033603476775696396`, 0.03364321618662304`}; > > > ListPlot[{xvals, yvals} // Transpose] > > > equilibrium[C1_, C2_, C3_, C4_, C5_] := Solve[ > > {C1 == var2/(var1 var5), > > C2 == var3/var2, > > C3 == var4/var2, > > var1 + var2 + var3 + var4 == C4, > > var5 + var2 + var3 + 2 var4 == C5}, {var1, var2, var3, var4, > > var5}, WorkingPrecision -> 100] // N // Chop > > > imagine[tests_] := > > Select[tests, > > And @@ {(Re[var1 /. #] == var1 /. #), (Re[var2 /. #] == > > var2 /. #), (Re[var3 /. #] == var3 /. #), (Re[var4 /. #] == > > var4 /. #), (Re[var5 /. #] == var5 /. #)} &] > > > dynamics[C1_, C2_, C3_, C4_, C5_] := > > Select[imagine[ > > equilibrium[C1, C2, C3, C4, > > C5]], #[[1, 2]] > 0 && #[[2, 2]] > 0 && #[[3, 2]] > 0 &] > > > data1[C1_, C2_, C3_, C4_, > > C5_] := {C5, var1} /. dynamics[C1, C2, C3, C4, C5] // Flatten > > data2[C1_, C2_, C3_, C4_, > > C5_] := {C5, var2 + var3} /. dynamics[C1, C2, C3, C4, C5] // > > Flatten > > data3[C1_, C2_, C3_, C4_, > > C5_] := {C5, var5} /. dynamics[C1, C2, C3, C4, C5] // Flatten > > > go[C1_, C2_, C3_, C4_, R1_, R2_, R3_] := ( > > listdata1 = data1[C1, C2, C3, C4, #] & /@ xvals; > > listdata2 = data2[C1, C2, C3, C4, #] & /@ xvals; > > listdata3 = data3[C1, C2, C3, C4, #] & /@ xvals; > > r1 = (R1 #)/C4 & /@ (listdata1 // Transpose)[[2]]; > > r2 = (R2 #)/C4 & /@ (listdata2 // Transpose)[[2]]; > > r3 = (R3 #)/C4 & /@ (listdata3 // Transpose)[[2]]; > > calcyvals = r1 + r2 + r3; > > error = Total[(calcyvals - yvals)^2]) > > > NMinimize[{go[c1, c2, c3, 25, ra1, ra2, ra3], > > c1 > 0 && c2 > 0 && c3 > 0 && ra1 > 0 && ra2 > ra1 && > > ra3 > ra2}, {c1, c2, c3, ra1, ra2, ra3}] > > > NMinimize::nnum: The function value (-0.0336474+0.04 var1+0.04 \ > > (var2+var3)+0.04 var5)^2+(-0.0336432+0.04 var1+0.04 (var2+var3)+0.04 \ > > var5)^2+(-<<20>>+<<2>>+0.04 \ > > var5)^2+(<<1>>)^2+<<1>>^2+<<1>><<1>><<1>>+(<<1>>)^2+(<<1>>)^2+(-0.\ > > 0328493+0.04 var1+<<1>>+0.04 var5)^2+<<21>> is not a number at \ > > {c1,c2,c3,ra1,ra2,ra3} = {2.,2.,2.,1.,1.,1.}. >> > > > NMinimize[{(-0.0336474 + (ra1 var1)/25 + 1/25 ra2 (var2 + var3) + ( > > ra3 var5)/25)^2 + (-0.0336432 + (ra1 var1)/25 + > > 1/25 ra2 (var2 + var3) + (ra3 var5)/25)^2 + (-0.0336187 + ( > > ra1 var1)/25 + 1/25 ra2 (var2 + var3) + (ra3 var5)/ > > 25)^2 + (-0.0336035 + (ra1 var1)/25 + 1/25 ra2 (var2 + var3) + ( > > ra3 var5)/25)^2 + (-0.0335409 + (ra1 var1)/25 + > > 1/25 ra2 (var2 + var3) + (ra3 var5)/25)^2 + (-0.0335384 + ( > > ra1 var1)/25 + 1/25 ra2 (var2 + var3) + (ra3 var5)/ > > 25)^2 + (-0.0333185 + (ra1 var1)/25 + 1/25 ra2 (var2 + var3) + ( > > ra3 var5)/25)^2 + (-0.0330023 + (ra1 var1)/25 + > > 1/25 ra2 (var2 + var3) + (ra3 var5)/25)^2 + (-0.0328983 + ( > > ra1 var1)/25 + 1/25 ra2 (var2 + var3) + (ra3 var5)/ > > 25)^2 + (-0.0328493 + (ra1 var1)/25 + 1/25 ra2 (var2 + var3) + ( > > ra3 var5)/25)^2 + (-0.0325897 + (ra1 var1)/25 + > > 1/25 ra2 (var2 + var3) + (ra3 var5)/25)^2 + (-0.032309 + ( > > ra1 var1)/25 + 1/25 ra2 (var2 + var3) + (ra3 var5)/ > > 25)^2 + (-0.0323031 + (ra1 var1)/25 + 1/25 ra2 (var2 + var3) + ( > > ra3 var5)/25)^2 + (-0.0322752 + (ra1 var1)/25 + > > 1/25 ra2 (var2 + var3) + (ra3 var5)/25)^2 + (-0.0322295 + ( > > ra1 var1)/25 + 1/25 ra2 (var2 + var3) + (ra3 var5)/ > > 25)^2 + (-0.0321407 + (ra1 var1)/25 + 1/25 ra2 (var2 + var3) + ( > > ra3 var5)/25)^2 + (-0.0320418 + (ra1 var1)/25 + > > 1/25 ra2 (var2 + var3) + (ra3 var5)/25)^2 + (-0.0320088 + ( > > ra1 var1)/25 + 1/25 ra2 (var2 + var3) + (ra3 var5)/ > > 25)^2 + (-0.0319657 + (ra1 var1)/25 + 1/25 ra2 (var2 + var3) + ( > > ra3 var5)/25)^2 + (-0.0319175 + (ra1 var1)/25 + > > 1/25 ra2 (var2 + var3) + (ra3 var5)/25)^2 + (-0.0318313 + ( > > ra1 var1)/25 + 1/25 ra2 (var2 + var3) + (ra3 var5)/ > > 25)^2 + (-0.0318169 + (ra1 var1)/25 + 1/25 ra2 (var2 + var3) + ( > > ra3 var5)/25)^2 + (-0.0317991 + (ra1 var1)/25 + > > 1/25 ra2 (var2 + var3) + (ra3 var5)/25)^2 + (-0.03174 + ( > > ra1 var1)/25 + 1/25 ra2 (var2 + var3) + (ra3 var5)/ > > 25)^2 + (-0.0316748 + (ra1 var1)/25 + 1/25 ra2 (var2 + var3) + ( > > ra3 var5)/25)^2 + (-0.031329 + (ra1 var1)/25 + > > 1/25 ra2 (var2 + var3) + (ra3 var5)/25)^2 + (-0.0313138 + ( > > ra1 var1)/25 + 1/25 ra2 (var2 + var3) + (ra3 var5)/ > > 25)^2 + (-0.0310627 + (ra1 var1)/25 + 1/25 ra2 (var2 + var3) + ( > > ra3 var5)/25)^2 + (-0.0308834 + (ra1 var1)/25 + > > 1/25 ra2 (var2 + var3) + (ra3 var5)/25)^2 + (-0.030612 + ( > > ra1 var1)/25 + 1/25 ra2 (var2 + var3) + (ra3 var5)/ > > 25)^2 + (-0.0304953 + (ra1 var1)/25 + 1/25 ra2 (var2 + var3) + ( > > ra3 var5)/25)^2, > > c1 > 0 && c2 > 0 && c3 > 0 && ra1 > 0 && ra2 > ra1 && > > ra3 > ra2}, {c1, c2, c3, ra1, ra2, ra3}] > > > go[2., 2., 2., 25, 1., 1., 1.] > > > 33275.9 > > > roar[x_, y_] := x^2 + y^2 > > NMinimize[roar[x, y], {x, y}] > > > {0., {x -> 0., y -> 0.}} > > > -- > > Jason S. Kong > > Graduate Student, Chen Lab > > Department of Chemistry and Chemical Biology > > Baker Laboratory, Cornell University > > Ithaca, NY, 14853 > > -- > Jason S. Kong > Graduate Student, Chen Lab > Department of Chemistry and Chemical Biology > Baker Laboratory, Cornell University > Ithaca, NY, 14853 Hi Jason, NMinimize evaluates your function "go" symbolically before it evaluates it numerically. (Evaluating go[c1, c2, c3, 25, ra1, ra2, ra3] returns something that is not useful for further evaluation.) To prevent this from happening, you can "trap" for symbolic arguments as follows: go[C1_?NumericQ, C2_, C3_, C4_, R1_, R2_, R3_] := ... (Make sure you Clear[go] first.) This pattern only matches if the first argument is numeric (i.e., a number or a symbol that represents a number, such as Pi). After I did this, I didn't get the error message. On the other hand, I didn't get a result after waiting 90 minutes. --Mark
- References:
- NMinimize Error In Evaluation
- From: "Jason S. Kong" <kongjs@gmail.com>
- NMinimize Error In Evaluation