2
$\begingroup$

I have a txt file with 3 columns of data:

x y1 y2

How do I import the file then create 2 functions, one interpolated with {x,y1} values and the other interpolated with {x,y2} values?

This is the sort of thing I want to achieve (in pseudocode):

Import "data.txt" into A
f1 = Interpolation(A,{x,y1})
f2 = Interpolation(A,{x,y2}) 
$\endgroup$
2
  • $\begingroup$ Did you happen to look up the Import documentation? This and its subpage on basic formats (search for "Table") would be a good starter.Interpolation also has a very readable doc page. To get the table you obtain after importing into separate columns, you need Transpose. Please, try to reach a minimum proficiency level of the language by reading the documentation before asking questions. $\endgroup$ Jun 22, 2012 at 12:39
  • 1
    $\begingroup$ For taking parts of arrays you could check out the documentation of Part $\endgroup$
    – Heike
    Jun 22, 2012 at 12:45

1 Answer 1

6
$\begingroup$

Maybe something like

a = Transpose[Import["data.txt", "Table"]];

f1 = Interpolation[{a[[1]], a[[2]]} // Transpose];
f2 = Interpolation[{a[[1]], a[[3]]} // Transpose];

might work?

Alternatively,

a = Import["data.txt", "Table"];

f1 = Interpolation[Drop[#, {3}] & /@ a];
f2 = Interpolation[Drop[#, {2}] & /@ a];

or even

f1 = Interpolation[ a[[All, {1, 2}]] ];
f2 = Interpolation[ a[[All, {1, 3}]] ];
$\endgroup$
2
  • $\begingroup$ For the second one, you can also use a[[All,{1,2}]] and a[[All, {1,3}]] instead of Drop[...]&/@a. $\endgroup$
    – Heike
    Jun 22, 2012 at 12:42
  • $\begingroup$ Thanks, but only the first method worked for me $\endgroup$
    – Eddy
    Jun 22, 2012 at 13:25

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.