3
$\begingroup$

I have this data:

    data4[100]={{99.98`, 1.477804748224006`}, {99.98010000000001`, 
   2365.0750456414803`}, {99.98020000000001`, 
   7388.983990539655`}, {99.9803`, 12214.415228382633`}, {99.9804`, 
   14703.728757320594`}, {99.9805`, 
   13833.517124715048`}, {99.98060000000001`, 
   9910.24843752318`}, {99.9807`, 4572.062808475352`}, {99.9808`, 
   570.8108232836022`}, {99.9809`, 
   1360.4394093799826`}, {99.98100000000001`, 
   10546.858200868768`}, {99.9811`, 31276.246551027016`}, {99.9812`, 
   65650.1254789103`}, {99.9813`, 
   114254.56416369356`}, {99.98140000000001`, 
   175877.45012692566`}, {99.9815`, 247463.43115736826`}, {99.9816`, 
   324324.10569403647`}, {99.9817`, 400585.6434020496`}, {99.9818`, 
   469822.1456480918`}, {99.9819`, 525795.5336175732`}, {99.982`, 
   563205.5837321972`}, {99.9821`, 578349.5929231215`}, {99.9822`, 
   569600.9007249621`}, {99.9823`, 537638.1255371793`}, {99.9824`, 
   485389.5506534906`}, {99.9825`, 417695.34242114425`}, {99.9826`, 
   340728.91596566234`}, {99.9827`, 261252.42488402053`}, {99.9828`, 
   185805.19866843504`}, {99.9829`, 119934.45681939927`}, {99.983`, 
   67573.04382586862`}, {99.98310000000001`, 
   30649.601122680797`}, {99.98320000000001`, 
   8984.93375823133`}, {99.98330000000001`, 
   488.57590847085385`}, {99.9834`, 1627.1205800512662`}, {99.9835`, 
   8096.737603320689`}, {99.98360000000001`, 
   15602.028881503466`}, {99.98370000000001`, 
   20626.473373518827`}, {99.9838`, 21078.87118582859`}, {99.9839`, 
   16715.90414629784`}, {99.98400000000001`, 
   9271.330417955402`}, {99.98410000000001`, 
   2263.4817347905678`}, {99.9842`, 499.00930637276855`}, {99.9843`, 
   9335.824909593328`}, {99.98440000000001`, 
   33805.42443036627`}, {99.98450000000001`, 
   77718.79959836493`}, {99.9846`, 142887.136511969`}, {99.9847`, 
   228577.00679417455`}, {99.9848`, 331290.87329430395`}, {99.9849`, 
   444921.04587067786`}, {99.985`, 561274.3394688623`}, {99.9851`, 
   670912.5821721719`}, {99.9852`, 764208.0275593415`}, {99.9853`, 
   832479.363953682`}, {99.9854`, 869058.3616487961`}, {99.9855`, 
   870142.0041714903`}, {99.9856`, 835310.0610613178`}, {99.9857`, 
   767630.5596346757`}, {99.9858`, 673329.9921983734`}, {99.9859`, 
   561064.1071001278`}, {99.986`, 
   440880.5960885422`}, {99.98610000000001`, 
   323009.09572123486`}, {99.98620000000001`, 
   216640.19370923933`}, {99.98630000000001`, 
   128859.40940812681`}, {99.9864`, 63883.29183825436`}, {99.9865`, 
   22704.86291029975`}, {99.98660000000001`, 
   3199.7311446716553`}, {99.98670000000001`, 
   679.7731520971815`}, {99.9868`, 8817.201777734488`}, {99.9869`, 
   20807.144107481454`}, {99.98700000000001`, 
   30599.608320206604`}, {99.98710000000001`, 
   34017.603224111605`}, {99.9872`, 29589.91591126962`}, {99.9873`, 
   18963.736616762995`}, {99.98740000000001`, 
   6819.466464542636`}, {99.98750000000001`, 
   280.1958293620077`}, {99.9876`, 7881.952610961169`}, {99.9877`, 
   38237.52969319436`}, {99.98780000000001`, 
   98576.85395021607`}, {99.9879`, 193372.72190030565`}, {99.988`, 
   323257.87767462275`}, {99.9881`, 484407.14150474017`}, {99.9882`, 
   668500.2152456209`}, {99.9883`, 863304.0960842049`}, {99.9884`, 
   1.0538288292973272`*^6}, {99.9885`, 
   1.223928369892497`*^6}, {99.9886`, 
   1.358151069349844`*^6}, {99.9887`, 
   1.4436018537197143`*^6}, {99.9888`, 
   1.471567167555936`*^6}, {99.9889`, 
   1.4386770107031108`*^6}, {99.989`, 
   1.3474337251571033`*^6}, {99.98910000000001`, 
   1.206018054152098`*^6}, {99.98920000000001`, 
   1.0273789719875318`*^6}, {99.98930000000001`, 
   827711.9871653258`}, {99.9894`, 624517.4470547661`}, {99.9895`, 
   434493.3112763912`}, {99.98960000000001`, 
   271546.2668802631`}, {99.98970000000001`, 
   145195.83424830047`}, {99.9898`, 59597.975846536276`}, {99.9899`, 
   13333.424807004074`}, {99.99000000000001`, 
   1.4778548374239222`}, {99.99010000000001`, 
   9546.365930991567`}, {99.9902`, 30136.213755790428`}, {99.9903`, 
   50329.26450484372`}, {99.99040000000001`, 
   61211.43650049187`}, {99.99050000000001`, 
   58181.79375774372`}, {99.9906`, 42100.992643598314`}, {99.9907`, 
   19598.506481051947`}, {99.99080000000001`, 
   2447.884102815886`}, {99.9909`, 6050.762474493852`}, {99.991`, 
   47201.991349865326`}, {99.9911`, 141421.66859646593`}, {99.9912`, 
   300218.0575426925`}, {99.9913`, 528675.9641149845`}, {99.9914`, 
   823741.4573664954`}, {99.9915`, 1.1734966999925314`*^6}, {99.9916`,
    1.5575965311706052`*^6}, {99.9917`, 
   1.9488858632768858`*^6}, {99.9918`, 
   2.3160542286129966`*^6}, {99.9919`, 
   2.6270326145706433`*^6}, {99.992`, 
   2.852719932349058`*^6}, {99.99210000000001`, 
   2.970559905168779`*^6}, {99.99220000000001`, 
   2.9674852618802455`*^6}, {99.99230000000001`, 
   2.841809004691841`*^6}, {99.9924`, 
   2.6037657460238747`*^6}, {99.9925`, 
   2.2745765447930614`*^6}, {99.99260000000001`, 
   1.8841060333862316`*^6}, {99.99270000000001`, 
   1.4673760546494084`*^6}, {99.9928`, 
   1.060367506635195`*^6}, {99.9929`, 
   695658.25368346`}, {99.99300000000001`, 
   398489.99935574655`}, {99.99310000000001`, 
   183822.56119456282`}, {99.9932`, 54819.36240358618`}, {99.9933`, 
   3026.5354262167316`}, {99.99340000000001`, 
   10280.101486167212`}, {99.99350000000001`, 
   52131.584079556276`}, {99.9936`, 102354.42420546012`}, {99.9937`, 
   137914.54566184452`}, {99.99380000000001`, 
   143685.80534156234`}, {99.9939`, 116183.81624423101`}, {99.994`, 
   65685.81312699754`}, {99.9941`, 16293.259464495655`}, {99.9942`, 
   3757.7396592063697`}, {99.9943`, 71196.78022114829`}, {99.9944`, 
   263136.03241681046`}, {99.9945`, 618585.1966204251`}, {99.9946`, 
   1.1640471696468873`*^6}, {99.9947`, 
   1.9074421621634928`*^6}, {99.9948`, 
   2.833882410239052`*^6}, {99.9949`, 3.90405567313237`*^6}, {99.995`,
    5.055682072659491`*^6}, {99.99510000000001`, 
   6.208131261346862`*^6}, {99.99520000000001`, 
   7.269868799615699`*^6}, {99.99530000000001`, 
   8.1479962141799`*^6}, {99.9954`, 8.758813651212059`*^6}, {99.9955`,
    9.038114802770944`*^6}, {99.99560000000001`, 
   8.949861198259037`*^6}, {99.99570000000001`, 
   8.491995143812856`*^6}, {99.9958`, 
   7.6984334207765255`*^6}, {99.9959`, 
   6.636718223481769`*^6}, {99.99600000000001`, 
   5.401335544570164`*^6}, {99.99610000000001`, 
   4.1032876411022553`*^6}, {99.9962`, 
   2.8570501793411407`*^6}, {99.9963`, 
   1.7664844156866106`*^6}, {99.99640000000001`, 
   911546.0560149102`}, {99.99650000000001`, 
   337687.72504541057`}, {99.9966`, 49662.31616212789`}, {99.9967`, 
   11014.220485333051`}, {99.99680000000001`, 
   149916.70067926153`}, {99.9969`, 371249.42592296394`}, {99.997`, 
   573985.0440702039`}, {99.9971`, 672172.6613239779`}, {99.9972`, 
   617162.7093132898`}, {99.9973`, 418298.5610825093`}, {99.9974`, 
   159190.11126375137`}, {99.9975`, 6889.055099344037`}, {99.9976`, 
   211857.28903701573`}, {99.9977`, 1.097445484728616`*^6}, {99.9978`,
    3.038700743165044`*^6}, {99.9979`, 
   6.431489307696678`*^6}, {99.998`, 
   1.1654127256214151`*^7}, {99.99810000000001`, 
   1.9024725313095316`*^7}, {99.99820000000001`, 
   2.875825988533397`*^7}, {99.99830000000001`, 
   4.092775280583405`*^7}, {99.9984`, 
   5.5433955481785744`*^7}, {99.9985`, 
   7.198736932275228`*^7}, {99.99860000000001`, 
   9.010559231046714`*^7}, {99.99870000000001`, 
   1.0912756879132845`*^8}, {99.9988`, 
   1.2824493566283156`*^8}, {99.9989`, 
   1.4654876982895207`*^8}, {99.99900000000001`, 
   1.630887950398025`*^8}, {99.99910000000001`, 
   1.7694038798517886`*^8}, {99.9992`, 
   1.8727407690049762`*^8}, {99.9993`, 
   1.934212083241554`*^8}, {99.99940000000001`, 
   1.9493079448759955`*^8}, {99.99950000000001`, 
   1.9161105149283525`*^8}, {99.9996`, 
   1.8355199261690336`*^8}, {99.9997`, 
   1.7112742180357695`*^8}, {99.99980000000001`, 
   1.5497651606295362`*^8}, {99.9999`, 
   1.3598374683380157`*^8}, {100.`, 
   1.1509227841822496`*^8}, {100.0001`, 
   9.35316855233305`*^7}, {100.0002`, 
   7.231025004379873`*^7}, {100.0003`, 
   5.256660434840215`*^7}, {100.0004`, 
   3.521381099545413`*^7}, {100.0005`, 
   2.096762182207146`*^7}, {100.0006`, 
   1.0307175273951748`*^7}, {100.0007`, 
   3.443687074219937`*^6}, {100.0008`, 
   319172.0909938909`}, {100.0009`, 624886.4691508369`}, {100.001`, 
   3.839629652685036`*^6}, {100.00110000000001`, 
   9.282792159957027`*^6}, {100.00120000000001`, 
   1.617690363214594`*^7}, {100.00130000000001`, 
   2.371457960763766`*^7}, {100.0014`, 
   3.1123276998223003`*^7}, {100.00150000000001`, 
   3.772278285822957`*^7}, {100.00160000000001`, 
   4.297077901877572`*^7}, {100.00170000000001`, 
   4.649332297408372`*^7}, {100.0018`, 
   4.809858468949901`*^7}, {100.0019`, 
   4.777386804395814`*^7}, {100.00200000000001`, 
   4.566746227227442`*^7}, {100.00210000000001`, 
   4.205818439052258`*^7}, {100.0022`, 
   3.731643524344679`*^7}, {100.0023`, 
   3.186112453043561`*^7}, {100.00240000000001`, 
   2.611684639639511`*^7}, {100.00250000000001`, 
   2.0475351093277786`*^7}, {100.0026`, 
   1.526456928610898`*^7}, {100.0027`, 
   1.0727432825096259`*^7}, {100.00280000000001`, 
   7.01155356380699`*^6}, {100.0029`, 
   4.1696218231500047`*^6}, {100.003`, 
   2.1693002162992805`*^6}, {100.0031`, 
   910514.5775877895`}, {100.0032`, 247468.92969395986`}, {100.0033`, 
   12466.399693349336`}, {100.0034`, 38713.52052913366`}, {100.0035`, 
   179679.7590042069`}, {100.0036`, 323213.6387249924`}, {100.0037`, 
   399394.1422363143`}, {100.0038`, 381919.9691393901`}, {100.0039`, 
   283614.326160101`}, {100.004`, 
   147264.125970723`}, {100.00410000000001`, 
   33457.5539198235`}, {100.00420000000001`, 
   7288.615010277445`}, {100.00430000000001`, 
   125771.23057228878`}, {100.0044`, 
   427548.6798790092`}, {100.00450000000001`, 
   926064.5824913883`}, {100.00460000000001`, 
   1.6068273548210126`*^6}, {100.00470000000001`, 
   2.428830198016985`*^6}, {100.0048`, 
   3.3296556940659885`*^6}, {100.0049`, 
   4.233355835687827`*^6}, {100.00500000000001`, 
   5.059908642966373`*^6}, {100.00510000000001`, 
   5.734928561736933`*^6}, {100.0052`, 
   6.198363502951681`*^6}, {100.0053`, 
   6.411118533755151`*^6}, {100.00540000000001`, 
   6.35887575814467`*^6}, {100.00550000000001`, 
   6.052779744495396`*^6}, {100.0056`, 
   5.527073307789168`*^6}, {100.0057`, 
   4.834148684727312`*^6}, {100.00580000000001`, 
   4.037775067693357`*^6}, {100.0059`, 
   3.2054465135499393`*^6}, {100.006`, 
   2.400845094917662`*^6}, {100.0061`, 
   1.6773369586025865`*^6}, {100.0062`, 
   1.0732294466178913`*^6}, {100.0063`, 
   609246.9462916934`}, {100.0064`, 288371.9280090763`}, {100.0065`, 
   97885.98961812796`}, {100.0066`, 13175.954352500674`}, {100.0067`, 
   2671.8581152992074`}, {100.0068`, 33180.8081956854`}, {100.0069`, 
   74879.15044292489`}, {100.007`, 
   105320.26006151935`}, {100.00710000000001`, 
   111989.1796066404`}, {100.00720000000001`, 
   93159.75531063265`}, {100.00730000000001`, 
   57054.633435510834`}, {100.0074`, 
   19538.03841524044`}, {100.00750000000001`, 
   758.3242056625403`}, {100.00760000000001`, 
   21277.671150084952`}, {100.00770000000001`, 
   98268.79276605384`}, {100.0078`, 242320.59325159696`}, {100.0079`, 
   455285.73967379465`}, {100.00800000000001`, 
   729441.1594492072`}, {100.00810000000001`, 
   1.0480410677191823`*^6}, {100.0082`, 
   1.3871489717460165`*^6}, {100.0083`, 
   1.7184649917589629`*^6}, {100.00840000000001`, 
   2.012740522166382`*^6}, {100.00850000000001`, 
   2.2433073813179927`*^6}, {100.0086`, 
   2.3892498069632286`*^6}, {100.0087`, 
   2.437811768754389`*^6}, {100.00880000000001`, 
   2.385747887226385`*^6}, {100.0089`, 
   2.239476243883074`*^6}, {100.009`, 
   2.0140542725085174`*^6}, {100.0091`, 
   1.7311522326453237`*^6}, {100.0092`, 
   1.4163228918969373`*^6}, {100.0093`, 
   1.095945353432984`*^6}, {100.0094`, 
   794247.1192294351`}, {100.0095`, 530779.7536792547`}, {100.0096`, 
   318646.5975802199`}, {100.0097`, 163667.62190115865`}, {100.0098`, 
   64533.7233387026`}, {100.0099`, 13868.84376476787`}, {100.01`, 
   1.4778114216281888`}}]

