Reputation: 15
I'm converting code from Matlab to Python 2.7 and am having a problem with the conversion of the interp1 function. I have looked at similar questions already posted but have not yet managed to solve it. The problem is that the first value of the vector of newly generated values (yn) is different while the rest are almost identical. I get slightly different values but the same problem when using different interpolation methods. At the moment I really have no clue why this is. Does anyone have any insight into this or see any errors I might have made? Thanks.
Variables:
x = [5.5 , 5.46678 , 5.408315, 5.33929 , 5.261025, 5.17605 ,
5.08684 , 4.995375, 4.902755, 4.80942 , 4.7157 , 4.621815,
4.52782 , 4.433715, 4.339555, 4.245395, 4.151235, 4.05713 ,
3.962915, 3.868645, 3.77432 , 3.680105, 3.585945, 3.491895,
3.397845, 3.303905, 3.21002 , 3.11619 , 3.02247 , 2.928805,
2.835195, 2.741695, 2.64836 , 2.55519 , 2.462295, 2.36951 ,
2.27689 , 2.184435, 2.092255, 2.00035 , 1.908775, 1.817475,
1.726505, 1.63592 , 1.54583 , 1.4564 , 1.36752 , 1.279245,
1.19163 , 1.10539 , 1.02135 , 0.94006 , 0.86163 , 0.786115,
0.713515, 0.64394 , 0.577555, 0.514635, 0.45562 , 0.400785,
0.35035 , 0.304425, 0.26301 , 0.22627 , 0.193875, 0.16544 ,
0.14058 , 0.118745, 0.09944 , 0.082335, 0.067265, 0.05401 ,
0.042405, 0.03234 , 0.023595, 0.01628 , 0.010175, 0.00528 ,
0.001485, -0.00121 , -0.002805, -0.003355, -0.00275 , -0.000935]
y = [0.19896, 0.18722, 0.155 , 0.13133, 0.11168, 0.09543,
0.0815 , 0.06556, 0.04191, 0.0117 , 0.00513, 0.00123,
-0.0036 , -0.00885, -0.01429, -0.01985, -0.02532, -0.03065,
-0.03574, -0.04082, -0.04594, -0.05104, -0.05596, -0.06091,
-0.06561, -0.07023, -0.07482, -0.07913, -0.08341, -0.08749,
-0.09155, -0.09551, -0.09952, -0.10334, -0.10694, -0.11011,
-0.11319, -0.11587, -0.11856, -0.12092, -0.12277, -0.12428,
-0.12506, -0.12567, -0.12567, -0.12497, -0.12369, -0.12135,
-0.11944, -0.1191 , -0.11983, -0.11819, -0.11197, -0.10004,
-0.08016, -0.05285, -0.01569, 0.03055, 0.08527, 0.1492 ,
0.21971, 0.29507, 0.37453, 0.45682, 0.53766, 0.61562,
0.6916 , 0.763 , 0.82907, 0.88665, 0.9367 , 0.97418,
0.99617, 0.99807, 0.97457, 0.91708, 0.81796, 0.66987,
0.46359, 0.19778, -0.13378, -0.54232, -1.0126 , -1.5297 ]
xn = [ 0., 0.61111111, 1.22222222, 1.83333333, 2.44444444,
3.05555556, 3.66666667, 4.27777778, 4.88888889, 5.5 ]
Matlab code:
yn_mat = interp1(x,y,xn,'linear','extrap')
Python code:
from scipy.interpolate import InterpolatedUnivariateSpline
yn_f1 = InterpolatedUnivariateSpline(x[::-1], y[::-1])
yn_py1 = yn_f1(xn)
from scipy.interpolate import interp1d
yn_f2 = interp1d(x[::-1], y[::-1])
yn_py2 = yn_f2(xn)
import numpy as np
yn_py3 = np.interp(xn, x[::-1], y[::-1])
Results:
yn_mat = [-0.7596, -0.0345, -0.1201, -0.1240, -0.1075,
-0.0819, -0.0517, -0.0179, 0.0374, 0.1990 ]
yn_py1 = [-0.23310355, -0.03594415, -0.11996893, -0.12406894, -0.10757466,
-0.08191329, -0.05174936, -0.01793778, 0.0371338 , 0.19896 ]
yn_py2 = [ 0.31712327, -0.03447354, -0.12010691, -0.12401772, -0.10754986,
-0.08189905, -0.05174217, -0.01793785, 0.03742192, 0.19896 ]
yn_py3 = [ 0.31712327, -0.03447354, -0.12010691, -0.12401772, -0.10754986,
-0.08189905, -0.05174217, -0.01793785, 0.03742192, 0.19896 ]
Upvotes: 1
Views: 454