P Ved
P Ved

Reputation: 139

Scipy Interpolate splrep Data Error?

I am writing the following code, pretty basic , just calling splrep in scipy.interpolate. However it throws and error on the dataset.

Following is the code

x_nodesTrial = np.array([ 1321.,  1327.,  1329.,  1326.,  1329.,  1327.,  1328.,  1336.,
        1335.,  1326.,  1327.,  1323.,  1337.,  1338.,  1337.,  1338.,
        1338.,  1338.,  1340.,  1340.,  1337.,  1334.,  1333.,  1342.,
        1343.,  1340.,  1344.,  1343.,  1346.,  1347.,  1348.,  1346.,
        1374.,  1377.,  1372.,  1378.,  1380.,  1384.,  1390.,  1391.,
        1386.,  1397.,  1395.,  1396.,  1397.,  1398.,  1399.,  1399.,
        1399.])

y_nodesTrial = np.array([ 0.97897541,  0.60222801,  0.95832816,  1.2578326 ,  1.08685326,
        1.29256113,  0.78091064,  0.96806139,  0.92957363,  0.46117087,
        0.72456884,  1.04872371,  0.87137002,  1.03914457,  0.74450367,
        0.96806139,  0.93808315,  0.79181076,  0.84673744,  1.15069855,
        0.8782084 ,  1.14509825,  0.97970477,  0.91182705,  1.12178111,
        1.07171425,  0.58492979,  0.89831191,  1.17473401,  0.97970477,
        0.87013956,  1.1568893 ,  1.01242284,  0.7309485 ,  1.00418766,
        1.04394513,  1.08947891,  0.81053333,  1.21184629,  0.94566681,
        0.73630351,  0.86839343,  1.07495847,  0.87003695,  0.        ,
        1.01559272,  0.97504579,  1.0377689 ,  1.05694979])

k = 3
tck = inter.splrep(x_nodesTrial, y_nodesTrial, k=k, s=0)

I get the following error:

k = 3
tck = inter.splrep(x_nodesTrial, y_nodesTrial, k=k, s=0)

Traceback:

Traceback (most recent call last):

  File "<ipython-input-24-a978d88e9773>", line 2, in <module>
    tck = inter.splrep(x_nodesTrial, y_nodesTrial, k=k, s=0)

  File "C:\Anaconda\lib\site-packages\scipy\interpolate\fitpack.py", line 278, in splrep
    res = _impl.splrep(x, y, w, xb, xe, k, task, s, t, full_output, per, quiet)

  File "C:\Anaconda\lib\site-packages\scipy\interpolate\_fitpack_impl.py", line 515, in splrep
    raise _iermess[ier][1](_iermess[ier][0])

ValueError: Error on input data

Can someone help point out what is wrong with the above code/dataset ?

Appreciate the help.

Thanks

Upvotes: 6

Views: 12038

Answers (1)

James Clark
James Clark

Reputation: 161

It's because your x-values aren't sorted from smallest to biggest

Upvotes: 16

Related Questions