1
$\begingroup$

I'm trying to debug this example code from a textbook:

Manipulate[
  (*Evaluate Eq.(8.2)*)
  hh = 1/Sqrt[(1 - (r Ω0) ˆ2) ˆ2 + (2 ζ r Ω0) ˆ2];
  thh = ArcTan[1 - (r Ω0) ˆ2, 2 ζ r Ω0];
  (*Obtain coefficients from either Eq.(8.5) or Eq.(8.6)*)
  cnn = 
    If[ptyp == 1, 
      Abs[Sin[r π α]/(r π α)], 
      a1 = (2 Sin[2 π α r] - Sin[4 π α r])/(π r);
      b1 = (1 - 2 Cos[2 π α r] + Cos[4 π α r])/(π r);
      Sqrt[a1ˆ2 + b1ˆ2]];
  psnn = 
    If[ptyp == 1, 
      ArcTan[1 - Cos[2 r π α], Sin[2 r π α]], 
      ArcTan[b1, a1]];
  ptin = Table[{n, cnn[[n]]}, {n, 1, nn}];
  ptout = Table[{n, hh[[n]] , cnn[[n]]}, {n, 1, nn}];
    lines = 
      Table[
        {{n, 0}, {n, If[cnn[[n]] < hh[[n]] cnn[[n]], hh[[n]] cnn[[n]], cnn[[n]]]}}, 
        {n, 1, nn}];
  (*Sum series:Eq.(8.1)*)
  xt = 
    If[ptyp == 1, 
      α + 2 α Total[cnn hh Sin[r Ω0 t - thh + psnn]], 
      Total[cnn hh Sin[r Ω0 t - thh + psnn]]];
  (*Coordinates to draw pulse*)
  If[ptyp == 1, 
    pulse1 = {{0, 0}, {0, 1}, {2 π α/Ω0, 1}, {2 π α/Ω0, 0}};
    pulse2 = {{2 π/Ω0, 0}, {2 π/Ω0, 1}, {π α/Ω0 + 2 π/Ω0, 1}}, 
    pulse1 = 
      {{0, 0}, {0, 1}, {2 π α/Ω0, 1}, {2 π α/Ω0, -1}, {4 π α/Ω0, -1}, 
       {4 π α/Ω0, 0}};
    pulse2 = {{2 π/Ω0, 0}, {2 π/Ω0, 1}, {2 π α/Ω0 + 2 π/Ω0, 1}}];
  (*Create two graphs,one above the other*)
  GraphicsColumn[
    {Plot[xt, {t, -π/Ω0/5., 2 π/Ω0 + π α/Ω0}, 
       PlotStyle -> {Red}, 
       PlotRange -> {Full, {-2, 2.3}}, 
       PlotLabel -> label1, 
       AxesLabel -> {"τ", "Amplitude"}, 
       Epilog -> {{Blue, Line[pulse1]}, {Blue, Line[pulse2]}}], 
     ListLinePlot[lines, PlotStyle -> Black, 
         PlotRange -> {{0, nn + 1}, Full}, 
         PlotLabel -> label2, 
         AxesLabel -> {"n=Ωn/Ω0", "Magnitude"},
         Epilog -> 
           {{Blue, PointSize[Medium], Point[ptin]}, 
            {Red, PointSize[Medium], Point[ptout]}}]}],
  (*Create sliders and radio buttons*)
  Style["Periodic Waveform", Bold], 
  {{ptyp, 1, " "}, {1 -> labs, 2 -> labd}, ControlType -> RadioButton}, 
  Delimiter, 
  Style["Input Parameters", Bold], 
  {{Ω0, 0.04, "Ωo"}, 0.01, 1, 0.01, Appearance -> "Labeled", ControlType -> Slider}, 
  {{α, 0.2, la}, 0.02, 0.49, 0.01, Appearance -> "Labeled", ControlType -> Slider}, 
  Delimiter, 
  Style["Damping Factor", Bold], 
  {{ζ, 0.1, "ζ"}, 0.02, 0.7,0.01, Appearance -> "Labeled", ControlType -> Slider}, 
  Delimiter, 
  Style["Frequency Spectrum -", Bold], 
  Style[" Maximum Number of Harmonics Displayed", Bold], 
  {{nn, 20, "N"}, 1, 50, 1, Appearance -> "Labeled", ControlType -> Slider}, 
  ControlPlacement -> Left, 
  Initialization :> (
    puls = {{0, 0}, {0, 1}, {0.25, 1}, {0.25, 0}, {1, 0}, {1, 1}, {1.1, 1}};
    (*Radio button images*)
    labs = 
      ListLinePlot[puls, 
        PlotRange -> {{0, 1.2}, {-0.1, 1}}, 
        Axes -> False, 
        ImageSize -> Tiny, 
        Epilog -> 
          {Arrowheads[0.1], 
           Arrow[{{0, 0.5}, {1, 0.5}}], 
           Arrow[{{1, 0.5}, {0, 0.5}}], 
           Inset[Style["2π/Ω0", 14], {0.5, 0.65}], 
           Inset[Style["τd", 14], {0.125, 0.1}]}];
     puld = 
       {{0, 0}, {0, 1}, {0.15, 1}, {0.15, -1}, {0.3, -1}, 
        {0.3, 0}, {1, 0}, {1, 1}, {1.1, 1}};
     labd = 
       ListLinePlot[puld, 
         PlotRange -> {{0, 1.2}, {-1.1, 1}}, 
         Axes -> False, 
         ImageSize -> Tiny, 
         Epilog -> 
           {Arrowheads[{-0.1, 0.1}], 
            Arrow[{{0, 0.5}, {1, 0.5}}], 
            Inset[Style["2π/Ω0", 14], {0.5, 0.75}], 
            Inset[Style["τd", 14], {0.075, 0}]}];
     (*Figure titles*)
     label1 = 
       Column[
        {"Time Domain Waveforms", 
         Row[{Style["Input, ", Blue], Style["Output ", Red]}]}, 
        Center];
     label2 = 
       Column[
         {"Frequency (Harmonic) Spectrum", 
          Row[{Style[Row[{"Input cn"}], Blue], 
          Style[Row[{" Output [cnH(Ωn)]"}], Red]}]}, 
         Center];
     (*Slider label*)
     la = "α=Ωoτd/(2π)"; 
     r = Range[1, 150];), 
  TrackedSymbols :> {Ω0, α, ζ, nn, ptyp}]

