# How to evaluate a function list, when size of the list is a dynamic input

I am not sure if my question makes any sense, however I will try to make it clear in the description. I am trying to plot a polygon inscribed inside a circle, where the variables are radius of the circle and number of sides. One way I did it was put all of the functions inside Manipulate command,however it is cumbersome and hard to read but it works:-

ClearAll["Global*"]
Manipulate[
ListLinePlot[
Transpose@{Table[
r/(Sqrt[3]*Sin[π/(n)] + Cos[π/(n)])*
Cos[θ], {θ, 0, 2 π, (2 π)/n}],
Table[r/(Sqrt[3]*Sin[π/(n)] + Cos[π/(n)])*
Sin[θ], {θ, 0, 2 π, (2 π)/n}]},
AspectRatio -> Automatic], {{r, 15}, 5, 20}, {{n, 8}, 3, 30, 1}]


Output:

The other way I am approaching is to define the functions and then use Manipulate command. However things are not working out the way they should.

ClearAll["Global*"]
f1[r_, n_, θ_] :=
r/(Sqrt[3]*Sin[π/(n)] + Cos[π/(n)])*Cos[θ];
f2[r_, n_, θ_] :=
r/(Sqrt[3]*Sin[π/(n)] + Cos[π/(n)])*Sin[θ];
g[f1_, f2_] :=
Transpose@{Table[
f1[r, n, θ], {θ, 0, 2 π, (2 π)/n}],
Table[f2[r, n, θ], {θ, 0, 2 π, (2 π)/n}]};
Manipulate[
ListLinePlot[g[f1, f2], AspectRatio -> Automatic], {n, 3, 10, 1}]


The above code is unable to run because n, which is number of sides is not defined and so Mathematica is unable to create a list of functions.I want to know, how do I go about coding like this in Mathematica. Also I am using Mathematica 11.2.

I want to extract the co-ordinates from the plots generated above to finally create something like this:-

I want to actually calculate the circumference of this star.I know that this can be achieved other ways but I need to have a visual representation. I have attached the Matlab code for this:-

r=6.5;
n=14;

alpha=pi/(n-1);
c=r/(sqrt(3)*sin(alpha)+cos(alpha));

theta=linspace(0,2*pi,n);
semi_theta=theta(2)/2;
x=zeros(1,n);
y=zeros(1,n);
xx=zeros(1,n+n-1);
yy=zeros(1,n+n-1);
i=1;
j=2;

while i<=n
x(i)=c*cos(theta(i));
y(i)=c*sin(theta(i));

if i>=2
side_length=sqrt((x(i)-x(i-1))^2+(y(i)-y(i-1))^2);

x_temp=(r)*cos(theta(i)-alpha);
y_temp=(r)*sin(theta(i)-alpha);

xx(j)=x_temp;
yy(j)=y_temp;

xx(j-1)=x(i-1);
xx(j+1)=x(i);
yy(j-1)=y(i-1);

yy(j+1)=y(i);

j=j+2;
end
i=i+1;

end
plot(xx,yy)

perimeter=(n-1)*side_length*2;
disp(perimeter)
pbaspect([1 1 1])


Thanks, Arunim

• It looks like you could either pass n to g as a third argument, or define g inside Manipulate -- something like Manipulate[ g[f1_,f2_]:=...; ListLinePlot... ]. The first is probably better and simpler. – jjc385 Nov 6 '17 at 19:49
• @jjc385 Thanks for the reply. Essentially you want me to do it the first way. However, the problem is I plan to use this polygon to plot something else, like a star or something, so the first method will become even more cumbersome. – Arunim Bhattacharya Nov 6 '17 at 19:55
• Could you edit the post with information explaining exactly what you'd like to do with the star (or something)? – jjc385 Nov 6 '17 at 19:58
• CirclePoints[] is really nice for this: With[{n = 8, r = 1}, Graphics[{Polygon[CirclePoints[{r, 0}, n] ~Riffle~ CirclePoints[{r/(Cos[π/n] + Sqrt[3] Sin[π/n]), π/n}, n]]}]] – J. M. will be back soon Nov 6 '17 at 21:22

You can use LocalizeVariables->False

Manipulate[
ListLinePlot[g[f1,f2],AspectRatio->Automatic],
{n,3,10,1},
LocalizeVariables->False
]


But you still to add control variable to set values for r.

ClearAll["Global*"]
f1[r_, n_, \[Theta]_] :=
r/(Sqrt[3]*Sin[\[Pi]/(n)] + Cos[\[Pi]/(n)])*Cos[\[Theta]];
f2[r_, n_, \[Theta]_] :=
r/(Sqrt[3]*Sin[\[Pi]/(n)] + Cos[\[Pi]/(n)])*Sin[\[Theta]];
g[f1_, f2_] := Transpose@{
Table[f1[r, n, \[Theta]], {\[Theta], 0, 2 \[Pi], (2 \[Pi])/n}],
Table[f2[r, n, \[Theta]], {\[Theta], 0, 2 \[Pi], (2 \[Pi])/n}]
};

Manipulate[
ListLinePlot[g[f1, f2], AspectRatio -> Automatic],
{n, 3, 10, 1},
{r, 1, 10, 1},
TrackedSymbols :> {n, r},
LocalizeVariables -> False
]
`

• Awesome!! Exactly what I was looking for. Thanks a lot! – Arunim Bhattacharya Nov 6 '17 at 20:13
• @ArunimBhattacharya Don't forget to accept the answer if it fit your needs; just click on the tick on the top left of the answer. – anderstood Nov 6 '17 at 21:15
• @anderstood done. – Arunim Bhattacharya Nov 6 '17 at 22:09
• @ArunimBhattacharya You must have upvoted, but not accepted :) Acceptation is the button just below the "downvote" arrow. It represents a tick mark. – anderstood Nov 6 '17 at 22:27