0
$\begingroup$

Code:

ClearAll["Global`*"]
x[t_] = L*Cos[\[CapitalPhi][t]];
y[t_] = L*Sin[\[CapitalPhi][t]] + a*Cos[\[Gamma]*t];
T = 1/2 * m * ((x'[t])^2 + (y'[t])^2);
U = -m*g*x[t];
Lagr = T - U;
Lgr = Simplify[Expand[Lagr]];
Eq1 =  D[#1, #2] - D[D[#1, #3], t]   & [Lgr, \[CapitalPhi][t], \[CapitalPhi]'[t]];
Eq4 = FullSimplify[Eq1];
Eq3 = Simplify[Expand[Eq4]];
Eq2 = Solve[Eq3 == 0, \[CapitalPhi]''[t]];

f[L_, a_, \[Gamma]_, g_] = Module[{sol},
sol = NDSolve[{Eq3 == 0, \[CapitalPhi][0] == 0, \[CapitalPhi]'[0] == 0}, \[CapitalPhi][t], {t, 0, 10},
 MaxSteps -> 100000];
Show[Plot[\[CapitalPhi][t] /. sol[[1]], {t, -4, 4}]]];

Manipulate[
f[L, a, \[Gamma], g], {{L, 1}, 1, 10}, {{a, 1}, 0,
10}, {{\[Gamma], 1}, 0, 10}, {{g, 9.81}, 1, 20}]

It shows me a lot of error messages:

enter image description here

If I dummy test the NDSolve function, everything works perfectly fine:

enter image description here

So I really don't know where's the mistake here. Even my Professor couldn't find it out.

$\endgroup$
3
  • $\begingroup$ In NDSolve, you have 2 equations, but only one depended variable. $\endgroup$ Nov 18, 2022 at 17:45
  • $\begingroup$ But I don't have two equations? I just got Eq3? $\endgroup$
    – Student
    Nov 18, 2022 at 18:17
  • $\begingroup$ Sorry, I made a mistake. $\endgroup$ Nov 18, 2022 at 19:12

1 Answer 1

1
$\begingroup$

enter image description here

two things. You did not set $m$ value in Manipulate, and did not evaluate the ode so it binds the manipulate slider values into the variable build in the ode before calling NDSolve. That is why NDSolve complained. It saw symbols instead of numbers.

btw, I noticed that changing a and m had no effect on the solution. You might want to check why that is. May be your equations eliminated these.

ClearAll["Global`*"]
x[t_] = L*Cos[Φ[t]];
y[t_] = L*Sin[Φ[t]] + a*Cos[γ*t];
T = 1/2*m*((x'[t])^2 + (y'[t])^2);
U = -m*g*x[t];
Lagr = T - U;
Lgr = Simplify[Expand[Lagr]];
Eq1 = D[#1, #2] - D[D[#1, #3], t] &[Lgr, Φ[t], Φ'[t]];
Eq4 = FullSimplify[Eq1];
Eq3 = Simplify[Expand[Eq4]];
Eq2 = Solve[Eq3 == 0, Φ''[t]];

f[L0_, a0_, γ0_, g0_, m0_] := Module[{sol},
   sol = 
    NDSolve[{(Eq3 /. {L -> L0, a -> a0, γ -> γ0, 
          g -> g0, m -> m0}) == 0, Φ[0] == 0, Φ'[0] == 0}, Φ[t], {t, 0, 10}, 
     MaxSteps -> 100000];
   Show[Plot[Φ[t] /. sol[[1]], {t, 0, 4}]]];

Manipulate[f[L, a, γ, g, m],
 {{L, 1}, 1, 10, Appearance -> "Labeled"},
 {{a, 4}, 1, 10, Appearance -> "Labeled"},
 {{γ, 1}, 0, 10, Appearance -> "Labeled"},
 {{g, 9.81}, 1, 20, Appearance -> "Labeled"},
 {{m, 1}, 1, 20, Appearance -> "Labeled"},
 TrackedSymbols :> {L, a, γ, g, m}
 ]
$\endgroup$
2
  • $\begingroup$ "did not evaluate the ode so it binds the manipulate slider values into the variable build in the ode before calling NDSolve. That is why NDSolve complained. It saw symbols instead of numbers" Here, I see that the only difference to my code is that you to /. {L -> L0} etc. Is this what you mean by "evaluating the ode"? So basically what you mean is that the variables of the function f[..] cannot be the same variables used prior (like I need to use e.g. L0, and not L like it's used in x[t]? And if I want to use an equation which's got L in it, I got to change it by /. L -> L0)? Right? $\endgroup$
    – Student
    Nov 19, 2022 at 11:06
  • $\begingroup$ @Student The Manipulate variables you used in the sliders $L,m,a$ etc.. are not the same global variables $L,m,a$. Each is different context. So what you changes these inside Manipulate they have no effect on the global variables backed into your ode. That is why the changes are not reflected. A better way to do all this is to put all the code inside Manipulate. But for now, can do /. as I showed. $\endgroup$
    – Nasser
    Nov 19, 2022 at 18:26

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.