Message Boards Message Boards

0
|
7686 Views
|
12 Replies
|
4 Total Likes
View groups...
Share
Share this post:

[Solved] Making a function from data by interpolation

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 utilizing an Interpolation between them. 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 they work well.

E=interp1(Data(:,2),Data(:,1),P);

P=interp1(Data(:,1),Data(:,2),E);

Regarding the codes in Matlab, I have a Matrix named Data which the first column is related to energy density and the second column is for pressure.

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

For my Mathematica code, I separated the columns of the matrix mentioned above into two data as two Excel files. Then these two files were imported in the notebook in order to make an interpolation between them to utilize it as a function. Indeed the interpolation between energy density and pressure could work like a function which gives pressure in terms of energy density or vice versa.

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

POSTED BY: Davood Rafiei
12 Replies

Assuming that your data are simple lists of numbers of the same length, here is a syntax that plots one against the other:

pressure = RandomReal[{0, 1}, 5]
energyDensity = RandomReal[{0, 1}, 5]
f1 = Interpolation[Transpose[{pressure, energyDensity}],
  InterpolationOrder -> 1]
f2 = Interpolation[Transpose[{energyDensity, pressure}],
  InterpolationOrder -> 1]
Plot[f1[t], {t, 0, 1}]
Plot[f2[t], {t, 0, 1}]
POSTED BY: Gianluca Gorni

Thanks a lot for your attention. To clarify more, In my notebook I have a set of data for energy density which I named it DataA and another set of data for pressure named DataB that were imported.

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 written in Matlab, E=interp1(Data(:,2),Data(:,1),p1); It gives me the corresponding energy density by utilizing the interpolation

For an arbitrary energy density e1 I can use this code written in Matlab, 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.

POSTED BY: Davood Rafiei

Prepare the data in form of lists and then feed them to Interpolation this way:

pressure = {0.695, 0.408, 0.021, 0.458, 0.636};
energyDensity = {0.521, 0.256, 0.076, 0.356, 0.870};
fromPressureToEnergy = 
  Interpolation[Transpose[{pressure, energyDensity}], 
   InterpolationOrder -> 1];
fromEnergyToPressure = 
  Interpolation[Transpose[{energyDensity, pressure}], 
   InterpolationOrder -> 1];
fromPressureToEnergy[.3]
fromEnergyToPressure[.5]

For example, fromPressureToEnergy[.3] gives the energy corresponding to a pressure =.3.

POSTED BY: Gianluca Gorni

I faced this error, Interpolation::indat: Data point 1 contains abscissa 2, which is not a real number. May be there is problem in my data. Can I send you my Data? They are as a Excel file.

POSTED BY: Davood Rafiei

Yes, you can attach the file to the post.

POSTED BY: Gianluca Gorni

Welcome to Wolfram Community! Please show your code written in the Wolfram Language in order to make it easier for others to help.

POSTED BY: Moderation Team

Please kindly find the data related to pressure and energy density.

POSTED BY: Davood Rafiei
DataA = Import["D:\\Energy density.xlsx"];
DataB = Import["D:\\Pressure.xlsx"];

Pressure = {DataB};

EnergyDensity = {DataA};

fromPressureToEnergy = 
  Interpolation[Transpose[{Pressure, EnergyDensity}], 
   InterpolationOrder -> 1];
POSTED BY: Davood Rafiei

Thank you very much, it is exactly what i was looking for, but i think there is a problem in my data. so I attached the data.

This is the problem :Interpolation::indat

Interpolation::indat: Data point {<<1>>,5.60939*10^-12


    1.79459*10^-11


    3.60575*10^-11


    1.45565*10^-10


    2.92474*10^-10


    5.87649*10^-10


    2.37235*10^-9


    4.7666*10^-9


    1.92429*10^-8


    3.86634*10^-8


    1.23694*10^-7


    2.48531*10^-7


    4.99356*10^-7


    2.01591*10^-6


    4.05043*10^-6


    0.0000129584


    0.0000328543


    0.000066012


    0.000266491


    0.000535443


    0.00216159


    0.00434314


    0.0138949


    0.0279181


    0.0560939


    0.290646


    0.572372


    1.04226


    1.51234


    1.8885


    2.26473


    2.54695


    2.8292


    3.11148


    3.39381


    3.67615


    3.95854


    4.24094


    4.61753


    4.90001


    5.18251


    5.46503


    5.74759


    6.21858


    6.59541


    6.97229


    7.34921


    7.82044


    8.19745


    8.48023


    <<115>>} contains abscissa <<1>>, which is not a real number.
POSTED BY: Davood Rafiei
Posted 4 years ago

Hi Davood,

There are a couple of problems. The XLSX files have an empty second column, so ignore it

energyDensity = Import["~/Downloads/Energy density.xlsx", {"Data", 1, All, 1}];
pressure = Import["~/Downloads/Pressure.xlsx", {"Data", 1, All, 1}];

There is one data point that is duplicated

Transpose[{pressure, energyDensity}] // Counts // Select[GreaterThan@1]
(* <|{970.137, 1659.82} -> 2|> *)

It happens to be the last one, so ignore it.

fromPressureToEnergy = 
 Interpolation[Most@Transpose[{pressure, energyDensity}], InterpolationOrder -> 1]
POSTED BY: Rohit Namjoshi

Thank you very much for your attention.. the problems are solved. I really appreciate your help.

POSTED BY: Davood Rafiei

I really appreciate your help, the codes works quite well.

POSTED BY: Davood Rafiei
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract