Why am I getting so many error messages from my pendulum simulation?

I'm doing my end of semester project for a physics class. We were supposed to graphically model a physical occurrence; I chose doing a Pendulum Wave.

I have my code written, however Mathematica keeps kicking out these errors:

Manipulate::vsform: Manipulate argument Null does not have the correct form for a variable specification.


The help files isn't much use, but going to the error box gives these errors:

Lighter is not a Graphics primitive or directive.
Darker is not a Graphics primitive or directive.
Blue is not a Graphics primitive or directive.


There are a couple of more errors dealing with the Coordinate command. Below is all of my code. My best guess as to what's going on is that Graphics cannot be combined with the Table command. If that's the case, how do I use the list of solutions for the lengths with Graphics?

    Manipulate[
Module[
{sol, \[Theta], t, L, periods, perFcn, nPend, \[Theta]0, lens,
g = 9.8},
periods = 60/Range[51 + nPend - 1, 51, -1] ;
perFcn =
Sqrt[(32 L)/(g (1 - Cos[\[Theta]0]))]
EllipticF[\[Theta]0/2, Csc[\[Theta]0/2]^2];
lens = Chop[
Table[L /. First@Solve[periods[[i]] == perFcn, L], {i, nPend}] // N];
sol = Table[
NDSolve[{\[Theta]''[t] + g/lens[[i]] Sin[\[Theta][t]] ==
0, \[Theta][0] == \[Theta]0, \[Theta]'[0] == 0}, \[Theta][
t], {t, 0, time}, Method -> "StiffnessSwitching"]
, {i, 1, nPend}
];
Table[
Graphics[{
{Line[{{0,
0}, {(lens[[i]])*Sin[sol[[i]]], -(lens[[i]])*
Cos[sol[[i]]]}}]},
{Lighter[Brown], Rectangle[{-4, 0}, {4, .5}]},
{Darker[Red], Disk[{0, 0}, .1]},
{Blue,
Disk[{(lens[[i]])*Sin[sol[[i]]], -(lens[[i]])*
Cos[sol[[i]]]}, .2]}
},
PlotRange -> {{-5, 5}, {.5, -14}}, ImageSize -> {500, 300}
],
{i, 1, nPend}
]
]
,
{{nPend, 10, "Number of Pendulums"}, 1, 25, 1(*,Appearance->"Labelled"*)},
{{\[Theta]0, \[Pi]/6, "Initial Angle"}, {0, \[Pi]/12, \[Pi]/6, \[Pi]/4, \[Pi]/3}},
]


The graphics code came from this, with minor variations.

-

migrated from stackoverflow.comDec 11 '12 at 16:24

This question came from our site for professional and enthusiast programmers.

Currently time is undefined. – Vitaliy Kaurov Dec 11 '12 at 7:22

Your problems mostly stem from wrongly scoped variables and not understanding what NDSolve returns. I found it easier to generate the pendulum graphics in a Table expression that does not wrap the Graphics expression. Also, time t must be introduced into the Manipulate for it show the pendulums moving in time.

With[{time = 10., g = 9.8},
Manipulate[
Module[{items, sol, \[Theta], L, periods, perFcn, lens},
periods = 60/Range[51 + nPend - 1, 51, -1];
perFcn =
Sqrt[(32 L)/(g (1 - Cos[\[Theta]0]))] EllipticF[\[Theta]0/2,
Csc[\[Theta]0/2]^2];
lens =
Chop[Table[
L /. First@Solve[periods[[i]] == perFcn, L], {i, nPend}] //
N];
sol = Table[
NDSolve[{\[Theta]''[tt] + g/lens[[i]] Sin[\[Theta][tt]] ==
0, \[Theta][0] == \[Theta]0, \[Theta]'[0] == 0}, \[Theta][
tt], {tt, 0., time}, Method -> "StiffnessSwitching"], {i, 1,
nPend}][[All, 1, 1]];
sol = Cases[sol, Rule[_, (p : InterpolatingFunction[__])[_]] :> p,
Infinity];
items =
Table[{Line[{{0,
0}, {(lens[[i]])*Sin[sol[[i]]@t], -(lens[[i]])*
Cos[sol[[i]]@t]}}], {Blue,
Disk[{(lens[[i]])*Sin[sol[[i]]@t], -(lens[[i]])*
Cos[sol[[i]]@t]}, .2]}},
{i, 1, nPend}];
Graphics[{items, {Lighter[Brown],
Rectangle[{-4, 0}, {4, .5}]}, {Darker[Red], Disk[{0, 0}, .1]}},
PlotRange -> {{-5, 5}, {.5, -14}},
ImageSize -> {200, 150}]],
{{nPend, 2, "Number of Pendulums"}, 1, 25, 1,
Appearance -> "Labeled"}, {{\[Theta]0, \[Pi]/6,
"Initial Angle"}, {0, \[Pi]/12, \[Pi]/6, \[Pi]/4, \[Pi]/3}},
{t, 0., time}]]


This code does not cure everything. But it evaluates without error messages. The pendulum lengths and positons still aren't right. You still have some work to do, but this answer should get you moving.

-