Short Version: I don't know if I worded the question wrong, I'm trying to loop an evaluation multiple times based on another variable.

I've been attempting to display Bifurcation of a Driven Pendulum, with the Formula

$$\ddot\phi+2\beta\dot\phi+\varpi^2\left(\phi-\frac16\phi^3\right)=\gamma\varpi^2\cos\omega t$$

and intended result coming out to bifurcation diagram I have a way to brute force a Bifurcation Diagram, using the code

β = 0.75*π; ω = 2 π; ϖ = 3 π; γ ="Number between 1.0600 and 1.0873";
soll = NDSolve[{ϕ''[t] + 2*β*ϕ'[t] + ϖ^2*Sin[ϕ[t]] == γ*ϖ^2*Cos[ω*t],
                ϕ[0] == -0.5 π, ϕ'[0] == 0}, {ϕ, ϕ'}, {t, 501, 600}];
Points = Table[Evaluate[{ϕ[t]} /. soll], {t, 501, 600}];
Export["DataAtPoints.xls", Flatten[Points], "Table"]

However, this only results in individual points of the solution at a single point gamma, and the desired result is one entire set of graphable points. I'm fairly new to Mathematica, so I may have missed something very basic, but since the for-and do loops aren't working with this, how would I loop this over several gammas?


1 Answer 1


First, we'll make your code into a function of the parameter gamma that you want to "loop over".

f[γ_] := Module[{}, β = 0.75*π; ω = 2 π; ϖ = 3 π; soll = NDSolve[{ϕ''[t] + 2*β*ϕ'[t] + ϖ^2* Sin[ϕ[t]] == γ*ϖ^2*Cos[ω*t], ϕ[0] == -0.5 π, ϕ'[0] == 0}, {ϕ, ϕ'}, {t, 501, 600}]; points = Table[Evaluate[{ϕ[t]} /. soll], {t, 501, 600}] // Flatten];

Now call (see Map, abbreviated with /@) this function for all the gamma values in a range, reformat, and plot

ran = Range[1.06, 1.0873, 0.0001];
dat = f[#] & /@ ran;
plotData = Flatten[Table[{ran[[i]], dat[[i, j]]}, {i, 1, Length[dat]}, 
    {j, 1, Length[dat[[i]]]}], 1];

enter image description here

  • $\begingroup$ You probably wanted to define the auxiliary variables local variables. Otherwise, why use Module? $\endgroup$
    – Felix
    Commented Apr 3, 2017 at 18:09
  • $\begingroup$ I wanted to change the function as little as possible. I just used Module to group everything. $\endgroup$
    – bill s
    Commented Apr 3, 2017 at 18:12

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.