Contents Previous Next Subchapters Current Chapters-> interp interp1 lagrange polyfit smospl cubespl cubeval interp2 mlmode_interp2 snewton brent linlsq linlsqb nlsq dnlsq nlsqbox dnlsqb conjdir neldermead conjgrad minline lemke Qpos Qbox Qpro sqp relative fordiff cendiff autodiff testder testgrad testhess Parent Chapters-> Omatrix6 fit interp Search Tools-> contents reference index search

Piecewise Linear Interpolation In One Dimension
 Syntax `interp(`new_grid`, `old_table`)` `interp(`new_grid`, `old_table`, `ind`)` See Also interp1 , interp2 , lagrange , cubespl

Description
Interpolates multiple variables onto a new grid of values. (The function interp1 can handle the complex case with only one variable.) The argument old_table is a real or double-precision matrix and each column corresponds to one of the variables on the old grid. The argument new_grid is a real or double-precision column vector that contains the new grid of values for the independent variable. The argument ind is an integer scalar between one and the column dimension of old_table that specifies the column corresponding to the independent variable in both table_old and the return value. If ind is not present, the first column is used for the independent variable. The return value has the same row dimension as new_grid and the same column dimension as old_table. The type of the return value is real, if both arguments are real, and double-precision otherwise. ``` ```Both the new and old grid of independent variables values (both new_grid and the independent variable column of old_table) must be strictly monotone increasing .

Example
The `interp` function uses a linear approximation between independent variable values in the old grid. For instance, suppose you recorded the following temperature data:
 time temperature 1:00 65 2:00 68 3:00 72 4:00 73 5:00 73
To interpolate the temperature at half-hour intervals, enter ```      old_table = { ...                [1., 65.], ...                [2., 68.], ...                [3., 72.], ...                [4., 73.], ...                [5., 73.]  ...      }      new_grid  = {1., 1.5, 2., 2.5, 3., 3.5, 4., 4.5, 5.}      interp(new_grid, old_table) ``` to which O-Matrix will respond ```      {      [ 1 , 65 ]      [ 1.5 , 66.5 ]      [ 2 , 68 ]      [ 2.5 , 70 ]      [ 3 , 72 ]      [ 3.5 , 72.5 ]      [ 4 , 73 ]      [ 4.5 , 73 ]      [ 5 , 73 ]      } ``` The first column in the matrix above is the time at half-hour intervals, and the second column is the interpolated values for temperature. ``` ```The `interp` function will extrapolate the linear approximation if an new grid value falls outside the range of old grid. If you continue the previous example by entering ```      new_grid = {.5, 5.5}      interp(new_grid, old_table) ``` O-Matrix will respond ```      [ 0.5 , 63.5 ]      [ 5.5 , 73 ] ``` This approximates the temperature at 12:30 and 5:30, both of which lie outside the range of the old grid (the first column of table).