Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list of symbols that I have defined using

d = Table[Symbol["d" <> ToString[i]], {i, 1, n}] 

giving

d = {d1, d2, d3, d4, d5}

where n = 5 in this case. I use this as part of an argument to an interpolation, giving me

Vchamber = 0.111549 d1 - 0.101842 d2 + 0.857554 d3 - 1.09366 d4 + 2.24236 d5 

Now I want to define a function that gives something like

 fVchamber[d1_,d2_,d3_d4_,d5_] := 0.111549 d1 - 0.101842 d2 + 0.857554 d3 - 1.09366 d4 + 2.24236 d5 

Now, n is variable. Hence, the argument list is variable, as is the expression Vchamber. I was hoping that something like

 fVchamber[d__]:=Function[d,Vchamber]

would work but it doesn't. What am I doing wrong? I have looked for answers but I haven't found one yet.

share|improve this question
2  
Could you perhaps just work with lists instead of introducing those symbols? fVchamber[dlist_] := {0.1,0.2,0.3,0.4,0.5} . dlist –  Szabolcs Nov 15 '12 at 20:06
    
This is just one form that I have. I have expressions that are more complicated. –  user4667 Nov 15 '12 at 20:47
add comment

1 Answer 1

This seems to work:

fVchamber = Function[Evaluate@d, Evaluate@Vchamber]

So first, when you define a Function, you don't need a definition like

fVchamber[d__]:=...

Second, Function apparently Holds the arguments, so you need to make sure they are evaluated. After the above definition, this seems to work:

In[10]:= fVchamber[0, 1, 0, 0, 0]
Out[10]= -0.101842
share|improve this answer
    
See, for example the example where they use a similar construct to set f equal to the pure function –  tkott Nov 15 '12 at 20:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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