# Difficult parametric function

I've got this strange parametric function and I want to implement it on Mathematica. I know that I could use Excel and calculate it, but my database is quite large and I want to do some reproducible code. So, this is part of my dataset:

data = {{"M",    "tr",     "tb",    "type"   },
{556.35, 0.535904, 349.79,  "methane"},
{556.35, 0.526917, 349.79,  "methane"},
{556.35, 0.544891, 349.79,  "methane"},
{556.35, 0.512537, 349.79,  "methane"},
{561.6,  0.557603, 356.594, "ethane" },
{561.6,  0.57541,  356.594, "ethane" },
{535.5,  0.603455, 344.12,  "propane"},
{535.5,  0.610644, 344.12,  "propane"}};


and these are my parameters:

    (*if methane  if ethane  if propane*)
α  = {8.9669,     6.436,     2.532};
β  = {0.952,      0.764,     0.548};
γ  = {8.111,      5.771,     2.395};
a  = {0.60,       0.66,      0.55 };
a1 = {3.357,      0.987,     1.977};


I want to implement this function:

A = a1*tb^α/(M^beta*tc^γ);
fi = 1.6;
λ = A*(Sqrt[5]*(fi - tr)^2/(fi + tr))^a


where tb, M, tc are the values contained in the data and alfa, beta gamma, a and a1 vary with the type of fluid. I've used the functions Piecewise or If, but it is too complex for me! Thanks in advance.

-
beta and tc are undefined? – Dr. belisarius Jan 27 '14 at 22:54

I've assumed where you have tr and tc they're the same and you meant one of them (a typo?), so I just used tr. It is a bad idea to use uppercase initials for symbols, so I've used m for M and ax for A. I also removed the "column" descriptor from the data-set, just makes things cleaner. It appears to want to run this across the data-set, so I did so, but you can adapt it as needed.

(*{"M","tr","tb","type"}*)
data = {{556.35, 0.535904, 349.79, "methane"},
{556.35, 0.526917, 349.79, "methane"},
{556.35, 0.544891, 349.79, "methane"},
{556.35, 0.512537, 349.79, "methane"},
{561.6, 0.557603, 356.594, "ethane"},
{561.6, 0.57541, 356.594, "ethane"},
{535.5, 0.603455, 344.12, "propane"},
{535.5, 0.610644, 344.12, "propane"}};

(*if methane if ethane if propane*)
ptype["methane"] = 1;
ptype["ethane"] = 2;
ptype["propane"] = 3;

params = {{8.9669, 6.436, 2.532},
{0.952, 0.764, 0.548},
{8.111, 5.771, 2.395},
{0.60, 0.66, 0.55},
{3.357, 0.987, 1.977}};

result = Module[{m, tr, tb, type, α, β, a, γ, a1, ax, fi = 1.6},
{m, tr, tb, type} = #;
{α, β, γ, a, a1} = params[[All, ptype[type]]];

{m, tr, tb, type, ax = a1*tb^α/(m^β*tr^γ), fi,
ax*(Sqrt[5]*(fi - tr)^2/(fi + tr))^a}] & /@ data;

result //
TableForm[#,
ToString /@ {m, tr, tb, type, ax, fi, final}}] &


Here's the pretty-printed output from your example data. The raw result is just in the list result.

-
I appreciate a lot! – Mary Jan 30 '14 at 20:57

I would approach this by making A and λ functions.

• I shall use \[CapitalAlpha] in place of A as a matter of "good practice" for naming user Symbols. (That is, avoiding starting user Symbol names with a capital letter.)

• I shall assume that tc should have been tr in the definition of A.

• I shall make fi a SubValues parameter to illustrate how these may be used.

Code:

key = {"methane" -> 1, "ethane" -> 2, "propane" -> 3};

Α[{M_, tr_, tb_, i_Integer}] := a1[[i]] tb^α[[i]]/(M^β[[i]] tr^γ[[i]])

λ[fi_][{x__, type_String}] := λ[fi][{x, type /. key}]

λ[fi_][p : {M_, tr_, tb_, i_Integer}] := Α[p] (Sqrt[5] (fi - tr)^2/(fi + tr))^a[[i]]


Use:

λ[1.6] /@ Rest[data] // Column

9.20748*10^22
1.06954*10^23
7.94408*10^22
1.36575*10^23
6.56973*10^15
5.32754*10^15
563388.
542316.

-
Very good idea. Thanks – Mary Jan 30 '14 at 20:59

Since you are already using global symbols to hold the parameter data (you could avoid this with the method rasher showed, by the way) here is another option using an additional global value: type. I don't prefer this method but it may hold some interest nevertheless; I do feel that it is both concise and readable.

data = (* as defined in question *)

α := {8.9669, 6.436, 2.532}[[type]];
β := {0.952, 0.764, 0.548}[[type]];
γ := {8.111, 5.771, 2.395}[[type]];
a := {0.60, 0.66, 0.55}[[type]];
a1 := {3.357, 0.987, 1.977}[[type]];

λ[{M_, tr_, tb_, x_}] :=
Block[{type = x /. {"methane" -> 1, "ethane" -> 2, "propane" -> 3}},
a1 tb^α/(M^β*tr^γ) (Sqrt[5] (fi - tr)^2/(fi + tr))^a
]

fi = 1.6;
λ /@ Rest[data] // Column

9.20748*10^22
1.06954*10^23
7.94408*10^22
1.36575*10^23
6.56973*10^15
5.32754*10^15
563388.
542316.

-
Thanks. very useful! – Mary Jan 30 '14 at 20:58