I need to plot an Interpolation function of one independent variable (only a 3kB file) on LogPlot and show the slope of its linear part together on the LogPlot, as manually drawn with a dashed red line in the following figure. But when I Show the LogPlot and a line with the slope of the linear part (see step 3), it turned out to be inconsistent.

  1. import data and plot:

    data = <<"...\\data.m";
    logPlot = LogPlot[data[t], {t, 0, 2}, PlotRange -> {{0, 2}, {0.01, 30000}}, Frame -> True]

enter image description here

  1. Observe the range of linear part and find a linear fit:

    lineardata = Table[Log[data[t]], {t, 1, 1.2, 0.02}];
    linearpart = Fit[lineardata, {1, t}, t];
    (*0.923913 + 0.689265 t*)
  2. Show together with the linearpart, it was found that the linearpart does not parallel to the linear part of the data on LogPlot.

    Show[logPlot, LogPlot[Exp[linearpart], {t, 1, 1.2}, PlotStyle -> {Red, Dashed}]]

What's wrong with this? Please help. I have updated the code according to the comments. Thanks for @Roman's suggestion.

  • $\begingroup$ Could you be more specific about what doesn't work in step 3 and/or include some sample data so that others can try it themselves? Maybe you need to use LogPlot for linearpart too? $\endgroup$ – Chris K Nov 10 '19 at 10:49
  • 2
    $\begingroup$ You have to LogPlot[Exp[linearpart], ...] $\endgroup$ – Roman Nov 10 '19 at 11:02
  • 1
    $\begingroup$ Your file is broken (after loading it, your code produces errors). Also, don't call two different things data (the interpolating function and the list of values). $\endgroup$ – Roman Nov 10 '19 at 13:19
  • $\begingroup$ Related, tho' a somewhat more complicated problem: mathematica.stackexchange.com/questions/57437/… $\endgroup$ – Michael E2 Nov 10 '19 at 17:17

Sorry, I didn't realize that you linked to the data in your post, but as @Roman noted, the InterpolatingFunction seems broken. Anyhow, I managed to extract enough to find another problem besides @Roman's comment to use LogPlot[Exp[linearpart], ...]. Specifically, your Table doesn't include x-coordinates, so Fit assumes they are 1, 2, 3, ... Instead try:

fdata = Table[{t, Log[data[t]]}, {t, 1, 1.2, 0.02}];
linearpart = Fit[fdata, {1, t}, t];
(* -34.7879 + 35.7638 t *)
 LogPlot[E^linearpart, {t, 1, 1.2}, PlotStyle -> {Red, Dashed}]]

Mathematica graphics

  • $\begingroup$ thank you for the suggestion. I have reuploaded the data and corrected the code according to Roman's suggestion. $\endgroup$ – Nobody Nov 10 '19 at 16:40
  • $\begingroup$ @Nobody I made a typo in the definition of fdata in my original answer. It's fixed now and should solve your problem. Sorry about that! $\endgroup$ – Chris K Nov 10 '19 at 16:46
  • 1
    $\begingroup$ I think LogPlot[E^linearpart, ... ] is equivalent to Plot[linearpart, ... ]. About this point, I guess Roman's 1st suggestion is redundant. $\endgroup$ – Nobody Nov 10 '19 at 16:51
  • $\begingroup$ @Nobody Yes, you're right, Plot[linearpart, ... ] also works. $\endgroup$ – Chris K Nov 10 '19 at 16:52

LogPlot plots $\log y$ vs. $x$, so the slope in the plot is given by $$m = {d \over dx} \log y = {dy/dx \over y} \,.$$

If you let f be your function, whether that is an InterpolatingFucntion[...][x] or some other function, the slope of the tangent to use in the LogPlot at x == x0 will be given by

D[f, x]/f /. x -> x0

Note that the tangent "line" represents the curve $\log y = \log y_0 + m (x - x_0)$, which is an exponential function. (For the code below, the tangent represents the function Exp[Log[f] + D[f, x]/f (t - x0)] /. x -> x0, as a function of t.)


 LogPlot[f, {x, 0, 2},
  Epilog -> {Red, 
    InfiniteLine[{x, Log@f} /. x -> x0, {1, D[f, x]/f /. x -> x0}]},
  PlotRange -> All, Frame -> True
 {{f, 0.1 + Exp[50 (x - 1) + 11]/(1 + Exp[50 (x - 1)])}, InputField},
 {{x0, 0.9}, 0, 2}

enter image description here


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.