2
$\begingroup$

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. >>
$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – mmal
    Oct 28, 2012 at 12:23
  • $\begingroup$ Are you and this user mathematica.stackexchange.com/questions/13745/… the same person? $\endgroup$ Oct 28, 2012 at 12:31
  • 1
    $\begingroup$ 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. $\endgroup$
    – rm -rf
    Oct 28, 2012 at 14:29
  • 1
    $\begingroup$ Same question, different user. mathematica.stackexchange.com/q/13576/973 $\endgroup$ Oct 28, 2012 at 19:21

2 Answers 2

5
$\begingroup$

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.

$\endgroup$
4
$\begingroup$

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);
$\endgroup$
2
  • 1
    $\begingroup$ hehe... what are we going to do now?? :) $\endgroup$
    – sebhofer
    Oct 28, 2012 at 12:36
  • $\begingroup$ Well it's my pleasure to return the favour! $\endgroup$
    – sebhofer
    Oct 28, 2012 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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