Here is our problem:
Suppose g[y,z] and f[x,z] is complicated,we have to solve it numerically.
Writing this in Mathematica:
Clear["`*"]
g[y_, w_] := 1/(w - y + I*0.1)
f[x_, w_] := (x*x)/(w - x + I*0.1)
NIntegrate[(g[k2, w]/(1 - NIntegrate[f[k1, w], {k1, 0, 30}])), {k2, 0,
30}, {w, -1, 2}]
the error message is
NIntegrate::inumr: The integrand x^2/((2. +0.1 I)-x) has evaluated to non-numerical values
for all sampling points in the region with boundaries {{0,30}}.
Answer
This might be a duplicate of one of the examples in User-defined functions, numerical approximation, and NumericQ, but there isn't one with nested NIntegrate
.
ClearAll[f, g, h];
g[y_, w_] := 1/(w - y + I*0.1) (* note use of patterns y_, w_ *)
f[x_?NumericQ, w_?NumericQ] := (x*x)/(w - x + I*0.1)
h[w_?NumericQ] := NIntegrate[f[k1, w], {k1, 0, 30}];
NIntegrate[(g[k2, w]/(1 - h[w])), {k2, 0, 30}, {w, -1, 2},
PrecisionGoal -> 2, AccuracyGoal -> 8] (* for speed over accuracy; adjust as desired *)
(* -0.0249226 - 0.0125844 I *)
The trouble is that NIntegrate
evaluates the integrand once symbolically. When the outer one evaluates the inner one, the symbol w
does not have a value, so the inner NIntegrate
complains. Note this error does not prevent the integral from being evaluated; but it is annoying and the messages slow things down.
For the use of patterns in defining functions, see the tutorial Defining Functions and its related tutorials.
Comments
Post a Comment