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'm stuck here with my variable assignment in this scenario

AntennaPower[measure_, antenna_] := 
 Module[{{x1, x2, sd1} = measure, {xa, ya} = antenna},
  N[ sd1*(4*Pi*((xa - x1)^2 + (ya - y1)^2)/1000)]]

AntennaPower[{2000, 0, 1/2}, {5, 60}]
Error message is Module::lvset: Local variable specification {{x1,x2,sd1}={2000,0,1/2},{xa,ya}={5,60}} contains {x1,x2,sd1}={2000,0,1/2}, which is an assignment to {x1,x2,sd1}; only assignments to symbols are allowed. >>
share|improve this question
    
You can not assign to a list in local variable specification as You did {x1,x2,sd1}=measure. Use an explicit names in function declaration, eg. AntennaPower[{x1_,x2_,sd1_},{xa_,ya_}] for local variables. –  mmal Oct 28 '12 at 12:23
    
Are you and this user mathematica.stackexchange.com/questions/13745/… the same person? –  belisarius Oct 28 '12 at 12:31
1  
You asked the same question earlier and you were given a simple answer and asked to read some basic tutorials... did you do that? This would've been answered had you simply looked up Module in the documentation. –  rm -rf Oct 28 '12 at 14:29
1  
Same question, different user. mathematica.stackexchange.com/q/13576/973 –  Fred Kline Oct 28 '12 at 19:21
add comment

2 Answers 2

The quick answer is:

AntennaPower[measure_List, antenna_List] := 
 Module[{x1,x2,xa,ya,sd2},
  {x1, x2, sd1} = measure;
  {xa, ya} = antenna;
  N[ sd1*(4*Pi*((xa - x1)^2 + (ya - y1)^2)/1000)]
]

The point here is that the first argument to Module can only be a sequence of symbols (or of assignments x=x0,y=y0,...) but no expressions are allowed. Just an aside: I changed your argument pattern to _List so that the functions only match when the arguments have the head List.

Edit

Of course there are many different ways to do what you want. mmal pointed out in his comment that you can also use

AntennaPower[{x1_,x2_,sd2_}, {xa_,ya}] := 
     Module[{},
      N[sd1*(4*Pi*((xa - x1)^2 + (ya - y1)^2)/1000)]]

In your case (where you know the length of your input lists) it is probably the wiser choice as this version only evaluates for the correct form of the input arguments.

share|improve this answer
add comment

You can not assign to a list in local variable specification as You did {x1,x2,sd1}=measure. You can assign specific parts of dummy variables:

AntennaPower[measure_, antenna_] := 
 Module[{x1=measure[[1]], x2=measure[[2]], sd1=measure[[3]], xa=antenna[[1]], ya=antenna[[2]]},
 N[sd1*(4*Pi*((xa - x1)^2 + (ya - y1)^2)/1000)]
]

or use equivalent sebhofer's method. But this might be dangerous when the length of actual arguments is different. Use an explicit names in function declaration, eg.

AntennaPower[{x1_,x2_,sd1_},{xa_,ya_}] :=
 N[sd1*(4*Pi*((xa - x1)^2 + (ya - y1)^2)/1000)]

EDIT: To end this lengthy discussion You can use method proposed by sebhofer with argument checking by:

AntennaPower[measure_, antenna_] := Module[{x1, x2, sd1, xa, ya},
     {x1, x2, sd1} = measure;
     {xa, ya} = antenna;
     N[sd1*(4*Pi*((xa - x1)^2 + (ya - y1)^2)/1000)]
    ] /; (Length[measure] == 3 && Length[antenna] == 2);
share|improve this answer
1  
hehe... what are we going to do now?? :) –  sebhofer Oct 28 '12 at 12:36
    
+1 for You of course! –  mmal Oct 28 '12 at 12:40
    
Well it's my pleasure to return the favour! –  sebhofer Oct 28 '12 at 12:41
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.