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
Importing Code.DataHandlers.GeneralModels
It took 0.374037 s to import Code.DataHandlers.GeneralModels
Importing Code.DataHandlers.HTMLModels
It took 0.081008 s to import Code.DataHandlers.HTMLModels
Importing Code.DataHandlers.NISTModels
The module smithplot was not found,please put it on the python path
It took 0.442044 s to import Code.DataHandlers.NISTModels
Importing Code.DataHandlers.TouchstoneModels
It took 0.001 s to import Code.DataHandlers.TouchstoneModels
Importing Code.DataHandlers.XMLModels
It took 0.043005 s to import Code.DataHandlers.XMLModels
Importing Code.DataHandlers.ZipModels
It took 0.004 s to import Code.DataHandlers.ZipModels
Importing Code.InstrumentControl.Experiments
It took 2.131213 s to import Code.InstrumentControl.Experiments
Importing Code.InstrumentControl.Instruments
It took 0.002 s to import Code.InstrumentControl.Instruments
Importing Code.Utils.Names
It took 0.003001 s to import Code.Utils.Names
It took 3.082308 s to import all of the active modules
In [2]:
from pyMez.Code.Analysis.Interpolation import *
In [3]:
# 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 [4]:
print(data_table)
A test of the data table
x,line,second_order
-10.0,-21.0,125.0
-7.777777777777778,-16.333333333333332,75.61728395061728
-5.555555555555555,-11.666666666666666,38.58024691358025
-3.333333333333333,-7.0,13.888888888888886
-1.1111111111111107,-2.3333333333333326,1.5432098765432087
1.1111111111111107,2.3333333333333326,1.5432098765432087
3.333333333333334,7.000000000000002,13.888888888888893
5.555555555555557,11.66666666666667,38.58024691358027
7.777777777777779,16.333333333333336,75.61728395061729
10.0,21.0,125.0
In [5]:
# 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 [6]:
print(interpolated_table)
A test of the data table
x,line,second_order
-5.0,-10.5,32.407407407407405
-4.795918367346939,-10.071428571428573,30.139833711262284
-4.591836734693878,-9.642857142857144,27.872260015117163
-4.387755102040816,-9.214285714285714,25.604686318972032
-4.183673469387755,-8.785714285714286,23.337112622826908
-3.979591836734694,-8.357142857142858,21.069538926681787
-3.7755102040816326,-7.928571428571429,18.80196523053666
-3.571428571428571,-7.5,16.53439153439153
-3.36734693877551,-7.071428571428572,14.266817838246407
-3.163265306122449,-6.642857142857144,12.944066515495086
-2.9591836734693877,-6.214285714285714,11.810279667422524
-2.7551020408163263,-5.785714285714286,10.67649281934996
-2.5510204081632653,-5.357142857142858,9.5427059712774
-2.3469387755102042,-4.928571428571429,8.40891912320484
-2.142857142857143,-4.5,7.2751322751322745
-1.9387755102040813,-4.071428571428571,6.141345427059711
-1.7346938775510203,-3.6428571428571423,5.007558578987151
-1.5306122448979593,-3.2142857142857144,3.873771730914589
-1.3265306122448979,-2.7857142857142847,2.739984882842027
-1.1224489795918364,-2.357142857142856,1.6061980347694629
-0.9183673469387754,-1.9285714285714284,1.5432098765432087
-0.7142857142857144,-1.5000000000000004,1.5432098765432087
-0.5102040816326525,-1.0714285714285703,1.5432098765432087
-0.3061224489795915,-0.6428571428571421,1.5432098765432087
-0.1020408163265305,-0.21428571428571397,1.5432098765432087
0.1020408163265305,0.21428571428571397,1.5432098765432087
0.3061224489795915,0.6428571428571423,1.5432098765432087
0.5102040816326534,1.071428571428572,1.5432098765432087
0.7142857142857144,1.5000000000000004,1.5432098765432087
0.9183673469387754,1.9285714285714284,1.5432098765432087
1.1224489795918373,2.3571428571428585,1.6061980347694675
1.3265306122448983,2.7857142857142865,2.7399848828420286
1.5306122448979593,3.214285714285715,3.87377173091459
1.7346938775510203,3.6428571428571432,5.007558578987151
1.9387755102040813,4.071428571428571,6.141345427059712
2.1428571428571432,4.500000000000002,7.275132275132278
2.3469387755102042,4.92857142857143,8.40891912320484
2.5510204081632653,5.357142857142858,9.5427059712774
2.755102040816327,5.785714285714288,10.676492819349967
2.959183673469388,6.214285714285716,11.810279667422527
3.163265306122449,6.642857142857144,12.944066515495088
3.36734693877551,7.071428571428572,14.266817838246407
3.571428571428571,7.5,16.53439153439153
3.775510204081632,7.928571428571428,18.801965230536652
3.979591836734695,8.35714285714286,21.069538926681794
4.183673469387756,8.785714285714288,23.33711262282692
4.387755102040817,9.214285714285715,25.604686318972043
4.591836734693878,9.642857142857144,27.872260015117163
4.795918367346939,10.071428571428573,30.139833711262288
5.0,10.5,32.407407407407405
In [7]:
%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[7]:
<matplotlib.legend.Legend at 0x941c2e8>
In [8]:
# 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[8]:
<matplotlib.legend.Legend at 0x949f2e8>