1
$\begingroup$

edit edit: In response to @Bill's comment below, I fixed my typo, and realized that instead of nesting potentially nebulous function calls inside one another, I could just combine the functions in order to get the functionality I need. Ty Bill

edit^3: to any potential post-viewers, the sole reason the statement with xplusa[...] seems not to work is because Block[] is inside Manipulate[]. Im not sure why this is a problem, but Mathematica seems unable to properly deal with it

I'm attempting to plot a fairly simple function of multiple sines and cosines, as well as exponentials. I want this plot to be manipulatable based on multiple different parameters - notable, since it is a coupled harmonic oscillator, the mass, spring tension, and coupling strength. I've been able to do a very simple Block[{},Manipulate[Plot[...]...]...] scheme that has worked for only the simplest representation of the model so far, but when getting into the more complex workings, mathematica doesn't seem to want to plot it.

I've been able to plot the expression below fairly well, even though it depends on a parameter that is a dependent variable. I do this by Block[]-ing the dependent and independent variables, and setting LocalizeVariables -> off ex.

Block[{a, m, k, κ, Omegao}, Manipulate[
  Column[{
    Plot[xplus[a, Omegao, t], {t, .01, 1}, ImageSize -> Large],
     Grid[{{"k", k}, {"Kappa", κ}, {"m", m}, {"Omegao", 
       Omegao}, {"a", a}}, Alignment -> Center]}, 
   Alignment -> Center], {a, 1, 10}, {k, 100, 10000}, {κ, 1, 
   10}, {m, 1, 100}, LocalizeVariables -> False]]

This is the expression I'm trying to plot (that is still simpler than what I would like to plot)

Block[{k, κ, m, deltak = 0, ωdrive = 0, 
  A = 0, γ = 0, Omegao, Omegad, 
  Omegac, Ωplus, Ωminus, 
  deltaΩ, ωd, δ, ΩR, ao, 
  bo}, Manipulate[
  Column[{
    Plot[{xplustotal[0, 0, 0, ao_, k_, κ_, 0, Omegao_, t_, 
       0]}, {t, .01, 1}, ImageSize -> Large, PlotRange -> 2],
    Grid[{{"k", k}, {"κ", κ}, {"mass", m}, {"Omega_o", 
       Omegao}, {"ao", ao}}, Alignment -> Center],
    Alignment -> Center}],
  {κ, 10, 100}, {m, 1, 100}, {k, 100, 10000}, {ao, 0, 1}, 
  LocalizeVariables -> False]]

The variables that it depends on are these:

Omegao = Sqrt[(k + κ)/m]
Omegad = Sqrt[deltak/m]
Omegac = Sqrt[κ/m]Ωplus = Sqrt[Omegao^2 - Sqrt[Omegad^4 + Omegac^4]]

Ωminus = Sqrt[Omegao^2 + Sqrt[Omegad^4 + Omegac^4]]
deltaΩ = Omegac^2/Omegao
ωd = Omegad^2/Omegao
δ = deltaΩ - ωdrive
ΩR = Sqrt[A^2 + δ^2]

xplus[a_, Omegao_, t_] := Re[a*E^(I*Omegao*t)]
xminus[b_, Omegao_, t_] := Re[b*E^(I*Omegao*t)]

a[abar_, ωdrive_, t_] := abar*E^(-I*ωdrive*t/2)
b[bbar_, ωdrive_, t_] := bbar*E^(I*ωdrive*t/2)

abar[A_, ωdrive_, t_, bo_, ao_, 
  k_, κ_] := (I*(A/ΩR) Sin[ΩR*
       t/2] bo + (Cos[ΩR*t/2] - 
       I*(δ/ΩR) Sin[ΩR*
          t/2]) ao) E^(-γ*t/2)
bbar[A_, ωdrive_, t_, bo_, ao_, 
  k_, κ_] := (I*(A/ΩR) Sin[ΩR*
       t/2] ao + (Cos[ΩR*t/2] + 
       I*(δ/ΩR) Sin[ΩR*
          t/2]) bo) E^(-γ*t/2)

xplustotal[A_, bo_, ao_, k_, κ_, ωdrive_, Omegao_, 
  t_, γ_] := 
 Re[(I*(A/ΩR) Sin[ΩR*
        t/2] bo + (Cos[ΩR*t/2] - 
        I*(δ/ΩR) Sin[ΩR*
           t/2]) ao) E^(-γ*t/2)*E^(-I*ωdrive*t/2)*
   E^(I*Omegao*t)]