The purpose for this code is to represent Periodic Force on a Single Degree-of-Freedom System.

Among this huge block, there is an issue with the plot elements representing this engineering problem. There is an error that says the following:

Coordinate {1, (0.008 $CellContext`ˆ2 + (1 - 0.04 $CellContextˆ2) $CellContextˆ2)^Rational[-1, 2], 0.9354892837886392} should be a pair of numbers, or a Scaled or Offset form.

Can anyone help me find the bug that's causing the titles in the graph figures to not come out properly?

$\endgroup$
  • $\begingroup$ That’s a lot of code to wade through! What steps have you taken to debug it yourself? You are asking somebody to do a lot of work on your behalf which, once done, will be of no benefit to anyone but you, since it’s highly unlikely that another user will have the same problem with the same code again in the future... $\endgroup$ – MarcoB Dec 22 '19 at 4:59
  • 3
    $\begingroup$ Take a look at ptout. It has 3 coordinates, but it should have only 2. Also, some of your exponent symbols (^) are not the right character. The clue is that the "2" that follows them is blue instead of black. Also, the wrong characters are smaller than the correct one. $\endgroup$ – LouisB Dec 22 '19 at 9:12
1
$\begingroup$
Manipulate[
  hh = 1/Sqrt[(1 - (r Ω0)^2)^2 + (2 ζ r Ω0)^2];
  thh = ArcTan[1 - (r Ω0)^2, 2 ζ r Ω0];
  If[ptyp == 1,
    cnn = Abs[Sin[r π α]/(r π α)];
    psnn = ArcTan[1 - Cos[2 r π α], Sin[2 r π α]],
    a1 = (2 Sin[2 π α r] - Sin[4 π α r])/(π r);
    b1 = (1 - 2 Cos[2 π α r] + Cos[4 π α r])/(π  r);
    cnn = Sqrt[a1^2 + b1^2];
    psnn = (Quiet @ ArcTan[b1, a1] /. Indeterminate -> 0.)];
  ptin = Table[{n, cnn[[n]]}, {n, 1, nn}];
  ptout = Table[{n, hh[[n]]}, {n, 1, nn}];
  lines =
    Table[
     {{n, 0},
      {n, If[cnn[[n]] < hh[[n]] cnn[[n]], hh[[n]] cnn[[n]], cnn[[n]]]}},
   {n, 1, nn}];
  xt =
    If[ptyp == 1,
      α + 2 α Total[cnn hh Sin[r Ω0 t - thh + psnn]],
      Total[cnn hh Sin[r Ω0 t - thh + psnn]]];
  If[ptyp == 1,
    pulse1 = {{0, 0}, {0, 1}, {2 π α/Ω0, 1}, {2 π α/Ω0, 0}};
    pulse2 = {{2 π/Ω0, 0}, {2 π/Ω0, 1}, {π α/Ω0 + 2 π/Ω0, 1}}, 
    pulse1 = 
      {{0, 0}, {0, 1}, {2 π α/Ω0, 1}, {2 π α/Ω0, -1},
       {4 π α/Ω0, -1}, {4 π α/Ω0, 0}};
    pulse2 = {{2 π/Ω0, 0}, {2 π/Ω0, 1}, {2 π α/Ω0 + 2 π/Ω0, 1}}];

  GraphicsColumn[
    {Plot[xt, {t, -π/Ω0/5., 2 π/Ω0 + π α/Ω0},
       PlotStyle -> {Red},
       PlotRange -> {Full, {-2, 2.3}},
       PlotLabel -> label1,
       AxesLabel -> {"τ", "Amplitude"},
       Epilog -> {{Blue, Line[pulse1]}, {Blue, Line[pulse2]}}],
     ListLinePlot[lines,
       PlotStyle -> Black,
       PlotRange -> {{0, nn + 1}, Full},
       PlotLabel -> label2,
       AxesLabel -> {"n=Ωn/Ω0", "Magnitude"},
       Epilog ->
         {{Blue, PointSize[Medium], Point[ptin]},
          {Red, PointSize[Medium], Point[ptout]}}]}],

  Style["Periodic Waveform", Bold],
  {{ptyp, 1, " "}, {1 -> labs, 2 -> labd}, RadioButton},
  Delimiter,
  Style["Input Parameters", Bold], 
  {{Ω0, 0.04, "Ωo"}, 0.01, 1, 0.01, Slider, Appearance -> "Labeled"}, 
  {{α, 0.2, la}, 0.02, 0.49, 0.01, Slider, Appearance -> "Labeled"},
  Delimiter,
  Style["Damping Factor", Bold],
  {{ζ, 0.1, "ζ"}, 0.02, 0.7, 0.01, Slider, Appearance -> "Labeled"},
  Delimiter,
  Style["Frequency Spectrum -", Bold],
  Style[" Maximum Number of Harmonics Displayed", Bold], 
  {{nn, 20, "N"}, 1, 50, 1, Slider, Appearance -> "Labeled"},
  ControlPlacement -> Left,

  Initialization :> (
    puls =
      {{0, 0}, {0, 1}, {0.25, 1}, {0.25, 0}, {1, 0}, {1, 1}, {1.1, 1}};
    labs =
      ListLinePlot[puls,
        PlotRange -> {{0, 1.2}, {-0.1, 1}},
        Axes -> False,
        ImageSize -> Tiny,
        Epilog ->
          {Arrowheads[0.1],
           Arrow[{{0, 0.5}, {1, 0.5}}],
           Arrow[{{1, 0.5}, {0, 0.5}}],
           Inset[Style["2π/Ω0", 14], {0.5, 0.65}],
           Inset[Style["τd", 14], {0.125, 0.1}]}];
    puld =
      {{0, 0}, {0, 1}, {0.15, 1}, {0.15, -1}, {0.3, -1}, 
       {0.3, 0}, {1, 0}, {1, 1}, {1.1, 1}};
    labd =
      ListLinePlot[puld,
        PlotRange -> {{0, 1.2}, {-1.1, 1}},
        Axes -> False,
        ImageSize -> Tiny,
        Epilog ->
          {Arrowheads[{-0.1, 0.1}],
           Arrow[{{0, 0.5}, {1, 0.5}}],
           Inset[Style["2π/Ω0", 14], {0.5, 0.75}],
           Inset[Style["τd", 14], {0.075, 0}]}];
    label1 =
      Column[
        {"Time Domain Waveforms",
         Row[{Style["Input, ", Blue], Style["Output ", Red]}]},
        Center];
    label2 =
      Column[
        {"Frequency (Harmonic) Spectrum",
         Row[
           {Style[Row[{"Input cn"}], Blue], 
            Style[Row[{" Output [cnH(Ωn)]"}], Red]}]},
        Center];
    la = "α=Ωoτd/(2π)";
    r = Range[1, 150]),

  TrackedSymbols :> {Ω0, α, ζ, nn, ptyp}]

demo

Notes

  1. Replaced bad unicode characters in the expressions for hh and the with ^.
  2. Rewrote expression fro pout to produce array of pairs rather than triples. This suppresses the error message that the OP reported.
  3. Revised the calculation of psnn so it doesn't complain about indeterminate values returned by ArcTan.
|improve this answer|||||
$\endgroup$
  • $\begingroup$ I have learned my lesson: Don't copy and paste code off a pdf $\endgroup$ – TexMexDex Dec 23 '19 at 1:26

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