2

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 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

dont forget to Clear[F] ( or restart your kernel..)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.