Question

I wrote IDL code:

zz= [  0,  5, 10, 15, 30, 50, 90, 100,  500]
uz= [ 20, 20, 20, 30, 60, 90, 30, -200, -200]*(-1.)
zp= findgen(120)*500+500
up= spline((zz-10.),uz,(zp/1000.0))
print, up 

and IDL gave me the values of up array from about -20 to 500

.The same I did in Python

import numpy as npy
zz = npy.array([  0,  5, 10, 15, 30, 50, 90, 100,  500])
uz = npy.array([ 20, 20, 20, 30, 60, 90, 30, -200, -200])*(-1.)
zp = npy.arange(0,120)*500+500
from scipy.interpolate import interp1d
cubic_interp_u = interp1d(zz-10., uz, kind='cubic')
up = cubic_interp_u(zp/1000)
print up

and it gave me up with values from about -20 to -160. Any idea? Thanks in advance!

Was it helpful?

Solution

Actually, I don't see a problem. I'm using UnivariateSpline here instead of interp1d and cubic_interp_u, but the underlying routines are essentially the same, as far as I can tell:

import numpy as npy
import pyplot as pl
from scipy.interpolate import UnivariateSpline
zz = npy.array([  0,  5, 10, 15, 30, 50, 90, 100,  500])
uz = npy.array([ 20, 20, 20, 30, 60, 90, 30, -200, -200])*(-1.)
zp = npy.arange(0,120)*500+500
pl.plot(zz, uz, 'ro')
pl.plot(zp/100, UnivariateSpline(zz, uz, s=1, k=3)(zp/100), 'k-.')
pl.plot(zp/1000, UnivariateSpline(zz, uz, s=1, k=3)(zp/1000), 'b-')

The only problem I see is that you limited the interpolation, by using zp/1000. Using zp/100, I get all lots of values outside that -160, -20 range, which you can also see on the graph from the dot-dashed line, compared to the blue line (zp/1000):

enter image description here

It looks like scipy is doing a fine job.

By the way, if you want to (spline-)fit such outlying values, you may want to consider working in log-log space instead, or roughly normalizing your data (log-log space kind-of does that). Most fitting problems work best if the values are in the same order of magnitude.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top