4
$\begingroup$
f[p_, g_, place1_, velocity1_, place2_, velocity2_, length1_, length2_, mass_] := 
Module[{sol},
sol = NDSolve[{-mass* (length1+length2* Sin[\[Beta][t]]) (2 *length2* Cos[\[Beta][t]] *\[Alpha]'[t] *\[Beta]'[t]+(length1+length2* Sin[\[Beta][t]]) *\[Alpha]''[t])==0,length2* mass*(g* Sin[\[Beta][t]]+Cos[\[Beta][t]] (length1+length2* Sin[\[Beta][t]]) *(\[Alpha]'[t])^2-length2*\[Beta]''[t]) == 0 , \[Alpha][0]==place1, \[Alpha]'[0]==velocity1, \[Beta][0]==place2, \[Beta]'[0]==velocity2}, {\[Alpha][t], \[Beta][t]}, {t, 0,p}, MaxSteps-> 100000];

a[t_] = \[Alpha][t] /. sol[[1]];
b[t_] = \[Beta][t] /. sol[[1]];


Show[ParametricPlot3D[{(length1 + length2*Sin[Evaluate[b[t]]])*Cos[Evaluate[a[t]]], (length1 + length2 * Sin[Evaluate[b[t]]])*Sin[Evaluate[a[t]]],length2 * Cos[Evaluate[b[t]]] }, {t, 0, p},PlotRange -> All]]];

Manipulate[
f[p, g, place1, velocity1, place2, velocity2, length1, length2, mass], {{g, 9.8, "gravitational acceleration"}, 1, 100, Appearance -> "Labeled"}, {{place1, Pi, "starting place alpha"}, 0, 2*Pi, Appearance -> "Labeled"}, {{velocity1, 1, "starting velocity alpha"}, 0, 10, Appearance -> "Labeled"}, {{place2, Pi, "starting place beta"}, 0, 2*Pi, Appearance -> "Labeled"}, {{velocity2, 1, "starting velocity beta"}, 0, 10, Appearance -> "Labeled"}, {{length1, 1, "length l1"}, 1, 10, Appearance -> "Labeled"}, {{length2, 1, "length l2"}, 1, 10, Appearance -> "Labeled"}, {{mass, 1, "mass1"}, 1, 10, Appearance -> "Labeled"}, {{p,1, "time"}, 1, 10, Appearance->"Labeled"}]

I mean it should be alright theoretically, I don't see any mistake here?

$\endgroup$
5
  • $\begingroup$ Before throwing all the code into Manipulate then wonder why it is not working, did you to test your f function on its own? This is really basic debugging. Always make sure the function you manipulating works OK outside of Manipulate. I just did f[1, 9.8, Pi, 1, Pi, 1, 1, 1, 1] and it produced an empty box. !Mathematica graphics which tells me the problem is in your f function. So you need to find out why your f function does not work first. Most likely NDSolve is not working or producing complex solution. You can use Print to find that out. $\endgroup$
    – Nasser
    Oct 23, 2022 at 15:04
  • 1
    $\begingroup$ I don't see any mistake here? have you tested it?? If you test each code you write before adding more to it, you will be able to find your error much easier. i.e. test to make sure NDsolve is first working, then test the ParametricPlot3D is working OK on the solution produced, then you can test f on its own, and only then use f inside Manipulate. $\endgroup$
    – Nasser
    Oct 23, 2022 at 15:05
  • $\begingroup$ @Nasser I think the problem is that I try to extract the solutions from NDSolve independently (it's what I do here with a[t], b[t]), but I don't know how to take into account that these solutions should still depend on all those parameters like g, place1, velocity1, ... So I think ParametricPlot3D doesn't show me anything as it interprets it this way, that mass, length1, ... are unknown constants, and no parameters $\endgroup$
    – anon
    Oct 23, 2022 at 15:10
  • 1
    $\begingroup$ I second what Nassar said. Your problem isn't with Manipulate. After you evaluate your definition for f, examine a and b. Information[a] gives you a[t$_] = \[Alpha][t$]. But Information[\[Alpha]] shows no OwnValues/DownValues. I'm not going to debug your code, but I suspect that the problem is here, a[t_] = \[Alpha][t] /. sol[[1]]. You probably just want to set a definition for a, not a[t]. $\endgroup$
    – lericr
    Oct 23, 2022 at 15:18
  • $\begingroup$ You can also use the code from https://mathematica.stackexchange.com/a/275061/72111 to make the manipulate work. $\endgroup$
    – cvgmt
    Oct 23, 2022 at 15:56

2 Answers 2

6
$\begingroup$
$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global`*"]

f[p_, g_, place1_, velocity1_, place2_, velocity2_, length1_, 
   length2_, mass_] :=
  Module[{sol},
   sol = NDSolve[{
       -mass*(length1 + 
           length2*Sin[β[t]]) (2*length2*
            Cos[β[t]]*α'[t]*β'[
             t] + (length1 + length2*Sin[β[t]])*α''[t]) ==
         0,
       length2*
         mass*(g*Sin[β[t]] + 
           Cos[β[t]] (length1 + 
              length2*Sin[β[t]])*(α'[t])^2 - 
           length2*β''[t]) == 0,
       α[0] == place1, α'[0] == velocity1,
       β[0] == place2, β'[0] == velocity2},
      {α, β}, {t, 0, p},
      MaxSteps -> 100000][[1]];
   a[t_] = α[t] /. sol;
   b[t_] = β[t] /. sol;
   ParametricPlot3D[{
     (length1 + length2*Sin[b[t]])*Cos[a[t]],
     (length1 + length2*Sin[b[t]])*Sin[a[t]], length2*Cos[b[t]]},
    {t, 0, p},
    PlotRange -> All,
    BoxRatios -> {1, 1, 1/2}]];

Manipulate[
 f[p, g, place1, velocity1, place2, velocity2, length1, length2, mass],
 {{g, 9.8, "gravitational acceleration"}, 1, 100, 
  Appearance -> "Labeled"},
 {{place1, Pi, "starting place alpha"}, 0, 2*Pi, 
  Appearance -> "Labeled"},
 {{velocity1, 1, "starting velocity alpha"}, 0, 10, 
  Appearance -> "Labeled"},
 {{place2, Pi, "starting place beta"}, 0, 2*Pi, 
  Appearance -> "Labeled"},
 {{velocity2, 1, "starting velocity beta"}, 0, 10, 
  Appearance -> "Labeled"},
 {{length1, 1, "length l1"}, 1, 10, Appearance -> "Labeled"},
 {{length2, 1, "length l2"}, 1, 10, Appearance -> "Labeled"},
 {{mass, 1, "mass1"}, 1, 10, Appearance -> "Labeled"},
 {{p, 1, "time"}, 1, 10, Appearance -> "Labeled"},
 ControlPlacement -> Top]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you so much! But how does your code differ from mine? What was my mistake here? $\endgroup$
    – anon
    Oct 23, 2022 at 15:27
  • $\begingroup$ In NDSolve I solved for {\[Alpha], \[Beta]} rather than {\[Alpha][t], \[Beta][t]}and took its first part sol = NDSolve[ ... ][[1]] rather than using Part each time sol is used. Then I eliminated the random sprinkling of Evaluate $\endgroup$
    – Bob Hanlon
    Oct 23, 2022 at 16:40
3
$\begingroup$

The third way is using := instead of using =, that is replace

a[t_] = α[t] /. sol[[1]];
b[t_] = β[t] /. sol[[1]];

to

a[t_] := α[t] /. sol[[1]];
b[t_]: = β[t] /. sol[[1]];

Edit

Reply to comment.

Clear["Global`*"]; 
f[p_, g_, place1_, velocity1_, place2_, velocity2_, length1_, 
  length2_, mass_] := 
 Module[{sol}, 
  sol = NDSolve[{-mass*(length1 + 
         length2*Sin[β[t]]) (2*length2*
          Cos[β[t]]*α'[t]*β'[
           t] + (length1 + length2*Sin[β[t]])*α''[t]) == 
      0, length2*
       mass*(g*Sin[β[t]] + 
         Cos[β[t]] (length1 + 
            length2*Sin[β[t]])*(α'[t])^2 - 
         length2*β''[t]) == 0, α[0] == 
      place1, α'[0] == velocity1, β[0] == 
      place2, β'[0] == velocity2}, {α[t], β[
      t]}, {t, 0, p}, MaxSteps -> 100000];
  a[t_] := α[t] /. sol[[1]];
  b[t_] := β[t] /. sol[[1]];
  Show[ParametricPlot3D[{(length1 + length2*Sin[Evaluate[b[t]]])*
      Cos[Evaluate[a[t]]], (length1 + length2*Sin[Evaluate[b[t]]])*
      Sin[Evaluate[a[t]]], length2*Cos[Evaluate[b[t]]]}, {t, 0, p}, 
    PlotRange -> All]]];
Manipulate[
 f[p, g, place1, velocity1, place2, velocity2, length1, length2, 
  mass], {{g, 9.8, "gravitational acceleration"}, 1, 100, 
  Appearance -> "Labeled"}, {{place1, Pi, "starting place alpha"}, 0, 
  2*Pi, Appearance -> "Labeled"}, {{velocity1, 1, 
   "starting velocity alpha"}, 0, 10, 
  Appearance -> "Labeled"}, {{place2, Pi, "starting place beta"}, 0, 
  2*Pi, Appearance -> "Labeled"}, {{velocity2, 1, 
   "starting velocity beta"}, 0, 10, 
  Appearance -> "Labeled"}, {{length1, 1, "length l1"}, 1, 10, 
  Appearance -> "Labeled"}, {{length2, 1, "length l2"}, 1, 10, 
  Appearance -> "Labeled"}, {{mass, 1, "mass1"}, 1, 10, 
  Appearance -> "Labeled"}, {{p, 1, "time"}, 1, 10, 
  Appearance -> "Labeled"}]
$\endgroup$
4
  • $\begingroup$ This won't work, OP uses {\[Alpha][t], \[Beta][t]} in NDSolve. $\endgroup$
    – xzczd
    Oct 24, 2022 at 1:52
  • $\begingroup$ @xzczd This work. Please test the the code of questionor and use my setting. The questionor use a[t] and b[t]. $\endgroup$
    – cvgmt
    Oct 24, 2022 at 1:57
  • $\begingroup$ OK, I forgot Block called by ParametricPlot will change every t. But I'd argue this is a rather dangerous fix. The function defined in this way only works together with Block e.g. Block[{t = 1}, a[t]] gives the desired numeric value, but a[1] won't. $\endgroup$
    – xzczd
    Oct 24, 2022 at 2:07
  • $\begingroup$ @xzczd Thanks. I also recommend the questionor use the setting in his another question mathematica.stackexchange.com/a/275061/72111 $\endgroup$
    – cvgmt
    Oct 24, 2022 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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