xminustotal[A_, ωdrive_, bo_, ao_, 
  k_, κ_, ωdrive_, Omegao_, t_, γ_] := 
 Re[ (I*(A/ΩR) Sin[ΩR*
        t/2] ao + (Cos[ΩR*t/2] + 
        I*(δ/ΩR) Sin[ΩR*
           t/2]) bo) E^(-γ*t/2)*E^(I*ωdrive*t/2)*
   E^(I*Omegao*t)]

I've tried going through it step by step, and It seems that Mathematica isn't interested in plotting this - although I think its probably that I forgot to define something. I've tried to do the simplest possible option, which is plotting without driving (A = 0, wdrive = 0, deltak = 0, gamma = 0).

I've also tried the simpler expression

Manipulate[
 Block[{k, κ, m, deltak = 0, ωdrive = 0, 
   A = 0, γ = 0, Omegao, Omegad, 
   Omegac, Ωplus, Ωminus, 
   deltaΩ, ωd, δ, ΩR, ao, 
   bo},
  Column[{
    Plot[{xplusa[1, 0, Omegao_, t_]}, {t, .01, 1}, 
     ImageSize -> Large, PlotRange -> 2], 
    Grid[{{"k", k}, {"κ", κ}, {"mass", m}, {"Omega_o", 
       Omegao}, {"ao", a}}, Alignment -> Center]},
   Alignment -> Center]],
 {κ, 10, 100}, {m, 1, 100}, {k, 100, 10000}, 
 LocalizeVariables -> False]

corresponding to the equation

    xplusa[abar_, ωdrive_, Omegao_, t_] := 
 Re[abar*E^(-I*ωdrive*t/2)*E^(I*Omegao*t)]

and I still get a blank plot. I've basically specified all of the constants mathematica should need to plot this thing, and the scoping seems to be appropriate. I've also noticed that Mathematica doesn't seem to plot at all if Block[] is inside manipulate.

My end goal is to have Delta-k be a sinusoidal function, and be able to show flipping between Omega-plus and Omega-minus.

edit:

I'd really like to be be able to plot this guy:

Block[{k, κ, m, deltak = 0, ωdrive = 0, 
  A = 0, γ = 0, Omegao, Omegad, 
  Omegac, Ωplus, Ωminus, 
  deltaΩ, ωd, δ, ΩR, ao, 
  bo}, Manipulate[
  Column[{
    Plot[{xplus[a[abar[0, 0, t, ao, bo, κ, k], 0, t], Omegao, 
        t] + xminus[b[bbar[0, 0, t, ao, bo, κ, k], 0, t], 
        Omegao, t]}, {t, .01, 1}, ImageSize -> Large, PlotRange -> 2],
     Grid[{{"k", k}, {"κ", κ}, {"m", m}, {"Omegao", 
       Omegao}, {"a", ao}, {"b", bo}}, Alignment -> Center]}, 
   Alignment -> Center], {k, 100, 10000}, {κ, 10, 100}, {m, 1, 
   100}, {bo, 0, 1 - a}, {ao, 0, 1 - b}, LocalizeVariables -> False]]
$\endgroup$
  • 1
    $\begingroup$ In your second block of code inside your Manipulate you have xplustotal[0, 0, 0, ao_, k_, \[Kappa]_, 0, Omegao_, t_, 0] and those _ are wrong and must be removed. Next you have 10 arguments in that function use, but you have only 9 arguments in your definition of that function in your third block of code. $\endgroup$ – Bill May 25 '18 at 19:03
  • $\begingroup$ @Bill thank you :) $\endgroup$ – Brandon May 25 '18 at 19:13
  • $\begingroup$ Even when I remove the _ and guess I should remove the first 0 in the argument list it looks like it isn't plotting anything, but this seems to be because your xplustotal function is returning zero and isn't visible plotted over the top of the x axis. You might also check whether user defined functions must be defined inside a Manipulate rather than outside. I think I have seen problems with things like this. $\endgroup$ – Bill May 25 '18 at 19:25
  • $\begingroup$ in Manipulate, ao and bo start at 0. using this function call xplustotal[0, bo, ao, k, \[Kappa], 0, Omegao, t, 0] I was able to get it to work. Also I believe there was one more error in the main function declaration. Many thanks friend $\endgroup$ – Brandon May 25 '18 at 19:30
  • $\begingroup$ Welcome to the Mathematica Stack Exchange. If your problem is solved, please consider accepting the answer that is the best solution. Accepting an answer often gives both you and the person who helped you a gain in reputation. $\endgroup$ – creidhne Jul 19 '18 at 21:15
