First up: Yes I'm a beginner with Mathematica although I have plenty of experience with other languages.

Currently I'm trying to define a function, that when run animates a pendulum. When I try to run it, nothing happens. No error message is generated but no result is given. It's probably just a simple misset semicolon. I tried for hours to debug it already by restructuring the code, adding and removing semicolons, running it in a new kernel, inserting sanity checks and simplifying the code - to no avail. Every time I try to run the animation nothing seems to work anymore.

The Code begins with a function to plot static instances (which in itself works, I just included it to be complete) and then adds a function, that solves a movement equation and tries to apply it to the pendulum.

++++++Trigger Warning:+++++++

My Code is pretty messed up from the debugging and looks really spaghettiesque, which I will change once I have found the flaw in the code. You have been warned!


plotPendulum[theta_, t_, subs_List ] := 
    Module[{p1, p2, le, ps, ra, lines, dots, ceil, 
            text, text2, all, pendulum},
        p1 = {0, h[t]} /. subs;
        p2 = {l*Sin[theta], h[t] + l*Cos[theta]} /. subs;   
        le = (l + a) /. subs;
        ra = le*{{-1.05, 1.05}, {-1.55, 1.55}};
        ceil  = {Brown, Thickness[0.05], Line[{{-l,0}/.subs, {l,0}/.subs}]};
        lines = {Black, Thickness[0.01], Line[{{0, 0}, p1}], Thickness[0.005], Line[{p1, p2}]};
        dots  = {Black, PointSize[0.04], Point[{{0, 0}, p1}], Red, PointSize[0.1], Point[{p2}]};
        text = {Black, Background -> None,
        Text[m, p2 + {1.5, 0}, {-1.5, 0}],
        Text[l, p1 + (p2 - p1)/2 - {0.05*le, 0}, {1,0}],
        Text[" t = " <> ToString[t] <> " s ",{-l*0.5,1.1*Ceiling[a]}/.subs]};

        all = Join[ceil, lines, dots, text];

        pendulum = Show[Graphics[all, Background->White], PlotRange -> ra];

(*plotPendulum[2.001,Pi/4//N,{l->10,a->1.5,omega->2*Pi}]  *)

(*Problematic part ahead:*)

f[ln_,an_,wn_,thetan_,dthetan_]:=Module[{g= 9.81},
moveq = (-g+an*wn^2*Sin[t*w])*Sin[theta[t]]+ ln* D[D[theta[t],t],t]==0    ;

thetaoft= NDSolve[{moveq, theta[0]== thn, theta`== dthn},theta,{t,0,60}];

image= plotPendulum[thetaoft, Pi/4//N,{l->ln,a->an,omega->wn}];

test = Animate[image, {t, 0,  Pi/4//N}];(*I tried removing and adding the semicolons on image, test and return*)




Thanks to all people making it this far - any help or input is appreciated!

  • $\begingroup$ As a start, in the definition of plotPendulum, you have not defined the function h[t]. In the definition of moveq, w is undefined. In the definition of thetaoft, theta` makes no sense. Perhaps you mean theta'[t] or theta'[0]. The output of NDSolve is not a number so thetaoft is not a value that can be used as an argument to plotPendulum $\endgroup$ – Bob Hanlon Nov 9 '19 at 15:14
  • $\begingroup$ See pendulum demonstrations $\endgroup$ – Bob Hanlon Nov 9 '19 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.