I can interpolate the data with

Clear[Interpolate]
Interpolate[100] = Interpolation[data4[100], InterpolationOrder -> 2];

However, I noticed that Interpolation[] gives negative values in a small domain of my interpolation:

Plot[Interpolate[100][x], {x, 99.98, 100.01}, PlotRange -> All]

enter image description here

Plot[Interpolate[100][x], {x, 100.0033, 100.0034}, 
 PlotRange -> {-2*10^4, 0}]

enter image description here

Why is Interpolation[] giving a negative value when none of the data gives negative values? I had looked at this question, which suggested adding InterpolationOrder, but unfortunately this solution had no effect.

$\endgroup$
2

3 Answers 3

14
$\begingroup$

One good way to interpolate a function of this nature is to take its Log, interpolate, and take Exp of the result.

lntrp = Interpolation[data4[100] /. {x_, y_} -> {x, Log[y]}]
Plot[Exp[lntrp[x]], {x, 99.98, 100.01}, PlotRange -> All]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ This is a very good answer. $\endgroup$
    – sirhans
    Commented May 20, 2020 at 8:37
2
$\begingroup$

Too long for a comment to John Doty's answer:

While the answer might suit your needs (i.e. a smooth curve that passes through your points and remains positive), I would not say that it is a good way of interpolating.

