Re: Re: NIntegrate::inum continued
- To: mathgroup at smc.vnet.net
- Subject: [mg59222] Re: [mg59131] Re: NIntegrate::inum continued
- From: wtplasar at lg.ehu.es
- Date: Tue, 2 Aug 2005 00:42:53 -0400 (EDT)
- References: <200507300525.BAA21839@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Peter's advice didn't work, perhaps I didn't implemented it OK, but
his previous guidance help me in making some modifications to get a
fast and unproblematic code:
In[1]:=
datanw = {{0.04`,
36.38`, 0.19`}, {0.05`, 36.84`, 0.21`}, {0.0307`, 35.9`, 0.2`}};
In[2]:=
ndat = 3;
In[3]:=
z = datanw[[#1, 1]] & ; mi = datanw[[#1, 2]] & ;
smi = datanw[[#1, 3]] & ; SetAttributes[smi, Listable];
In[4]:=
f[x_, om_, w_] := 1/Sqrt[om*(1 + x)^3 + ( 1 - om)*(1 + x)^(3*(1 +
w))];
In[5]:=
rr[1, om_, w_] := rr[1, om, w] = NIntegrate[f[x, om, w], {x, 0, z
[1]}];
In[6]:=
rr[i_?IntegerQ, om_, w_] := rr[i, om, w] = rr[i - 1, om,
w] + NIntegrate[f[x, om, w], {x, z[i - 1], z[i]}];
In[7]:=
ff[i_?IntegerQ, om_, w_] := 5*Log[ 10, rr[i, om, w]*(1 + z[i])];
In[8]:=
ci = Sum[1/smi[i]^2, {i, 1, ndat}]; chi2f2[(om_)?
NumericQ, (w_)?NumericQ] := Module[{vec = ((mi[#1] - ff[#1,
om, w])/
smi[#1] & ) /@ Range[ndat]}, vec .
vec - Total[vec/smi[Range[ndat]]]^2/ci];
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[chi2f2[0.1, -1]]
Out[10]=
{0.01 Second, 0.315617}
Thanks to all of you for your time.
> wtplasar at lg.ehu.es schrieb:
> > Hi,
> >
> > I have these code which is a modification on a code which was improved
>
> > by one of the mathgroup members (see the original message below). When
>
> > I evaluate it a get a Recursion Limit problem. Can you help me?
> >
> >
> > In[1]:=
> > datanw={{0.04`,36.38`,0.19`},{0.05`,36.84`,0.21`},
> > {0.0307`,35.9`,0.2`}};
> >
> > In[2]:=
> > ndat=3;
> >
> > In[3]:=
> > Nn = ndat;
> >
> > In[4]:=
> > z =datanw[[#1,1]] & ; mi = datanw[[#1,2]] & ;
> > smi =datanw[[#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];
> >
> >
> >
> > ---------- Mensaje reenviado ----------
> > Para: mathgroup at smc.vnet.net
> > Asunto: NIntegrate::inum
> > De: <wtplasar at lg.ehu.es>
> > Fecha: Thu, 28 Jul 2005 02:35:51 +0200 (CEST)
> >
> > 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
> >
>
> In this case, there are other first arguments for rr possible than
> positive Integers. Replace the two definitions for rr by
> rr[zz_?NumericQ,om_?NumericQ,w_?NumericQ]:=NIntegrate[f[x,om,w],
{x,0,zz=
> }];
> and chi2f2[(om_)?NumericQ, (w_)?NumericQ]:=... by chi2f2
[om_,w_]:=...=
> =2E
>
> Good luck,
> Peter
> --
> Peter Pein
> Berlin
> http://people.freenet.de/Peter_Berlin/
>