Re: (again) common problem with FindMinimum and NIntegrate

*To*: mathgroup at smc.vnet.net*Subject*: [mg69535] Re: (again) common problem with FindMinimum and NIntegrate*From*: Paul Abbott <paul at physics.uwa.edu.au>*Date*: Fri, 15 Sep 2006 06:44:43 -0400 (EDT)*Organization*: The University of Western Australia*References*: <eebf11$1iv$1@smc.vnet.net>

In article <eebf11$1iv$1 at smc.vnet.net>, wtplasar at ehu.es wrote: > Some people have enquired in the past about how to avoid error > messages similar to this one I get: > > NMinimize::nnum: The function value > Experimental`NumericalFunction[{Hold[bestchi[om, ol]], Block}, > <<4>>, {None, None}] is not a number at {ol, om} = > {1.5636149496419025`,0.08724393316032257`} > > I have read the documentation and tried to implement the > recommendations posted at mathgroup, but to no avail. > > Here is my code (it won't take much from your time): > > ndat = {1.`, 2.`, 3.`, 4.`, 5.`, 6.`, 7.`, 8.`, 9.`}; > Hobs = {69.`, 83.`, 70.`, 87.`, 117.`, 168.`, 177.`, 140.`, 202.`}; > sH = {12.`, 8.3`, 14.`, 17.4`, 23.4`, 13.4`, 14.2`, 13.5`, 40.4`}; > z = {0.09`, 0.17`, 0.27`, 0.4`, 0.88`, 1.3`, 1.43`, 1.53`, 1.75`}; > H[x_, om_, ol_, H0_] := H0 Sqrt[om (1 + x)^3 + ol + (1 - om - ol)(1 + > x)^2]; > Hlist[om_, ol_, H0_, mylist_] := Thread[H[#, om, ol, H0]] &[mylist]; > chisq[om_?NumericQ, ol_?NumericQ, H0_? > NumericQ] := Switch[Head[N[#]], Real, #, Complex, 10^20] &[Module > [{vec1 = (Hobs - Hlist[om, ol, H0, z])^2, vec2 = (sH^2)}, Total > [vec1/vec2]]]; > bestchi[om_?NumericQ, ol_?NumericQ] := -2*Log[NIntegrate[1/(3 Sqrt[2 > Pi])Exp[- > Chop[(H0 - 73)]^2/18]Exp[-chisq[om, > ol, H0]/2], {H0, 71, 75}, WorkingPrecision -> 20]]; > NMinimize[{bestchi[om, ol], 0.7 > om > 0 && ol > 0}, { > om, ol}, Method -> {"RandomSearch"}]; > > I would appreciate someone posted a reply to show me how to fix it. I think that the error message is spurious, and not helpful -- there is no problem with bestchi[om, ol] at {ol, om} = {1.5636149496419025`,0.08724393316032257`} as direct numerical evaluation will show. First, have a look at a ContourPlot of bestchi: ContourPlot[bestchi[om, ol], {om, 0, 0.7}, {ol, 0, 1}] This may give you a clue as to why NMinimize might have a problem. You are trying to minimize -2 Log[ f ]. The effect of Log will be to flatten out the function. If you modify bestchi, calling it bestchi2, to evaluate f instead of -2 Log[ f ] and then do ContourPlot[bestchi2[om, ol], {om, 0, 0.7}, {ol, 0, 1}] you can see where to look for the maximum. Then NMaximize[{bestchi2[om, ol], 0.7 > om > 0 && ol > 0}, {{om, 0.1, 0.3}, {ol, 0.5, 0.7}}, Method -> {"RandomSearch"}] works without problem. Cheers, Paul > > Thanks a lot in advance, > > Ruth Lazkoz _______________________________________________________________________ Paul Abbott Phone: 61 8 6488 2734 School of Physics, M013 Fax: +61 8 6488 1014 The University of Western Australia (CRICOS Provider No 00126G) AUSTRALIA http://physics.uwa.edu.au/~paul