NMinimice and NumberQ
- To: mathgroup at smc.vnet.net
- Subject: [mg62093] NMinimice and NumberQ
- From: "Guillermo Sanchez" <guillermosanchezleon at hotmail.com>
- Date: Fri, 11 Nov 2005 02:51:56 -0500 (EST)
- Reply-to: guillermo at usal.es
- Sender: owner-wri-mathgroup at wolfram.com
Dear group (*First evaluate these expressions, don't worry it contents ) ff1[{f2_, inp2_:inp, p2_:p, tiemp_:t}, inp1_, p1_, t0_, a_, var_, n_, opts___] := Module[{modelo, dd, ddd, tt, ff, i, j, g, X, sigma, invsigma, m, deter, bb}, Subscript[d, 0] = t0; modelo[inp_, p_, t1_] = f2 /. {inp2 -> inp, inp -> p2 -> p, tiemp -> t1}; g[t_] = Simplify[{D[modelo[inp, p, t], inp], D[modelo[inp, p, t], p]} /. {inp -> inp1, p -> p1}]; tt = Table[Subscript[t, i], {i, 0, n}]; dd = Table[Subscript[d, i], {i, n}]; ddd = FoldList[Plus, Subscript[d, 0], dd]; X = (g[#1] & ) /@ tt /. Thread[tt -> ddd]; ff[i_, j_] := Which[i == j, 1, i < j, E^((-a)*Sum[Subscript[d, k], {k, i, j - 1}]), i > j, E^((-a)*Sum[Subscript[d, k], {k, j, i - 1}])]; sigma = var*Array[ff, {n + 1, n + 1}]; invsigma = Inverse[sigma]; m = Transpose[X] . invsigma . X; m]; m1 = ff1[{1.12*E^(-0.04*p - 0.12*t)*inp + 0.7*E^(-1.09*p - 0.0012*t)*inp + 1.2*E^(-0.58*p - 0.0001*t)*inp}, 100000, 5, 0.5, 1, 1, 2]; dd = Table[Subscript[d, i], {i, 3}]; (*My questions start here *) (*I apply the below function to obtain {d1, ..., di, ..dn} by minimization. It works find but I wish increase the speed.*) deter[x_/;VectorQ[x,NumberQ]]:=Det[m1/. Thread[dd\[Rule]x]] ; NMinimize[{-deter[dd],Thread[dd>1]},dd];//Timing (* In this example the above function only spents a few seconds, however in real problem it spent a few minutes. I was looking for a faster way. I realize that the below expression usually (no always) spent the halt of the time but I need build it each time depending on the number of di, for this reason it is not applicable inside a package where the number of di can be chose by the user.*) deter1[d1_?NumberQ,d2_?NumberQ ,d3_?NumberQ ]:=Det[m1/. Thread[dd\[Rule]{d1,d2, d3}]] ;NMinimize[{-deter1[d1,d2,d3],Thread[{d1,d2, d3}>1]},{d1,d2, d3}];//Timing (* Note that in the below expression I only introduce a curly braces and the time of computation increases almost the double. Why? *) deter2[{d1_?NumberQ,d2_?NumberQ ,d3_?NumberQ }]:=Det[m1/. Thread[dd\[Rule]{d1,d2, d3}]] ;NMinimize[{-deter2[{d1,d2, d3}],Thread[{d1,d2, d3}>1]},{d1,d2, d3}];//Timing (* Any help for enhance deter[x_/;VectorQ[x,NumberQ]] *) Guillermo