2
$\begingroup$

I'm a beginner in Mathematica with little programming background but I'm determined to learn this.

I'm trying to make a function that calculates two variables r2 & ψ. from my search I should use Module inside the function to achieve that. Yet I can't get it to return the variables correctly... can you tell me what am I doing wrong?

calc[r1_, r3_, r4_, ϕ_, ψ_] :=
 Module[{r2_},
    r2 = N[√((r3 Cos[ϕ Degree] - 
         r1 Cos[ψ Degree])^2 + (r3 Sin[ϕ Degree] - 
         r1 Sin[ψ Degree])^2)]; {r2};
  Module[{a, b, ob},
    a = AngleVector[{r1, ϕ Degree}];
    b = AngleVector[{r3, ψ Degree}];
    ob = AngleVector[{-r4, 0}]; {a, b, ob}
   ]
    ]

Phi & Psi are both greek symbols for angles.

  1. How to control which variable the module returns?
  2. Am I including two modules correctly in terms of syntax in the function?
  3. How to control which variable the big function returns?
$\endgroup$
2
  • $\begingroup$ For one thing, get rid of the underbar in Module[{r2_},.... $\endgroup$
    – ciao
    Aug 16, 2015 at 6:42
  • $\begingroup$ @ciao YUP that made the whole thing works after removing the semicolon too. THANKS! $\endgroup$ Aug 16, 2015 at 7:27

1 Answer 1

5
$\begingroup$

Everything in Mathematica has a precedence, including semicolon! That is similar to 3+4*5 meaning the multiplication is done before the addition. So the way you have it written above says the definition of your calc function ends with your first semicolon and then your second Module will calculate a result without waiting for you to later use your calc function.

Try writing really really simple Modules that just print which module just executed and see if you can figure out how your code would run.

If you were to write calc[r1_, etc]:= (Module[stuff];Module[morestuff]) then the () changes the order of operations. So when you use calc it will do the first module, throw away the result, do the second module and return that as the value of your calc function.

So what can you do? Well you could do this

calc[r1_, r3_, r4_, ϕ_, ψ_] :=
 { N[√((r3 Cos[ϕ Degree] - r1 Cos[ψ Degree])^2 +
    (r3 Sin[ϕ Degree] - r1 Sin[ψ Degree])^2)],
  {AngleVector[{r1, ϕ Degree}],
   AngleVector[{r3, ψ Degree}],
   AngleVector[{-r4, 0}]
  }
 };

That will calculate both results, put them in a list and return that as the value of your calc function. Then you could use calc[stuff][[1]] to extract the first value or calc[stuff][[2]] to extract the second list of three values.

Or you could add an extra final argument to calc, which would be either True or False, and use an If function inside the definition of calc to decide which calculation to do.

Notice that I didn't even use Module in this. That was because you didn't have enough reason to have "local variables", etc. and just the expressions you wanted were able to be used on their own. In other words I tried to simplify your code down to just the bare amount needed.

Hopefully this gives you some ideas to think about and perhaps you can progress to the next level of understanding.

$\endgroup$
8
  • $\begingroup$ Thanks so much for the quick response. So you basically used this syntax calc[] := {operation1, {operation2}} which corresponds to the format of the results I got too. So in general it's like this: F[]:= {op1,op2,{op31,op32}} Am I correct? $\endgroup$ Aug 16, 2015 at 7:19
  • $\begingroup$ Correct. I modified your calc function to return a list of both results. Then your code using calc could extract the desired value from that list. $\endgroup$
    – Bill
    Aug 16, 2015 at 7:26
  • $\begingroup$ Thanks Bill! I appreciate it... $\endgroup$ Aug 16, 2015 at 7:30
  • $\begingroup$ One more question, If I want to use the on of those results in the same function can I do it this way? or should I use another way? $\endgroup$ Aug 16, 2015 at 7:44
  • $\begingroup$ Maybe a spelling error in that last comment? I don't understand. Or maybe you put back in your Module which can let you use previous results while still inside the Module. Please correct or explain if I have misunderstood. $\endgroup$
    – Bill
    Aug 16, 2015 at 7:48

Your Answer

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

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