1
$\begingroup$

Lets start from this

deltak = 0; ωdrive = 0; A = 0; γ = 0; \
aButNotTheFunctionNameda = 1/2; bButNotTheFunctionNamedb = 1/3;
Manipulate[
 Omegao = Sqrt[(k + κ)/m];
 Omegad = Sqrt[deltak/m];
 Omegac = Sqrt[κ/m];
 Ωplus = Sqrt[Omegao^2 - Sqrt[Omegad^4 + Omegac^4]];
 Ωminus = Sqrt[Omegao^2 + Sqrt[Omegad^4 + Omegac^4]]; 
 deltaΩ = Omegac^2/Omegao;
 ωd = Omegad^2/Omegao;
 δ = deltaΩ - ωdrive;
 ΩR = Sqrt[A^2 + δ^2]; 
 xplus[a_, Omegao_, t_] := Re[a*E^(I*Omegao*t)]; 
 xminus[b_, Omegao_, t_] := Re[b*E^(I*Omegao*t)]; 
 a[abar_, ωdrive_, t_] := abar*E^(-I*ωdrive*t/2); 
 b[bbar_, ωdrive_, t_] := bbar*E^(I*ωdrive*t/2); 
 abar[A_, ωdrive_, t_, bo_, ao_, k_, κ_] :=
  (I*(A/ΩR) Sin[ΩR*t/2] bo +
  (Cos[ΩR*t/2] - I*(δ/ΩR)*
  Sin[ΩR*t/2]) ao) E^(-γ*t/2); 
 bbar[A_, ωdrive_, t_, bo_, ao_, k_, κ_] :=
  (I*(A/ΩR) Sin[ΩR*t/2] ao +
  (Cos[ΩR*t/2] + I*(δ/ΩR)*
  Sin[ΩR*t/2]) bo) E^(-γ*t/2); 
 xplustotal[A_, bo_, ao_, k_, κ_, ωdrive_, Omegao_, t_, γ_] := 
  Re[(I*(A/ΩR) Sin[ΩR*t/2] bo +
  (Cos[ΩR*t/2] - I*(δ/ΩR)*
  Sin[ΩR*t/2]) ao) E^(-γ*t/2)*
  E^(-I*ωdrive*t/2)*E^(I*Omegao*t)]; 
 xminustotal[A_, ωdrive_, bo_, ao_, k_, κ_, ωdrive_,
  Omegao_, t_, γ_] := 
  Re[(I*(A/ΩR) Sin[ΩR*t/2] ao +
  (Cos[ΩR*t/2] + I*(δ/ΩR)*
  Sin[ΩR*t/2]) bo) E^(-γ*t/2)*
  E^(I*ωdrive*t/2)*E^(I*Omegao*t)];
 Column[{
   Plot[
    xplus[ a[abar[0, 0, t, ao, bo, κ, k], 0, t], Omegao, t] + 
    xminus[b[bbar[0, 0, t, ao, bo, κ, k], 0, t], Omegao, t],
    {t, .01, 1}, ImageSize -> Large, PlotRange -> 2], 
   Grid[{{"k", k}, {"κ", κ}, {"m", m}, {"Omegao", Omegao},
      {"a", ao}, {"b", bo}}, Alignment -> Center]}, 
   Alignment -> Center], {k, 100, 10000}, {κ, 10, 100}, {m, 1, 100},
    {bo, 0, 1 - aButNotTheFunctionNameda}, {ao, 0, 1 - bButNotTheFunctionNamedb}]

There are little changes in that from what you wrote. But it does appear to plot. If you are going to make more changes then do one little change at a time, make certain that it works exactly the same way and then go on to the next change.

If there are mare mistakes in what I've done then please try to point out what they are and I will try to correct them. Please test this carefully before you depend on it.

$\endgroup$

Your Answer

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

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