I have the following function:

h = 1;
c = 1;
k = 1;
B2 = (2*h*c^2)/(x^5 (Exp[(h*c)/(x*k*T)] - 1));

(someone can see that this integral is the Planck function). In the simplest case the function T is a constant.

But if I have that T is itself a function of a parameter: y, then the following integral could be solved only via numerical method:

B1 = (2*h*c^2)/(x^5 (Exp[(h*c)/(x*k*y^(-3/4))] - 1));
Rslt=NIntegrate[2*Pi*y*B1,{y, 1, 10}]

But what I like to have is the result of the integral as a function to be plotted in terms of the variable x, as follow:

LogLogPlot[Rslt, {x, 1, 10}]

I do not understand if it is possible to do that or there is a problem in the definition of the integral or something else.

Could someone help me?

Thanks a lot.

  • 2
    $\begingroup$ Have you tried defining functions? (See SetDelayed[]) $\endgroup$ Jan 5 '15 at 17:39
h = c = k = 1;

b1[x_, y_] := 2 h c^2/(x^5 (Exp[h c y^(3/4)/(x k)] - 1));
rslt[x_?NumericQ] := NIntegrate[2 Pi y b1[x, y], {y, 1, 10}];
LogLogPlot[rslt[x], {x, 1, 10}]

Edit: There seems to be an issue with LogLogPlot in V10.0.1 related to this and Bob Hanlon's answer, which has been posted here: LogLogPlot plugs in zero. Error messages are generated, but the correct plot is produced. One can use Quiet, if the messages are annoying.

  • 1
    $\begingroup$ +1 for doing it right, even though the Q is a combination of a borderline "easily found in the documentation" question and a duplicate of the ?NumericQ "pitfalls" answer. $\endgroup$
    – Michael E2
    Jan 6 '15 at 1:22
  • $\begingroup$ @MichaelE2 Thanks! On a short calc like this I would probably do it as Bob did in his answer, but here I tried to show a safer approach to the OP. Anyway I'm quite sure it has been asked a zillion times before (and it will keep coming) $\endgroup$ Jan 6 '15 at 2:56
  • $\begingroup$ Strangely, I get "integrand has evaluated non-numerical values" errors with both Bob's code and yours in V10.0.1 but not in V9. Probably a bug, I guess. Update: rslt[x_ /; x != 0] := NIntegrate[2 Pi y b1[x, y], {y, 1, 10}] fixes it. LogLogPlot likes to plug in x = 0, for fun, I guess, since it's clearly a dumb thing to do in a log-log plot. $\endgroup$
    – Michael E2
    Jan 6 '15 at 12:09
  • $\begingroup$ @MichaelE2 V9 Here.Impossible to check. Please go ahead and add a note to the answer warning about that. $\endgroup$ Jan 6 '15 at 13:38
h = 1; c = 1; k = 1;

B1 = (2*h*c^2)/(x^5 (Exp[(h*c)/(x*k*y^(-3/4))] - 1));

Rslt can only be evaluated for a numerical value of x so it shoud be defined as

Rslt[x_?NumericQ] := NIntegrate[2*Pi*y*B1, {y, 1, 10}]

LogLogPlot[Rslt[x], {x, 1, 10}]

enter image description here

  • 1
    $\begingroup$ Interesting, I get the same plot with LogLogPlot[Rslt[100], {x, 1, 10}], but not with LogLogPlot[Rslt[x0], {x0, 1, 10}]. The reasons for this are quite subtle and to hard to address here. In part, it's because LogLogPlot sets the value of x by using Block to temporarily set the global value of x, which bypasses the local parameter x in the definition of Rslt. Basically the local parameter of x is irrelevant, except that the pattern test prevents the evaluation of NIntegrate until x is assigned a numeric value through Block. See belisarius's answer for the correct way. $\endgroup$
    – Michael E2
    Jan 6 '15 at 1:17
  • $\begingroup$ Bob, you might be interested in the comment and bug related to this problem I mentioned on belisarius's post. $\endgroup$
    – Michael E2
    Jan 6 '15 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.