Re: Problem of NUMERICAL constraints (Ndsolve) with Nminimize
- To: mathgroup at smc.vnet.net
- Subject: [mg100876] Re: Problem of NUMERICAL constraints (Ndsolve) with Nminimize
- From: laianhngoc at yahoo.com
- Date: Wed, 17 Jun 2009 04:36:04 -0400 (EDT)
- Reply-to: laianhngoc at yahoo.com
Hi Daniel, I understand what u want to say. In other words, do u think that is not one of inconvenient of Mathematica??? Thanks. Anh. --- On Wed, 6/17/09, dh <dh at metrohm.com> wrote: From: dh <dh at metrohm.com> Subject: [mg100876] Re: Problem of NUMERICAL constraints (Ndsolve) with Nminimize To: laianhngoc at yahoo.com Date: Wednesday, June 17, 2009, 1:21 AM Hi, your main problem is not the time it takes to solver 1000 differential equa= tions (DE), but that "x" appears in the DE. That is, for different x, you h= ave different DE. Therefore, you can not numerically pre-solve the DE. That= would only be possible if you were able to symbolically solve the DE with = x a parameter. Daniel laianhngoc at yahoo.com wrote: > Hi, > > Let take an simple example: > > f[x_?NumericQ] := y[0.5] /.NDSolve[{y'[t] == (x*y[t] + Exp[-t])/(1 = + y[t]), y[0] == 1},y, {t, 0, 1}][[1, 1]]; > > cons[x_] := Table[i f[x] > 0, {i, 1000}]; fun[x_?NumericQ] := x^2; > > NMinimize[Prepend[cons[x], fun[x]], x]// Timing > > and the result : {38.5, {5.55112*10^-17, {x -> -7.45058*10^-9}}} > > Here i have a Table : {1*f[x]>0,...,1000*f[x]>0} as constraints. Do you t= hinks that there is a problem of REevaluate of Ndsolve 1000 times? so what = i want to do is to compute f[x] One time for All in the constraints. Do u t= hinks that i can do it in Mathematica? Another system can do something like that bu= t i dont see how with Mathematica, and i need Mathematica to solve my probl= em. > > Thanks for your help. > > Anh. > > --- On *Tue, 6/16/09, dh /<dh at metrohm.com>/* wrote: > > > From: dh <dh at metrohm.com> > Subject: Re: Problem of NUMERICAL constraints (Ndsolve) with = Nminimize > To: "Anh Ngoc LAI" <laianhngoc at yahoo.com> > Date: Tuesday, June 16, 2009, 7:10 AM > > Hi, > I still not sure what you try to do. Try to give a simple exa= mple. > Are you trying to construct the constrains using a function? > This can be done like: > ===================== ====== > cons[x_] := Table[x > i, {i, 3}]; > fun[x_?NumericQ] := x^2; > NMinimize[Prepend[cons[x], fun[x]], x] > ===================== ===== > However, if the constrains change with x, your are out of luc= k. > Daniel > > > Anh Ngoc LAI wrote: > > Hi Daniel, > > > > Thanks for your reply, > > > > I want to minimize a function using NMinimize and and a lot = of > constrains (about 1000). But > > the constraints are not in that CONCRET form like your examp= le. I > have to construct it using the form (for example) : > > > > f[x_?NumericQ, ...] = With[{a=A[0.5]/.NDSolve[x],...}, C= [a,y[[i]]]] > > > where y[[i]] is data and C[a,y[[i]]] depend on = y[[i]]: if y[[i]] > is 1000 data then C[a,y[[i]]] is a List of 1000 elements. > > > > Then i use f[x,...] for my constraints (C[a,y[[i]]]>0). > > > > And here come the problem: > > > > + If C[a,y[[i]]] is a scalar, i have no problem, i just put > f[x,...]>0 on my constraint and it's ok. > > > > ++ But if C[a,y[[i]]] is a List of Element, I dont know what= can > i do, because for Mathematica, f[x, ...] is just one element,= not a > List. > > > > Are there any way that Mathematica take directly C[a,y[[i]]]= as > constraints or understant that f[x, ...] is a List ??? it see= ms to > me that we can not do it with Mathematica??? i lost a lot of = time > for it but i dont see how? > > > > I'm thinking to create 1000 functions like : > > > > f1[x_?NumericQ, ...] = With[{a=A[0.5]/.NDSolve[x],...}, = C[a,y[[1]]]]; > > > > ... > > > > f1000[x_?NumericQ, ...] = With[{a=A[0.5]/.NDSolve[x],...= }, > C[a,y[[1000]]]]; > > > > As you see, Mathematica have to REcalcule "a" each time, and= that > can take a lot of time. To ovecome the problem of time, i thi= nk to > use Memoization but the memory may fill up quickly. > > > > Well, in all cases, i will try this last option but any > suggestions will be very very very welcome. > > > > Thanks. > > > > Anh. > > > > --- On *Tue, 6/16/09, dh /<dh at metrohm.com>/* wrote: > > > > > > From: dh <dh at metrohm.com> > > Subject: Re: Problem of NUMERICAL constraints (N= dsolve) with > Nminimize > > To: "Anh Ngoc LAI" <laianhngoc at yahoo.com> > > Date: Tuesday, June 16, 2009, 1:39 AM > > > > Hi Anh, > > if I understand correctly, you want minimize a f= unction using > > NMinimize and several constrains. > > Here is an example: > > ================= ====== > > fun=x^2 - (y - 1)^2 > > cons={x^2 + y^2 <= 4, x > 1} > > NMinimize[Prepend[cons, fun], {x, y}] > > ================= ======= > > Daniel > > > > Anh Ngoc LAI wrote: > > > Hello everybody, > > > I have to optimize an function with numerica= l constraints > using > > Nminimize. To construct the constraints, i have = to solve a > system of > > ODEs (type): > > > Sol=NDSolve[{{B'[ > > > t] == -(t6 + t13) B[t] -(t7= + t15 + t14*t11) F[ > > > t] +(1/2)*(B[t] B[t] + t11*= F[t] F[t] + t12*GG[t] > GG[t]) - t2, > > > F'[t] == -t8*F[t] - t3, > > > GG'[t] == -(t9 + t16) F[t] - (t= 10 + t17) GG[t] - t4, > > > A'[t] == > > > t5 B[t] +((t5*t7/t6) - t14) F[ > > > t] +(1/2)*(F[t] F[t] + G= G[t] GG[t]) - t1}, {B[0] == > > F[0] == > > > GG[0] == A[0] == 0}}, {= B, F, GG, A}, {t, 0, 10}][[1]] > > > where t1,...,t17 are parameters to obtain. > > > and then i have to use A,B,F,GG and my data = to construct this > > constraints. > > > So i use WITH (or Block) commend for it: > > > f[t1_?NumericQ, t2_?NumericQ, t3_?NumericQ, = t4_?NumericQ, > > > t5_?NumericQ, t6_?NumericQ, t7_?Numeric= Q, t8_?NumericQ, > > > t9_?NumericQ, t10_?NumericQ, t11_?Numer= icQ, t12_?NumericQ, > > > t13_?NumericQ, t14_?NumericQ, t15_?Nume= ricQ, t16_?NumericQ, > > > t17_?NumericQ] := With[{...}, C] > > > Here C are constraints to put on Nminimize o= ptimization. > > > I don't know how to deal with this, if C is = only one > constraint > > i have no problem with, but > > > here C is a list. For Mathematica, > > > f1[t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11= , t12, t13, > t14, > > t15, t16, t17] is only one > > > element and not a List. > > > I think to another way to solve this problem= which is to get > > A,B,F,GG by defining functions > > > directly with Ndsolve (then construct constrain= ts with Data > > without using WITH) but i have > > > to evaluate a lot of time NDSOLVE on my procedu= re, and it > take a > > much of time. > > > > > > That will be great if someone could give an ide= a. > > > Thanks. > > > > > > > > > > -- Daniel Huber > Metrohm Ltd. > Oberdorfstr. 68 > CH-9100 Herisau > Tel. +41 71 353 8585, Fax +41 71 353 8907 > E-Mail:<mailto:dh at metrohm.com> > Internet:<http://www.metrohm.com> > > -- Daniel Huber Metrohm Ltd. Oberdorfstr. 68 CH-9100 Herisau Tel. +41 71 353 8585, Fax +41 71 353 8907 E-Mail:<mailto:dh at metrohm.com> Internet:<http://www.metrohm.com>