1
$\begingroup$

I have also tried the "Do" function, that too does not work

ztar[n_] := Module[{xlist, ylist, points, plots}, plots = {}; xlist = {1}; ylist = {0}; For[i = 1, i < n + 1, i++, AppendTo[xlist, Cos[i*(n - 1)/n*Pi]]; AppendTo[ylist, Sin[i*(n - 1)/n*Pi]]; points = Transpose[{xlist, ylist}]; AppendTo[plots, Graphics[Line[points], Axes -> True]] ]; Show[plots, PlotRange -> Full] ] 

I can't figure out how to make a function be called within a For Loop while also giving an output. The output works fine, just not in a For Loop. Any help is greatly appreciated.

$\endgroup$
5
  • 1
    $\begingroup$ For loops never return anything. Use Table instead: Table[ztar[i], {i, 1, 9, 2}]. See Alternatives to procedural loops and iterating over lists in Mathematica. $\endgroup$ Commented Jan 2, 2023 at 15:23
  • $\begingroup$ You can change your For [ ] loop by writing For[i = 1, i <= 9, i += 2; Print[ztar[i]]]. You must use Print[ ] to have the For[ ] loop show the plots. $\endgroup$ Commented Jan 2, 2023 at 15:28
  • 3
    $\begingroup$ Required reading: Why should I avoid the For loop in Mathematica? $\endgroup$ Commented Jan 2, 2023 at 15:41
  • $\begingroup$ Basic principle: An "Output" cell shows the value of an executed statement unless the value is Null. An "Input "cell may contain more than one statement and produce more than one "Output" cell. Print writes a "Print" cell into the evaluation notebook, CellPrint can write any type of cell into a notebook, etc. Personally, I avoid Print[], esp. in loops, except for debugging (but still not in loops). Outside loops, it's mainly a style preference. If just learning Mma, style preferences should not be a priority, but avoiding an infinite loop of Print[] statements should be. $\endgroup$ Commented Jan 2, 2023 at 15:56
  • $\begingroup$ A long time ago (pre V5?) Show used to do what its name implies: show graphics. Now it is mainly used to combine multiple graphics. You can semi-emulate the old functionality with Show[plots, PlotRange -> Full, DisplayFunction -> Print] or DisplayFunction -> (CellPrint[ExpressionCell[#, "Output"]] &) $\endgroup$ Commented Jan 2, 2023 at 16:07

2 Answers 2

5
$\begingroup$

Try (see @MarcoB's comment)

ztar[n_Integer?Positive] := Module[{xy, plots}, xy = Table[{Cos[k (n - 1)/n Pi], Sin[k (n - 1)/n Pi]}, {k, 0, n}]; Graphics[Line[xy]]] ztar[5] 

enter image description here

$\endgroup$
2
  • $\begingroup$ The definition should be ztar[n_Integer?Positive] := ... $\endgroup$ Commented Jan 2, 2023 at 15:42
  • $\begingroup$ Thanks for your hint $\endgroup$ Commented Jan 2, 2023 at 20:44
4
$\begingroup$

The issue around For not producing an output has been pretty well covered, and I suggest the OP follow the links in the comments. My contribution is to fix what I presume is a defect for cases when ztar is passed an even number. Maybe it will also provide some hints for finding alternates to the imperative style.

myStarPoints[ptCount_Integer?(GreaterEqualThan[3])] := With[ {jump = Max[Select[Range@Floor[ptCount/2], CoprimeQ[#, ptCount] &]]}, With[ {seq = Mod[jump Range[1 + ptCount], ptCount, 1]}, CirclePoints[ptCount][[seq]]]] 

Comparison

Multicolumn[ztar /@ Range[3, 10], {2, Automatic}] 

enter image description here

In the above results, note the dangling ends when the argument is even.

Multicolumn[Graphics@*Line /@ myStarPoints /@ Range[3, 10], {2, Automatic}] 

enter image description here

$\endgroup$
5
  • $\begingroup$ Hi, thank you for your answer, I appreciate it. Why does just adding 2 not work? (look below for my example) For[i = 3, i <= 9, i += 2; Print[star[i]]] $\endgroup$ Commented Jan 2, 2023 at 17:48
  • $\begingroup$ Not entirely sure I understand the question. If you're asking why the i+=2 bit doesn't work, then don't worry--that works fine. You're skipping the even values so you never try to generate a star with even points. My point is that your code doesn't produce stars for even inputs that I consider valid (but maybe you do). There is nothing in your code that makes it obvious that it's not intended to work for even inputs. At the same time, it's certainly possible to produce stars with even numbers of points. So, I'm just filling that gap. $\endgroup$ Commented Jan 2, 2023 at 18:14
  • 1
    $\begingroup$ I'm also introducing you to CirclePoints, which seems relevant to your code. And I'm showing how you can take values from a list in a particular order/permutation, which allows you to avoid looping constructs in this case. Finally, I'm showing a separation between presentation and "domain", which is an important concept. $\endgroup$ Commented Jan 2, 2023 at 18:19
  • $\begingroup$ Hi again, and thank you. I think I found the culprit for my error. When creating the "star" function, I used a for loop with "i", so when I later try out "For[i = 3, i <= 9, i += 2; Print[star[i]]]", I get wrong output ", but get what I am after by replacing "i" with "k", like this: For[k = 3, k <= 9, k += 2; Print[star[k]]] Does that make sense? Again, thank you. $\endgroup$ Commented Jan 2, 2023 at 18:22
  • $\begingroup$ Actually, no it doesn't. I think we're talking about two completely different things. Maybe I missed a comment thread or something. But I don't think it really matters as long as you're getting your question answered. $\endgroup$ Commented Jan 2, 2023 at 18:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.