I want to dynamically plot the Kuramoto model with the parameter K as an adjustable slider
\dot theta_i = omega_i + K/N \sum_j sin(theta_j - theta_i)
For fixed K, I can plot the results dynamically. But how can I add a Manipulate over this, so that I can dynamically change K? Thanks!
(*Preamble*)
{dt, n, T} = {0.5, 200, 1000};
k = 1.5;
omega = Sort[RandomReal[{-1, 1}, n]];
theta0 = RandomReal[{0, 2 \[Pi]}, n];
results = {};
Dynamic[results] (*For dynamic plotting*)
{t, NT} = {0, Floor[T/dt]} ;
(*Main sim*)
indices = Table[i, {i, 1, n}];
While[t < NT,
thetaNext = euler[theta0, dt, F, omega, k];
theta0 = thetaNext; t++;
results = plotResults[indices, thetaNext, k]
]
(*Utils / methods*)
euler[theta_, dt_, F_, omega_, k_] := Block[{thetaNext},
thetaNext = theta + dt F[theta, omega, k]
];
F[theta_, omega_, k_] := Block[{Z, thetaNext},
Z = Mean[Exp[I theta]];
thetaNext = omega + Im[k Exp[-I theta]*Z];
Return[thetaNext]
];
plotResults[is_, \[Theta]_, k_] :=
ListPlot[Transpose[{is, Mod[\[Theta], 2 \[Pi]]}],
PlotRange -> {0, 2 \[Pi]}, AxesLabel -> {"index", "\[Theta]"},
LabelStyle -> 15, PlotLabel -> "K = " <> ToString[k]]