Re: Recognising parameters in function
- To: mathgroup at smc.vnet.net
- Subject: [mg94570] Re: Recognising parameters in function
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Tue, 16 Dec 2008 02:37:19 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <gi5jmt$ppv$1@smc.vnet.net>
Stuart Nettleton wrote: > Hi, would someone be able to suggest why FindMinimum will recognise > parameters in the following function but the backsubstitution will not. > Thanks for any help, Stuart > Clear[f, vars1, z]; > vars1 = {x, y}; > z = (x - 5)^2 + (y - 3)^2; > f[vars_] := Module[{a}, > a = z/2; > Return[a] > ] /; VectorQ[vars, NumericQ]; > optim = FindMinimum[Join[{f[vars1]}, Thread[vars1 >= 0]], vars1] > optim[[2]] > f[vars1] /. optim[[2]] Hi Stuart, The test on the parameter of f apparently interferes with FindMinimum and prevent the replacement of the numerical values due to the order of argument evaluation. (One can use f[vars1] /. optim[[2]] // Trace to see what is going on.) Without this test, everything works fine (at least on my system :-) In[1]:= $Version Out[1]= "6.0 for Mac OS X x86 (64-bit) (May 21, 2008)" (* Case #1 w/o conditions on arguments *) In[2]:= Clear[f, vars1, z]; vars1 = {x, y}; z = (x - 5)^2 + (y - 3)^2; f[vars_] := Module[{a}, a = z/2; Return[a] ] optim = FindMinimum[Join[{f[vars1]}, Thread[vars1 >= 0]], vars1] optim[[2]] f[vars1] /. optim[[2]] Out[6]= {2.57563*10^-28, {x -> 5., y -> 3.}} Out[7]= {x -> 5., y -> 3.} Out[8]= 2.57563*10^-28 (* Case #2 with conditions on the arguments of f *) In[9]:= Clear[f, vars1, z]; vars1 = {x, y}; z = (x - 5)^2 + (y - 3)^2; f[vars_] := Module[{a}, a = z/2; Return[a] ] /; VectorQ[vars, NumericQ]; optim = FindMinimum[Join[{f[vars1]}, Thread[vars1 >= 0]], vars1] optim[[2]] f[vars1] /. optim[[2]] During evaluation of In[9]:= FindMinimum::eit: The algorithm does not \ converge to the tolerance of 4.806217383937354`*^-6 in 500 \ iterations. The best estimated solution, with feasibility residual, \ KKT residual or complementary residual of {4.92518,0.985028,3.94015}, \ is returned. >> Out[13]= {2.30357*10^-14, {x -> 5., y -> 3.}} Out[14]= {x -> 5., y -> 3.} Out[15]= 1/2 ((-5 + x)^2 + (-3 + y)^2) Regards, -- Jean-Marc