I Have two set of data separately, which one of them is pressure and the other one is energy density.(P: pressure, E: energy density) I want to use these data as a function, P(E) or E(P) by using Interpolation. Could you please help me that how I can make a function of these data?

For instance in Matlab I wrote the codes below and it works well.



Data 1 is energy density and Data 2 is pressure.

In fact by the codes above in Matlab I could give an arbitrary pressure and then the result is the correspond energy density or vice versa.

My question is that how I can write such a code in Mathematica.

To clarify more, I have a set of data for energy density which I named it Data1 and another set of data for pressure named Data2 that were imported in the notebook.

The number of elements of both of data are equal since these data are related to a plot which shows pressure in terms of energy density.

Due to I do not have an explicit function for energy density in terms of pressure or pressure in terms of energy density I have to use interpolation.

In fact by using Interpolation I could use these data as a function. For instance, I choose an arbitrary pressure p1 for this code in Matlab, E=interp1(Data(:,2),Data(:,1),p1);

Then it gives me the corresponding energy density by the interpolation, or for an arbitrary energy density e1 I can use this code,

P=interp1(Data(:,1),Data(:,2),e1); and the result is the corresponding pressure.

Now I want to do the calculation explained above by Mathematica.

  • 1
    $\begingroup$ For getting the data from files into Mathematica have a look at Import. For interpolation there is Interpolation and for fitting data to a model there is FindFit. If you need help with any of these update your post and specify what the problem is specifically. $\endgroup$
    – Natas
    Jul 19 '20 at 12:58
  • $\begingroup$ I edited the question. @Natas $\endgroup$ Jul 19 '20 at 13:30
  • $\begingroup$ Perhaps you should give what code you have tried, including the form of the data you have in Mathematica. $\endgroup$
    – Michael E2
    Jul 19 '20 at 14:29
  • $\begingroup$ I really appreciate your help and attention, the code mentioned below works quite well. $\endgroup$ Jul 20 '20 at 6:08

Perhaps this:

energy   = Interpolation[Transpose@Reverse@Transpose@data, pressure];
pressure = Interpolation[data, energy];

Or this:

energy   = Interpolation[Transpose[{pressuredata, energydata}], pressure];
pressure = Interpolation[Transpose[{energydata, pressuredata}], energy];


Inverting data alternatives (in order of decreasing code length and decreasing efficiency):

data[[All, {2, 1}]] (* faster on large data (len > ~10^6 for me) *)
Reverse /@ data (* will unpack packed array if length < 100 *)

I suspect the difference in the first two depends on the size of the cache and is processor-dependent. I'm using an i7 with a 2MB cache and the times of the first two start coming together when the size of data starts approaching 2MB.

  • $\begingroup$ Thanks for your attention. As I said I have two set of data separately which I imported them from excel file. Data 1 Energy density, Data 2 Pressure. how can i use your answer for two data, since you only use one data. $\endgroup$ Jul 19 '20 at 14:19
  • $\begingroup$ @DavoodRafiei I just translated the MATLAB code, because code is clearer than descriptions. For instance, these are not valid variable names: "Data 1 Energy density, Data 2 Pressure". So I thought you were saying data[[All, 1]] is energy density and so forth. So if you have data1 and data2, use Transpose[{data2, data1}] and Transpose[{data1, data2} then. $\endgroup$
    – Michael E2
    Jul 19 '20 at 14:25
  • $\begingroup$ Sorry that my description is not clear since I am a new user of Mathematica. I have not found out exactly how I should rewrite your answer based on your last comment. $\endgroup$ Jul 19 '20 at 14:35
  • $\begingroup$ @DavoodRafiei No problem, I was just explaining my thinking. Replace the first argument of Interpolation with Transpose[{data2, data1}] === Transpose[{domaindata, rangedata}] and so forth. Is that what you mean? (Do you have two variables like data1 and `data2? Is it clear to you what to do for them?) $\endgroup$
    – Michael E2
    Jul 19 '20 at 14:49
  • $\begingroup$ I have edited my question to clarify more, please check it. Thanks a lot. $\endgroup$ Jul 19 '20 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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