Message Boards Message Boards


Solve numerically the following integral?

Posted 1 year ago
10 Replies
6 Total Likes

If I have a simple integral that I wish to solve numerically, such as

NIntegrate[x^a, {a, 1., 2., .1}]

often on the first evaluation, I just get the function back since x is undefined.

However, the second time I do this, I get: "NIntegrate::inumr: The integrand x^a has evaluated to non-numerical values for all sampling points in the region with boundaries {{1.,2.}}"

When I ClearAll or Clear x and the variable, the integral still tries to evaluate and I get the same statement as above.

Why does Mathematica try to evaluate the integral when all terms are not defined? In other more complex functions, if I miss a variable or screw up the syntax, the function does not evaluate and the function is repeated as the output, and I get no statement such as the above, which is a cue to look for a syntax error.

Also, in some cases, when I set up the accuracy and precision to be some value and set the working precision to be twice these values, I also get inane statements like "the precision in the argument is less than the working precision." Of course it is!!

These behaviors likely have an obvious cause and can likely be simply explained...I hope.

Thanks for the help.


10 Replies

It would be much easier to respond if examples of each phenomenon were included in the post. The one example provided is NIntegrate[x^a, {a, 1., 2., .1}].

In a fresh session I get the following .

In[1]:= NIntegrate[x^a, {a, 1., 2., .1}]

During evaluation of In[1]:= NIntegrate::inumr: The integrand x^a has evaluated to non-numerical values for all sampling points in the region with boundaries {{1.,2.}}.

Out[1]= NIntegrate[x^a, {a, 1., 2., 0.1}]

Is this an unexpected behavior? I could not tell from the wording of the post whether this, or something else, was unexpected.

Posted 1 year ago

Daniel, your reply did not post in the User Group. I got your message and logged in on the User Group web site but your reply is not here. It seems you duplicated my issue, which is why I did not attach a worksheet, since I wanted an independent verification outside of my Mathematica application.

The issue is to me clear: somehow the simple numerical integration does not execute on the first attempt to evaluate but it does execute on the second attempt with the message you posted. Why does it execute the second time and throw out an inane comment? If I use the same process on a more complete numerical integration with the precision and accuracy goal and the working precision indicated, the same thing happens: it does not execute on the first iteration but does on the second, even though all parameters are not defined and so the numerical integration cannot proceed...yet on the second evalution is does with the second inane comment I posted.

Why does this happen and how do I keep it from happening? The reason I would like some clarity is that these integrals are defined as functions that are embedded in more complex expressions and I cannot assure that the second and unexpected execution has not created an expression that is incorrect in the more complete expression.

Now, my concern may not mean anything to you but to me it means that I cannot trust the results that I calculate. The reason is that I am preparing a worksheet to post in the physics section that identifies a particular integral that needs to be numerically evaluated and there are some issues I am addressing on how to interpret the results given how Mathematical performs the numerical calculations.

(1) My response is in the forum. Not sure why it ws not visible to you, might be a glitch on one of our servers.

(2) The only behavior from that NIntegrate example I can elicit is where it gives the error message and returns unevaluated. I find it to be a very useful error message, by the way. Returning unevaluated with no indication as to why would be unhelpful, not to mention annoying.

(3) The warning messages associated with precision could be better. The usual reason such a message appears is that one requests WorkingPrecision that is higher than the precision of the input. So again, the message is useful, but sometimes people fail to understand what it is telling them and that could perhaps be due to the wording.

(4) The questions posed do not have inputs where I can actually figure out what is the underlying issue.

Posted 1 year ago

If I set a variable, call it d and evaluate d without giving it a value, I simply get d matter how many times I evaluate d.

In the simple function I used as an example, when I evaluate that function without defining certain variables, I get the function back with no comment. If I evaluate this function a second time or more, I get the comment back that it evaluates with indeterminate values.

If a more complicated function with the accuracy and precision and working precision limits set but no variables defined, again, the first evaluation gives back the function with no comments. The second and later evaluations with the variable still not defined gives back the statement that it tried to evaluate the function and that the function's precision was less than the working precision. What can this possibly mean, since the function should not evaluate at all? And, I do not want some spurious evaluation to "contaminate" the function.

I don't understand what the expected output is for the example. You have variables x and a but you specify limits only for a, Second, what is the last parameter for? Usually, limits of integration are, for example, like {a, 1., 2.} -- the extra value doesn't seem to have any use -- and it is not included in the documentation.

The error message is clear. Since you did not specify any range for x, this is a symbolic integration, and that does not work for NIntegrate. If you replace x with a number, the result appears immediately.

If I leave off the last parameter, I get a different result, but the documentation does not cover any limits other than {x, xmin, xmax}.

