1
votes

To illustrate my problem here is a toy example:

F[x_] := Module[{out},
   If[x > 1,
    out = 1/2,
    out = 1
    ];
   out
   ];

The function can be evaluated and plotted. However when I try to numerically integrate it I get an error

NIntegrate[F[x], {x, 0, 2}]

NIntegrate::inumr: The integrand out$831 has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,2}}. >>

1

1 Answers

3
votes

Integrate and some other functions will do some probing with symbolic values first. Your Module when given only the symbol x will return only the (aliased) out.

Fix this by defining your F only for numeric values. NIntegrate will discover this and then use numeric values.

In[1]:= F[x_?NumericQ] := Module[{out},
   If[x > 1, out = 1/2, out = 1]; out];
NIntegrate[F[x], {x, 0, 2}]

Out[2]= 1.5