1

I am trying to solve coupled differential equations, inside which I have a function which I am getting by doing NIntegrate. The code is shown below:

x[z_] := NIntegrate[(x - 1)/x Sqrt[x]  BesselK[1, z Sqrt[x]], {x,1, \[Infinity]}];  
y[z_] := N[z^2*BesselK[2, z]];
s = NDSolve[{m1'[z] == x[z] * (m1[z] - 1),m2'[z] == (m1[z]/y[z] - 1)*m2[z]* 1/2* x[z], {m1[0.01] == y[0.01],m2[0.01] == 10^-14}}{m1, m2}, {z, 0.01, 10}]

But when I run this code I am getting following error massages:

NIntegrate::inumr: The integrand ((-1+x) BesselK[1,Sqrt[x] z])/Sqrt[x] has evaluated to non-numerical values for all sampling points in the region with boundaries {{[Infinity],1.}}.

NIntegrate::inumr: The integrand ((-1+x) BesselK[1,Sqrt[x] z])/Sqrt[x] has evaluated to non-numerical values for all sampling points in the region with boundaries {{[Infinity],1.}}.

General::stop: Further output of NIntegrate::inumr will be suppressed during this calculation.

NDSolve::nbnum1: The function value 0.01 Im[Sqrt[x]]==0 is not True or False when the arguments are {0.01,1.99995,1.10^-14,3.9987610^6,-2.21987*10^-24}.

NDSolve::nbnum1: The function value 0.01 Im[Sqrt[x]]==0 is not True or False when the arguments are {0.01,1.99995,1.10^-14,3.9987610^6,-2.21987*10^-24}.

NDSolve::nbnum1: The function value 0.01 Re[Sqrt[x]]<=0 is not True or False when the arguments are {0.01,1.99995,1.10^-14,3.9987610^6,-2.21987*10^-24}.

General::stop: Further output of NDSolve::nbnum1 will be suppressed during this calculation.

NDSolve::ecboo: The value of event condition function at z = 0.01` was not True or False. The event will be considered inactive.

NDSolve::ecboo: The value of event condition function at z = 0.010000000062299782` was not True or False. The event will be considered inactive.

NDSolve::ecboo: The value of event condition function at z = 0.010000000124599564` was not True or False. The event will be considered inactive.

General::stop: Further output of NDSolve::ecboo will be suppressed during this calculation.e here

Is it a problem in NIntegrate? How should I bypass these errors?

Arghya Datta
  • 269
  • 1
  • 6
  • 3
    This is a very common problem on this site. Just add ?NumericQ to your functions e.g x[z_?NumericQ]:=... and y[z_?NumericQ]:=... . You also need to fix your braces in the NDSolve. It should be: s = NDSolve[{m1'[z] == x[z]*(m1[z] - 1), m2'[z] == (m1[z]/y[z] - 1)*m2[z]*1/2*x[z], m1[0.01] == y[0.01], m2[0.01] == 10^-14}, {m1, m2}, {z, 0.01, 10}] . This takes a couple of minutes to run and gives m1,m2 interpolating functions and a warning about stiffness at z == 0.010002. – flinty Aug 02 '20 at 16:00
  • @flinty I'm not the original poster. Added all your changes. Still get the reported problem. Thanks for any help. (Also, do you think any problem defining function named x and using x within it as dummy variable? Also why should NumericQ be necessary? – PaulCommentary Aug 02 '20 at 16:34
  • 1
    You need to quit the kernel, or Remove["Global`*"] first because the original functions are still defined. – flinty Aug 02 '20 at 16:36
  • I think that worked, even when ClearAll[x,y] did not. – PaulCommentary Aug 02 '20 at 16:44
  • @flinty Thank you. I have changed the code as you mentioned . It is working perfectly with the warning as you mentioned. May I ask you what change is "?NumaricQ" making in my code? and if I have a multivariable function, let's say f[x_,y_], do I have to use ?NumaricQ every time? – Arghya Datta Aug 02 '20 at 16:45
  • 1
    @ArghyaDatta Yes it's a common mistake to forget it. Read here: https://mathematica.stackexchange.com/a/26037/72682 ?NumericQ is a pattern test that checks the argument is numeric. This is supposed to prevent functions like NDSolve, NIntegrate, NMinimize/NMaximize etc. from doing symbolic manipulation/preprocessing by changing evaluation order. See here too: https://support.wolfram.com/12502 - yes you need it for the multivariate case too. You can also use it to test higher rank objects https://mathematica.stackexchange.com/a/19600/72682 – flinty Aug 02 '20 at 16:50
  • This is covered in the "Possible Issues" section of the documentation for NIntegrate – Bob Hanlon Aug 02 '20 at 18:39
  • @BobHanlon I think the surprise and thus confusion (for me), is why is NDSolve "calling" NIntegrate with a symbolic parameter? – PaulCommentary Aug 02 '20 at 18:46

0 Answers0