Posted 1 year ago

There are two issues. The first is that if there is nothing to execute, then why show any results? I have input the function NIntegrate[x^a, {a, 1., 2., .1}], it should not execute, and when it is first executed, in fact I simply get back NIntegrate[x^a, {a, 1., 2., .1}]. If I re-execute an evaluation on the same function, it evaluates. Why?

My goal is to set fff=NIntegrate[x^a, {a, 1., 2., .1}] and then to use fff over variable x to do a parametric evaluation. But if, when setting fff, the function evaluates to something, what is it evaluating to and what is the function fff that I am using? I don't really have to do it this way but I did and I got an output that is least to me. To my simple way of looking at things, if it should not evaluate, and so, then, it should not give back something I don't understand.

I simply want to understand what is going on and what the "false" evaluation is leaving hidden in or associated with the function fff.

(1) I have never seen NIntegrate[x^a, {a, 1., 2., .1}] do anything other than return unevaluated. Below is the behavior i get every time.

In[89]:= NIntegrate[x^a, {a, 1., 2., .1}]

During evaluation of In[89]:= NIntegrate::inumr: The integrand x^a has evaluated to non-numerical values for all sampling points in the region with boundaries {{1.,2.}}.

(* Out[89]= NIntegrate[x^a, {a, 1., 2., 0.1}] *)

Notice the output is the same as the input (that's what "returning unevaluated" means). If you are getting anything different from this, then either x had been given a value somewhere, or else we (at Wolfram Research) really need to see a screen shot, and also the notebook itself, for further diagnosis.

(2) As had been noted, the use of {a, 1., 2., .1} as an integration range is suspect. It almost certainly does not do whatever you might be thinking it does.

(3) One can (and typically does) define a function to do a parametrized numeric integration. I show a very simple example of this below, based on the integrand under discussion.

f[x_?NumericQ] := Integrate[x^a, {a, 1., 2.}]

Table[f[x], {x, .1, 1., .1}]

(* Out[96]= {0.0390865033713, 0.0994135895295, 0.174422544467, \
0.261925600305, 0.360673760222, 0.469827645353, 0.588771382932, \
0.717027218836, 0.854209942293, 1.} *)

@GeorgeWoodrowIII The multi-point form is not heavily covered in the NIntegrate reference page documentation but it is shown under Scope > Basic Uses in the example in "Along a piecewise linear contour in the complex plane:". That said, the use in this post seems unlikely to be what was intended-- why would one integrate along a path from 1 to 2 and then back to 0.1, instead of just integrating from 1 to 0.1 to begin with?

Posted 1 year ago

I am not comfortable with working in the complex domain when the arguments to integrals are real and finite. I went through all the advanced mathematics and complex variable analysis coursework in my youth but in the pre-Mathematica days I just used Gratshteyn and Ryzhik and kept my fingers crossed. If that did not work out I resorted to writing numerical integral code using the trapezoidal or Simpson rules and stayed away from the complex plane. I simply do not want to have to understand the complex solutions that arise when what are finite real integrals throw off complex solutions.

Posted 1 year ago

OK, now that I have that off my chest, I have attached a worksheet that one of the Community members helped me develop. Do not pay attention to why I want to evaluate the particular integral, since I was going to post this to the Physics section. It is the real mutual attraction model that has never been solved....the classical point-mass integral we are all taught is a heuristic.

Consequently, I set out to prove my point. The issue became that the mutual attraction between sphere's is so close to the point-mass model that the solutions are really perturbations on the point-mass approach....which is why no issues have ever been identified. If the model is extended to cylinders and internal masses...a test mass within a distributed mass such as a sphere or disk, the results are smooth and totally unlike the point-mass results...the solutions are not simply perturbations. My goal has been to understand the detail of the spherical solutions. However, the perturbations are on the same order as the variances in the numerical results when I switch between coordinate-system descriptions of the spheres.

That being said, the worksheet models have been set up to explicitly evaluate at one point and I was attempting to generalize the approach to allow a parametric evaluation over the separation distance. It is actually easy to do and I have voluminous plots showing the result. However, when using the worksheet as a template and getting ready to post this material in the physics community, I got the behavior that I have been grumbling over and I do not understand what is going on. So far, I still don't understand the issue. To may simplistic way of looking at things, the integrals should not have evaluated at all until I defined the variable parameter...which it did in my original clunky methodology. I have no doubts about the validity of the results, but I wanted to create a more sophisticated approach and ran into these issues.

I am a bear about details, which is why I noticed that the Newtonian gravity model...or static electric interaction models as we use them...are heuristics. Now the issue is that I simply cannot understand why the numerical models evaluate at all when they are incomplete.

Regards and thanks for your forbearance.


Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract