4

I'm in a bit of a pickle:

I try to plot a graph of a simple function, and for some reason unknown to me, it is drawn blank even though evaluation of the function at specific points returns the required values. My code is as follows:

A = 0.1;
B = 0.11;
CParam[x_] :=  BesselK[2, A/x] BesselI[0, B/x] + BesselI[2, A/x] BesselK[0, B/x];
Plot[Log[Log[-CParam'[x]]], {x, 10^-8, 10^-5}]

However, typing {Log[Log[-CParam'[10^-5]]], Log[Log[-CParam'[10^-6]]], Log[Log[-CParam'[10^-7]]], Log[Log[-CParam'[10^-8]]]} returns {6.91618835667271168, 9.211416996515317434, 11.5130562034263446895, 13.81552593513862411174}.

Why doesn't Plot work properly?

Thanks

EZLearner
  • 295
  • 2
  • 6

2 Answers2

3

I think this is a bug in Plot[].

Aside from making a table and ListPlot-ing it, such as in my comment,

ListLinePlot@ Table[Log[Log[-CParam'[x]]], {x, Subdivide[10^-8, 10^-5, 100]}]

one could use NDSolve to get adaptive sampling:

ListLinePlot@
 NDSolveValue[{y[x] == Log[Log[-CParam'[x]]], z'[x] == 1, 
   z[10^-8] == 0}, y, {x, 10^-8, 10^-5}, PrecisionGoal -> 3]

Mathematica graphics


Adaptive sampling is not needed here, but in an oscillatory function, it could be helpful, should Plot[] fail as in this case.

Options[adaptiveListLinePlot] = Options[ListLinePlot];
adaptiveListLinePlot[f_, {x_, a_, b_}, opts : OptionsPattern[]] :=
  Module[{y, z, a1, b1},
   {a1, b1} =              (* open sampling *)
    Rescale[{Sqrt@$MachineEpsilon, 1 - Sqrt@$MachineEpsilon}, {0, 1}, {a, b}]; 
   ListLinePlot[
    NDSolveValue[{y[x] == f, z'[x] == 1, z[a1] == 0}, y, {x, a1, b1}, PrecisionGoal -> 3],
    opts]
   ];

adaptiveListLinePlot[Sin[x^2], {x, 0, 20}]

Mathematica graphics

Michael E2
  • 235,386
  • 17
  • 334
  • 747
1

I can verify the comment of @Michael E2 as I ran into the same problem with LogPlot on Version 11.0. The solution he suggested worked for me by extracting the calculation from inside Plot into Table. So for your problem it works like this:

A = 0.1;
B = 0.11;
CParam[x_] := BesselK[2, A/x] BesselI[0, B/x] + BesselI[2, A/x] BesselK[0, B/x];
stepIncrement = 10^(-8);
data = Table[Log[Log[-CParam'[x]]], {x, 10^-8, 10^-5, stepIncrement}];
ListPlot[data]

Choose the value of stepIncrement to a value that fulfills your requirements regarding precision.

TSwift
  • 121
  • 5