2
$\begingroup$

Here is a sample list

data = {{1, 2.1, 55.2}, {2, 2.7, 60}, {3, 3.1, 65}, {4, 3.5, 67}, {5, 4.5, 72}, {6, 4.2, 77}, {7, 5.9, 80}}

which contains three rows.

Now, let's create another list

ti = 2.;
tf = 6.;
nmax = 200;
dt = (tf - ti)/(nmax - 1);
times = Table[i, {i, ti, tf, dt}];

So, we created a list containing 200 equally spaced numbers in the interval [2,6]. These numbers correspond to the second row of the initial data list.

Now, I want to interpolate the initial data list so as to be able to predict what would be the values of the first and third columns using the 200 list as a second row. So we should obtain a new list data2 containing as a second row the 200 equally spaced data from times list and as first and third columns the interpolated ones. Finally, if possible, I would like to merge data and data2 with ordered columns.

Any ideas?

$\endgroup$
4
  • $\begingroup$ Interpolation[data[[;; , {2, 1}]]] ? $\endgroup$
    – Alx
    Dec 18, 2019 at 10:05
  • $\begingroup$ @Alx And then, what about data2? $\endgroup$
    – Vaggelis_Z
    Dec 18, 2019 at 10:13
  • $\begingroup$ I don't understand why you need data2? With this simple interpolation you can sample 1st or 3rd column with any step of the 2nd column. $\endgroup$
    – Alx
    Dec 18, 2019 at 10:16
  • $\begingroup$ @Alx This is not what I want. I want to use the 200 values of times and create a new list data2 containing as a second column the 200 values and as first and third columns the interpolated ones from data. Please if possible provide a detailed answer. $\endgroup$
    – Vaggelis_Z
    Dec 18, 2019 at 10:18

2 Answers 2

3
$\begingroup$

With simple interpolation based on 2nd column:

ti = 2.;
tf = 6.;
nmax = 200;
dt = (tf - ti)/(nmax - 1);
int1 = Interpolation[data[[;; , {2, 1}]]] /@ Range[ti, tf, dt] // Quiet;
int2 = Interpolation[data[[;; , {2, 2}]]] /@ Range[ti, tf, dt] // Quiet;
int3 = Interpolation[data[[;; , {2, 3}]]] /@ Range[ti, tf, dt] // Quiet;

newdata = Transpose[{int1, int2, int3}]
$\endgroup$
1
  • 3
    $\begingroup$ This can be done more simply: newdata = Interpolation[{#2, {##}} & @@@ data] /@ Range[ti, tf, dt] // Quiet $\endgroup$
    – Mr.Wizard
    Dec 18, 2019 at 13:07
4
$\begingroup$
  1. Re-arrange data into two lists of {time, value} pairs rdata = data[[All, {2, #}]] & /@ {1, 3}.
  2. Use TemporalData with the option ResamplingMethod on rdata to get a temporal data object td.
  3. Use td["PathFunctions"] to get the two interpolating functions.
  4. Map the interpolating functions to your times.

td = TemporalData[data[[All, {2, #}]] & /@ {1, 3}, 
   ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}];

Show[ListPlot[td, PlotMarkers -> Automatic],
 ListPlot[Quiet@Through[td["PathFunctions"]@times], 
  PlotStyle -> {Red, Green}, BaseStyle -> PointSize[Small], 
  Joined -> False, DataRange -> {2, 6}], ImageSize -> Large]

enter image description here

To get data2 use

data2 = Quiet @
 Transpose[{td["PathFunctions"][[1]] @ #, #, td["PathFunctions"][[2]] @ #}]& @ times
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.