Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am relatively new to the Mathematica enviroment and not sure where I am going wrong. I have evaluated the question and understand it, but dont know how to exactly create a module for it.

The problem is:

  • I need to calculate PowerDensity at a location $(x,y)$
  • I am given the power $P$ at the source and its location $(x2,y2)$
  • To calculate the PowerDensity, you use $P_d= \frac{P}{4\pi r^2}$
  • The two $x$ and $y$ coordinates compute to give : $P_d = \frac{P}{(4*\pi*(y_2 - y_1)^2 - (x_2 - x_1)^2)}$

How do I write a Module like:

PowerDensity[antenna_, loc_] := Module[(* Enter your code here*)]

so the user would enter e.g. PowerDensity[{1000,1000,12}, {0,0}]?

So far I have this:

PowerDensity[antenna_, loc_] := Module[{xone, yone, w} {xtwo, ytwo},
    PD = w/(4*Pi*(ywo - yone)^2 - (xtwo - xone)^2),

But it is not working. Any help?

share|improve this question
Where's the power in PowerDensity[{1000,1000,12}, {0,0}]? You might be interested in the function SquaredEuclideanDistance[]... – J. M. Oct 24 '12 at 16:34
{1000,1000,12} is {xone,xtwo, and the power} – Daniel R Oct 24 '12 at 16:37
{0,0} is the location where we want to find the the x and y of that point – Daniel R Oct 24 '12 at 16:38
up vote 3 down vote accepted

A few things I see right away:

  • Whatever function you create inside the Module needs to depend on the variables antenna and loc. Figure out whether antenna and loc will be lists of numbers or just numbers. If they are lists, like a pair of $x$ and $y$ numbers, then your function will have to depend on antenna[[1]], antenna[[2]], etc.

  • Also, you have the structure for Module wrong. To figure out how a built-in function works, try typing


    and the Mathematica help function will show you how to use Module. Try building the simplest Module that you can, just so you know how the function works, then once you have a working example you can modify it to deal with the physics problem above.

ETA: I think your last formula is wrong (try and figure out what $r$ is in terms of the two given coordinates). You have two commas in the Module environment, where the second one should be a semicolon. And finally, the Print function must have its first letter capitalized just like any built-in mathematica function.

I don't know what the policy is around here about answering homework questions. I was trying to walk you to the answer without actually giving it, but I don't know that you'll get there from where you are. I would write the function the following way:

PowerDensity[antenna_, loc_] := Module[{r, power},
 r = Sqrt[(antenna[[1]] - loc[[1]])^2 + (antenna[[2]] - loc[[2]])^2];
 power = antenna[[3]];
 power/(4 Pi r^2)]
share|improve this answer
Antenna will be plain {x,y, power}, loc will be {x,y}.. that is a list yeah? – Daniel R Oct 24 '12 at 16:46
Both of those are lists. Inside Module, you'll call power as antenna[[3]]. xone would be antenna[[1]], and so on. – user4368 Oct 24 '12 at 16:48
no actually they are just numbers – Daniel R Oct 24 '12 at 16:50
ohh okay, i see – Daniel R Oct 24 '12 at 16:50
i'll try that right now, thanks! :) – Daniel R Oct 24 '12 at 16:51

This works here but does not work within Module because the 2nd and 3rd lines will not work inside the {} to create local variables. The variables in this example are global.

PowerDensity[antenna_List, loc_List] := {
{xone, yone, w} = antenna;
{xtwo, ytwo} = loc; 
 PD = w/(4*Pi*(ytwo - yone)^2 - (xtwo - xone)^2); PD

This splits out the info upon input and doesn't need Module because the variables are local. This works as long as the shapes of the lists defined here are the same shapes as the lists in the function call.

PowerDensity[{xone_, yone_, w_}, {xtwo_, ytwo_}] := {  
PD = w/(4*Pi*(ytwo - yone)^2 - (xtwo - xone)^2); PD  

You should print outside of the function, so you can use its output for calculations if desired.

share|improve this answer

Your Answer


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.