Re: Re: NMinimize Error In Evaluation
- To: mathgroup at smc.vnet.net
- Subject: [mg85159] Re: [mg85059] Re: [mg84801] NMinimize Error In Evaluation
- From: "Jason S. Kong" <kongjs at gmail.com>
- Date: Wed, 30 Jan 2008 06:13:31 -0500 (EST)
- References: <200801150813.DAA11621@smc.vnet.net>
Thank you Mark and Andrzej! These suggestions are delightfully helpful. I noticed the following changes: 1. imagine function was greatly simplified. (Of course I'd do it the longhand way! *smacks head*) 2. dynamics has an N function now incorporated within it. I presume this is to force it to return a purely numeric answer? I didn't notice this before. 3. of course, changing the go function in itself. As you noted that there are many things I can still optimize, I'll keep looking into it to find these issues. Thanks again for a start at optimizing this code to perfect functionality. -Jason S. Kong On Jan 29, 2008 5:41 AM, Andrzej Kozlowski <akoz at mimuw.edu.pl> wrote: > There are lots of problems and I can only describe some of them. First > of all, change the definition of go to something like: > > go[C1_?NumberQ, C2_?NumberQ, C3_?NumberQ, C4_?NumberQ, R1_?NumberQ, > R2_?NumberQ, > R3_?NumberQ] := (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]) > > I would also add a defintion of g0 on the boundary (where one of the > C's is 0) otherwise you will get an error message (althogh the code > will work). > > With these changes NMinimize should now work, but terribly slowly. To > make the code more efficient one would need to totally re-write it but > for this I cannot spare the itme, so I have made just one basic change > (the compiled PreEquilibrium function) which at least gives a result > in a reasonable time: > > PreEquilibrium = > Compile[{C1, C2, C3, C4, C5}, > Evaluate[ > Block[{var1, var2, var3, var4, var5}, {var1, var2, var3, var4, > var5} /. > 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}]]]]; > > equilibrium[C1_, C2_, C3_, C4_, C5_] := > Thread /@ (Thread[{V1, V2} -> > PreEquilibrium[C1, C2, C3, C4, C5]] /. {V1 -> {var1, var2, var3, > var4, > var5}, V2 -> {var1, var2, var3, var4, var5}}) > > imagine[tests_] := > Select[tests, > And @@ {(Im[var1 /. #] == 0.), (Im[var2 /. #] == 0.), (Im[var3 /. #] > == > 0.), (Im[var4 /. #] == 0.), (Im[var5 /. #] == 0.)} &] > dynamics[C1_, C2_, C3_, C4_, C5_] := > Select[imagine[ > N[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_, ra1_, ra2_, > ra3_] /; (c1 == 0. || c2 == 0. || c3 == 0.) := 10^7 > go[C1_?NumberQ, C2_?NumberQ, C3_?NumberQ, C4_?NumberQ, R1_?NumberQ, > R2_?NumberQ, > R3_?NumberQ] := (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}] // Timing > > NMinimize::Failed to converge to the requested accuracy or > precision within 100 iterations. > > {227.508, {0.0157464, {c1 -> 3.79253*10^-8, c2 -> 0.316349, c3 -> > 7.93686, > ra1 -> 0.000436015, ra2 -> 0.000346922, ra3 -> 0.000722034}}} > > Note the message about the failure to converge. You can try various > options of NMinimize to see if you can get a better result. > > > Andrzej Kozlowski > > On 26 Jan 2008, at 10:01, Jason S. Kong 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 <kongjs 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 > > > > > -- Jason S. Kong Graduate Student, Chen Lab Department of Chemistry and Chemical Biology Baker Laboratory, Cornell University Ithaca, NY, 14853
- References:
- NMinimize Error In Evaluation
- From: "Jason S. Kong" <kongjs@gmail.com>
- NMinimize Error In Evaluation