1
$\begingroup$

I'd like to vary a factor in a matrix calculation, so I have used Module to make it happened, but it doesn't work. Below is a code example. can someone tell me how to fix it? It would be appreciated.

polardata[omega_] := 
  Module[{},
    matrix = Flatten[RandomReal[0.5, {1, 6}], 1];
    intermediate = omega*matrix;
    addition = Flatten[RandomReal[0.5, {1, 6}], 1];
    polardata = Join[intermediate, addition]]

Manipulate[
 ListPolarPlot[polardata[omega], DataRange -> {0, 2 π}, 
   Joined -> True], 
 {omega, 0.1, 1.0}]
$\endgroup$
4
  • 2
    $\begingroup$ Don't define polardata inside the definition for polardata, you are wiping the Module definition as soon as you evaluate it. Just take the polardata = out of the last line of the Module and you are fine. $\endgroup$
    – Jason B.
    Commented Feb 23, 2016 at 14:42
  • 1
    $\begingroup$ You need to read up on how code functions in the Wolfram Language. Functions and Programs overview should help. You appear to be using syntax from another programming language. $\endgroup$
    – Edmund
    Commented Feb 23, 2016 at 14:48
  • $\begingroup$ @ Jason B, You're right. It works. Thank you for your help. $\endgroup$
    – SungwooY
    Commented Feb 23, 2016 at 16:46
  • $\begingroup$ @ Edmund, I already read those and couples of books, but I guess I am a very slow learner. Thank you for your suggestion. I appreciate it. $\endgroup$
    – SungwooY
    Commented Feb 23, 2016 at 16:47

1 Answer 1

3
$\begingroup$

If you are going to use a programming style that breaks down a procedure into several named steps (and I often use this style myself), then use Module properly and localize the names. Further, return the result, do not assign it to the procedure name.

With a few changes along the lines mentioned above, your code will work.

 polardata[omega_] :=
   Module[{matrix, intermediate, addition}, 
     matrix = Flatten[RandomReal[0.5, {1, 6}], 1];
     intermediate = omega*matrix;
     addition = Flatten[RandomReal[0.5, {1, 6}], 1];
     Join[intermediate, addition]]

Manipulate[
  ListPolarPlot[polardata[omega], DataRange -> {0, 2 π}, Joined -> True],
  {omega, 0.1, 1.0}]

demo

$\endgroup$
1
  • $\begingroup$ Hi m_goldberg, yes, it works. Thank you for your help. In addition, I also see that a code also works without anything in Module[{},...]. Just removing " polardata" in Module from my original code seems works fine. I don't have a good intuition to understand this though. Thank you. $\endgroup$
    – SungwooY
    Commented Feb 23, 2016 at 16:50

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