Prev: NonlinearModelFit vector valued functions
Next: Bug? Analytical integration of cosines gets the sign
From: g.resta on 5 Feb 2010 03:28 I have the following little problem with a function defined by means of NIntegrate. My function is much more complicated, but the following example suffices. Suppose I've defined a function in this way fu[x_] := NIntegrate[ Cos[ x * Sin[t] ], {t, 0, 1}]; Mathematica can easily compute numerically and plot the function fu. Now, I want to use the function fu in another NIntegrate, say NIntegrate[ fu[x]^2, {x,0,2}] I hoped that was innocuous (maybe slow, since each point of fu require another automatic quadrature) but I got instead this error: NIntegrate::inumr: The integrand Cos[x Sin[t]] has evaluated to non- numerical values for all sampling points in the region with boundaries {{0,1}}. I also got a number which looks like the right value, but I'm afraid to trust it because I do not fully understand the error message. It seems like Mathematica is trying to do something symbolic with the guts of fu, even if fu is defined by means of NIntegrate. But I'm probably wrong. Surely I'm missing something. Can anybody show me the light? (that is, the right way to perform similar computations, maybe the right option to pass along?) thank you very much, giovanni
From: Leonid Shifrin on 5 Feb 2010 07:13 Hi, you have a couple of choices. Either use NIntegrate to do both integrations at once (the domain can be not necessarily rectangular): In[1]:= NIntegrate[Cos[x*Sin[t]],{t,0,1},{x,0,2}] Out[1]= 1.6679 or define your function only on numeric values: In[2]:= f[x_?NumericQ] := NIntegrate[Cos[x*Sin[t]], {t, 0, 1}]; In[3]:= NIntegrate[f[x], {x, 0, 2}] Out[3]= 1.6679 Regards, Leonid On Fri, Feb 5, 2010 at 11:19 AM, g.resta(a)iit.cnr.it <g.resta(a)iit.cnr.it>wrote: > I have the following little problem with a function defined by means of > NIntegrate. > My function is much more complicated, but the following example suffices. > > Suppose I've defined a function in this way > > fu[x_] := NIntegrate[ Cos[ x * Sin[t] ], {t, 0, 1}]; > > Mathematica can easily compute numerically and plot the function fu. > > Now, I want to use the function fu in another NIntegrate, say > > NIntegrate[ fu[x]^2, {x,0,2}] > > I hoped that was innocuous (maybe slow, since each point of fu require > another > automatic quadrature) but I got instead this error: > NIntegrate::inumr: The integrand Cos[x Sin[t]] has evaluated to non- > numerical values for all sampling points in the region with boundaries > {{0,1}}. > > I also got a number which looks like the right value, but I'm afraid > to trust it because > I do not fully understand the error message. It seems like Mathematica > is trying to do > something symbolic with the guts of fu, even if fu is defined by means > of NIntegrate. > But I'm probably wrong. > > Surely I'm missing something. Can anybody show me the light? > (that is, the right way to perform similar computations, maybe the > right option to pass along?) > > thank you very much, > giovanni > > > >
From: DrMajorBob on 5 Feb 2010 07:14
No problem here: Quit Clear[fu, f, t] fu[x_?NumericQ] := NIntegrate[Cos[x*Sin[t]], {t, 0, 1}] NIntegrate[fu[x]^2, {x, 0, 2}] 1.43171 Plot[fu[x], {x, 0, 2}, PlotRange -> All] Bobby On Fri, 05 Feb 2010 02:19:04 -0600, g.resta(a)iit.cnr.it <g.resta(a)iit.cnr.it> wrote: > I have the following little problem with a function defined by means of > NIntegrate. > My function is much more complicated, but the following example suffices. > > Suppose I've defined a function in this way > > fu[x_] := NIntegrate[ Cos[ x * Sin[t] ], {t, 0, 1}]; > > Mathematica can easily compute numerically and plot the function fu. > > Now, I want to use the function fu in another NIntegrate, say > > NIntegrate[ fu[x]^2, {x,0,2}] > > I hoped that was innocuous (maybe slow, since each point of fu require > another > automatic quadrature) but I got instead this error: > NIntegrate::inumr: The integrand Cos[x Sin[t]] has evaluated to non- > numerical values for all sampling points in the region with boundaries > {{0,1}}. > > I also got a number which looks like the right value, but I'm afraid > to trust it because > I do not fully understand the error message. It seems like Mathematica > is trying to do > something symbolic with the guts of fu, even if fu is defined by means > of NIntegrate. > But I'm probably wrong. > > Surely I'm missing something. Can anybody show me the light? > (that is, the right way to perform similar computations, maybe the > right option to pass along?) > > thank you very much, > giovanni > > > -- DrMajorBob(a)yahoo.com |