1

I'm trying to interpolate using the cubic spline, but the interpolation fluctuates wildly around the data points. I don't know how to fix this. Any advice would be greatly appreciated.

enter image description here

TY = [90.3601709789,40.5189212844,20.3763456417,10.8899070251,6.19675122846,5.23402053792,6.05144428822,9.64276229024,24.1886072903,41.5725945652,171.923304843]
TX = [0.93984962406,0.90225563910,0.84586466165,0.75187969925,0.56390977444,0.37593984962,0.18796992481,0.07518796992,0.01879699248,0.00939849624,0.00187969925]

ipos = TX[0:6]
jpos = TY[0:6]

minpos = min(jpos)
maxpos = max(jpos)

ineg = TX[5:11]
jneg = TY[5:11]

minneg = min(jneg)
maxneg = max(jneg)

ypos1 = interp1d(jpos,ipos)
ypos2 = interp1d(jpos,ipos, kind='cubic')
xpos = np.linspace(minpos,maxpos,10000,endpoint=True)

yneg1 = interp1d(jneg,ineg)
yneg2 = interp1d(jneg,ineg, kind='cubic')
xneg = np.linspace(minneg,maxneg,10000,endpoint=True)

plt.figure('T')
plt.xscale('log')
plt.plot(TX,TY)
plt.plot(xpos,ypos2(xpos))
plt.plot(xneg,yneg2(xneg))

The positive and negative parts are because the data changes direction at the midpoint.

enter image description here

DeiDei
  • 10,205
  • 6
  • 55
  • 80
jasongold
  • 11
  • 1
  • It would be helpful if you provided a [Minimal, Complete and Verifiable Example](https://stackoverflow.com/help/mcve). Now we can only guess for which modules `np` and `plt` stand for, and where `interp1d` comes from. – gflegar May 16 '18 at 01:06
  • About the fluctuation - don't use a cubic spline if you don't want it. It looks to me like this is a perfectly valid result for the data you're giving it. If you're asking it to match the 2nd derivation at the interpolation points, that's what you're going to get, and it is going to fluctuate to accommodate that request. – gflegar May 16 '18 at 01:10

1 Answers1

1

Cubic splines are known to be prone to oscillations due to the requirement of the continuous second derivative. To avoid that, one way wound be to use instead C1 continuous splines. In the scipy land, look at PchpIntetpolator and/or Akima1DInterpolator

ev-br
  • 24,968
  • 9
  • 65
  • 78