I've been struggling to make the Which command more efficient. Below is my original code:
fitmodel[set_, k1_, k2_, k3_, k4_, k5_, m_, n_][t_] :=
Which[set == 1, Evaluate@model[1][k1, m, n][t], set == 2,
Evaluate@model[2][k2, m, n][t], set == 3,
Evaluate@model[3][k3, m, n][t], set == 4,
Evaluate@model[4][k4, m, n][t], set == 5,
Evaluate@model[5][k5, m, n][t]]
I Prepend the set number in front of my data and use this fitmodel to assign different model to different data. Since it's very inconvenient to change this fitmodel whenever I have different numbers of kinetic constants(k1,k2,...kn), I'm trying to make the code easier to handle. I tried the following:
args = Append[
Append[Prepend[
Table[ToExpression["k" <> ToString[i] <> "_"], {i, 1,
Length@allNames}], ToExpression["set_"]], ToExpression["m_"]],
ToExpression["n_"]];
k = Flatten[{set == #,
Evaluate@model[#][ToExpression["k" <> ToString[#]], m, n][t]}&/@Range@5, 2];
fitmodel[args][t_] := Which[Sequence@@k]
But it failed and the error message is:
Which::argctu: Which called with 1 argument. >>
I checked the output of "k" and it's exactly the same as my original code inside Which command. Then I used "Sequence@@k" to get rid of the curly bracket. So I thought it should work. Obviously I'm wrong and I couldn't come up with a solution to this issue. Hope someone could guide me forward. Thank you!!