Documentation Home | API Documentation | Examples | Index

Interpolation Example

Interpolation allows you to create data points inside of a defined range. It interpolates the data points, the interpolate_table function reassigns the independent variable to a user defined list and interpolates all the other values in data, returning a data table with the same format. It should be noted that this does not extrapolate, so if the new range is outside the old range it will fail.

In [1]:
from pymez import *
Importing pymez, this should take roughly 30 seconds
The module smithplot was not found,please put it on the python path
The module smithplot was not found,please put it on the python path
In [2]:
from pymez.Code.Analysis.Interpolation import *
In [9]:
# first we define a table where the independent x-axis is the first column
x_data=np.linspace(-10,10,10).tolist()
data_table=[[x,2.1*x,1.25*x**2] for x in x_data]
column_names=["x","line","second_order"]
data_table=AsciiDataTable(data=data_table,
                          column_names=column_names,
                          header=["A test of the data table"],
                         column_types=["float","float","float"])
In [10]:
print(data_table)
A test of the data table
x,line,second_order
-10.0,-21.0,125.0
-7.77777777778,-16.3333333333,75.6172839506
-5.55555555556,-11.6666666667,38.5802469136
-3.33333333333,-7.0,13.8888888889
-1.11111111111,-2.33333333333,1.54320987654
1.11111111111,2.33333333333,1.54320987654
3.33333333333,7.0,13.8888888889
5.55555555556,11.6666666667,38.5802469136
7.77777777778,16.3333333333,75.6172839506
10.0,21.0,125.0
In [15]:
# now we want a lot of data points in between the defined range
interpolated_table=interpolate_table(table=data_table,independent_variable_list=np.linspace(-5,5,50))
In [16]:
print(interpolated_table)
A test of the data table
x,line,second_order
-5.0,-10.5,32.4074074074
-4.79591836735,-10.0714285714,30.1398337113
-4.59183673469,-9.64285714286,27.8722600151
-4.38775510204,-9.21428571429,25.604686319
-4.18367346939,-8.78571428571,23.3371126228
-3.97959183673,-8.35714285714,21.0695389267
-3.77551020408,-7.92857142857,18.8019652305
-3.57142857143,-7.5,16.5343915344
-3.36734693878,-7.07142857143,14.2668178382
-3.16326530612,-6.64285714286,12.9440665155
-2.95918367347,-6.21428571429,11.8102796674
-2.75510204082,-5.78571428571,10.6764928193
-2.55102040816,-5.35714285714,9.54270597128
-2.34693877551,-4.92857142857,8.4089191232
-2.14285714286,-4.5,7.27513227513
-1.9387755102,-4.07142857143,6.14134542706
-1.73469387755,-3.64285714286,5.00755857899
-1.5306122449,-3.21428571429,3.87377173091
-1.32653061224,-2.78571428571,2.73998488284
-1.12244897959,-2.35714285714,1.60619803477
-0.918367346939,-1.92857142857,1.54320987654
-0.714285714286,-1.5,1.54320987654
-0.510204081633,-1.07142857143,1.54320987654
-0.30612244898,-0.642857142857,1.54320987654
-0.102040816327,-0.214285714286,1.54320987654
0.102040816327,0.214285714286,1.54320987654
0.30612244898,0.642857142857,1.54320987654
0.510204081633,1.07142857143,1.54320987654
0.714285714286,1.5,1.54320987654
0.918367346939,1.92857142857,1.54320987654
1.12244897959,2.35714285714,1.60619803477
1.32653061224,2.78571428571,2.73998488284
1.5306122449,3.21428571429,3.87377173091
1.73469387755,3.64285714286,5.00755857899
1.9387755102,4.07142857143,6.14134542706
2.14285714286,4.5,7.27513227513
2.34693877551,4.92857142857,8.4089191232
2.55102040816,5.35714285714,9.54270597128
2.75510204082,5.78571428571,10.6764928193
2.95918367347,6.21428571429,11.8102796674
3.16326530612,6.64285714286,12.9440665155
3.36734693878,7.07142857143,14.2668178382
3.57142857143,7.5,16.5343915344
3.77551020408,7.92857142857,18.8019652305
3.97959183673,8.35714285714,21.0695389267
4.18367346939,8.78571428571,23.3371126228
4.38775510204,9.21428571429,25.604686319
4.59183673469,9.64285714286,27.8722600151
4.79591836735,10.0714285714,30.1398337113
5.0,10.5,32.4074074074
In [19]:
%matplotlib inline
# for demonstation we can now plot to compare them
plt.plot(data_table["x"],data_table["line"],label="original")
plt.plot(interpolated_table["x"],interpolated_table["line"],label="interpolated")
plt.legend()
Out[19]:
<matplotlib.legend.Legend at 0x11cad7f0>
In [20]:
# The second order 
plt.plot(data_table["x"],data_table["second_order"],label="original")
plt.plot(interpolated_table["x"],interpolated_table["second_order"],label="interpolated")
plt.legend()
Out[20]:
<matplotlib.legend.Legend at 0x11e6af28>