In the example below, I illustrate two bad properties of taking the exponential of log-interpolated values:

  • sensitivity to vertical translation (the interpolation is sensitive to a vertical translation)
  • sensitivity to small absolute variations near 0

Naturally, these two "properties" are related.

 data1 = {{0, 1}, {1, 10^-3}, {2, 1}};
 data2 = {{0, 1}, {1, 0}, {2, 1}} /. {x_, y_} -> {x, y + 1};
 data3 = {{0, 1}, {1, 10^-5}, {2, 1}};

One would expect interpolation of these three datasets to have quasi-exact same shapes. It is the case with "classic" interpolation:

int1 = Interpolation[data1, InterpolationOrder -> 2];
int2 = Interpolation[data2, InterpolationOrder -> 2];
int3 = Interpolation[data3, InterpolationOrder -> 2];
Plot[{int1[t], int2[t] - 1, int3[t]}, {t, 0, 2}]

enter image description here

But not at all with the "Log-Exp trick":

int4 = Interpolation[data1 /. {x_, y_} -> {x, Log[y]}, InterpolationOrder -> 2];
int5 = Interpolation[data2 /. {x_, y_} -> {x, Log[y]}, InterpolationOrder -> 2];
int6 = Interpolation[data3 /. {x_, y_} -> {x, Log[y]}, InterpolationOrder -> 2];
Plot[{Exp@int4[t], -1 + Exp@int5[t], Exp@int6[t]}, {t, 0, 2}]

