Re: NIntegrate::inum
- To: mathgroup at smc.vnet.net
- Subject: [mg59122] Re: NIntegrate::inum
- From: Peter Pein <petsie at dordos.net>
- Date: Fri, 29 Jul 2005 00:42:05 -0400 (EDT)
- References: <dca01n$cul$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
wtplasar at lg.ehu.es schrieb: > Hi, > > I have to minimize a function which is defined through a numerical > integral. I get the "NIntegrate::inum .." error message. I know I can > switch it off, but I wonder if there is a more elegant way to deal > with the problem. > > These my input and output lines: > In[1]:= > Do[z[i] = i, {i, 1, 50}] > Do[mi[i] = i^2, {i, 1, 50}] > Do[smi[i] = i^3, {i, 1, 50}] > > In[4]:= > f[x_, om_, w_] := 1/Sqrt[om (1 + x)^3 + (1 - om)(1 + x)^(3*(1 + w))];= > rr[zz_?NumberQ, om_, w_] := NIntegrate[f[x, om, w], {x, 0, zz}]; > ff[zz_?NumberQ, om_, w_] := 5*Log[10, rr[zz, om, w]*(1 + zz)]; > Nn = 50; > > In[8]:= > ci = Sum[1/smi[i]^2, {i, 1, 50}]; > > In[9]:= > chi2f2[om_, w_] := Sum[(mi[i] - ff[z[i], om, w])^2/smi[i]^2, {i, 1, > Nn}] - > (Sum[(mi[i] - ff[z[i], om, w])/smi[i]^2, {i, 1, Nn}])^2/ci > > In[10]:= > Timing[NMinimize[{chi2f2[om, w], 0 =A1=DC om =A1=DC 1}, {om, w}]] > > NIntegrate::inum: Integrand ..... is not numerical at {x} = {0.5} > > Out[10]= > {38.966 Second, {0.26337, {om -> 0.999998, w -> 0.0738109}}} > > > Thanks in advance, > > Ruth Lazkoz > Hi Ruth, this is, what I get with your input after some NIntegrate::inum messages:= Out[14]= {15.922*Second, {0.26337014333139525, {om -> 0.9999982304774572, w -> 0.07381086751638463}}} But if I redefine chi2f2 to evaluate only numeric arguments, results change dramatically: In[15]:= Clear[chi2f2]; chi2f2[(om_)?NumericQ, (w_)?NumericQ] := (* exactly your code *) In[17]:= Timing[NMinimize[{chi2f2[om, w], 0 <= om <= 1}, {om, w}]] Out[17]= {277.25*Second, {0.14928592347858327, {om -> 5.870827547618156*^-8, w -> -2.43876283722217}}} OK, it seems to be necessary to experiment with this function - let's speed it up a bit: z = #1 & ; mi = #1^2 & ; smi = #1^3 & ; SetAttributes[smi, Listable]; f[x_, om_, w_] := 1/Sqrt[om*(1 + x)^3 + (1 - om)*(1 + x)^(3*(1 + w))]; rr[1, om_, w_] := rr[1, om, w] = NIntegrate[f[x, om, w], {x, 0, 1}]; rr[zz_, om_, w_] := rr[zz, om, w] = NIntegrate[f[x, om, w], {x, zz - 1, zz}] + rr[zz - 1, om, w]; ff[zz_, om_, w_] := 5*Log[10, rr[zz, om, w]*(1 + zz)]; ci = Sum[1/smi[i]^2, {i, 1, Nn}]; chi2f2[(om_)?NumericQ, (w_)?NumericQ] := Module[{vec = ((mi[#1] - ff[z[#1], om, w])/smi[#1] & ) /@ Range[Nn]},= vec . vec - Total[vec/smi[Range[Nn]]]^2/ci]; In[10]:= Nn = 50; Timing[NMinimize[{chi2f2[om, w], 0 <= om <= 1}, {om, w}]] Out[11]= {17.375*Second, {0.1492859234785726, {om -> 5.870827547618156*^-8, w -> -2.43876283722217}}} In[12]:= Nn = 1000; chi2f2[0., -2.5424] Out[12]= 0.16636080313033652 In[13]:= NMinimize[chi2f2[0, w], w] Out[13]= {0.1663608031303223, {w -> -2.542400491622037}} In[14]:= (*let's have a look at om==1*) NMinimize[chi2f2[1, w], w] Out[14]= {0.2821141395387428, {w -> 704.7500004178966}} In[15]:= Nn = 50; NMinimize[chi2f2[1, w], w] Out[16]= {0.2633701352226361, {w -> 704.7500004178966}} Are there any expected results? -- Peter Pein Berlin