enter image description here

So maybe it is good enough for you in this case, but I would not recommend it generally speaking (and I would interrogate myself on the grounds to create an interpolation method that works only on a limited domain - positive values).

$\endgroup$
3
  • $\begingroup$ However, the Log-Exp pair can be replaced with any other functional pair such that $f(g(x))=x$ and $f(x)>0$ for domain of interest. For instance g[x]=Sqrt[x] and f[x]=x^2. $\endgroup$
    – yarchik
    Commented Oct 30, 2019 at 10:41
  • $\begingroup$ @yarchik Of course, but with the same drawback more or less amplified (based on the slope of $g$). Bottom line: what is the missing information that is sought between the known points? If the purpose of interpolating is to take the derivative, that could be a very bad strategy. $\endgroup$
    – anderstood
    Commented Oct 30, 2019 at 12:10
  • 1
    $\begingroup$ The problem is intrinsically unsymmetrical: zero is a line that must not be crossed, but there is no upper limit. Looking at the function, the peaks are rounded, but the valleys are nearly flat. The Log method reflects this, while you seem to insist that a proper method should not. $\endgroup$
    – John Doty
    Commented Oct 30, 2019 at 19:25
1
$\begingroup$

Linear interpolation doesn't show this over/under-swinging behavior of polynomial interpolation of higher order.

Clear[Interpolate]
Interpolate[100] = Interpolation[data4[100], InterpolationOrder -> 1];


Plot[Interpolate[100][x], {x, 100.0033, 100.0034}, PlotRange -> {-2*10^4, +10*10^4}]

enter image description here

Alternatively, if you don't want to use linear interpolation you can use Akima Interpolation which gives very smooth results with little over/undershooting.

f = ResourceFunction["AkimaInterpolation"][data4[100]];
Plot[f[x], {x, 99.98, 100.01}, PlotRange -> All]

enter image description here

Plot[Interpolate[100][x], {x, 100.0033, 100.0034},  PlotRange -> {-2*10^4, +10*10^4}]

enter image description here

$\endgroup$
1
  • $\begingroup$ +1 for AkimaInterpolation - I saw the author of that function give a talk on it a couple of days ago. Much more satisfying than the accepted answer. $\endgroup$
    – Jason B.
    Commented Oct 31, 2019 at 13:22

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