Message Boards Message Boards

Function output used in Graphics[]....

Posted 10 years ago

I'm trying to plot the output of an equation in Graphics. However, Graphics does not appear to recognize the output; only explicitly writing the function into Graphics will work (see code below). Does anyone know a workaround for this?

Eqn1 = D[f[t], {t, 2}] + \[Omega]^2*f[t] == 0;

IC1 = f[0] == \[Alpha];

IC2 = (D[f[t], {t, 1}] == \[Gamma]) /. {  t -> 0  };

Sol2 = Simplify[  DSolve[{Eqn1, IC1, IC2}, f[t], t]  ];

x[\[Alpha]_, \[Gamma]_, \[Omega]_, t_] = Replace[  f[t], Sol2  ];


Manipulate[

  Graphics[{

    (* Frame *)
    {  
     Directive[AbsoluteThickness[5], RGBColor[0.25, 0.25, 0.25]],
     Line[{{-25, 0}, {50, 0}}],
     Line[{{-25, 0}, {-25, 15}}]
     },

    {
     RGBColor[0.25, 0.25, 0.75],
     AbsoluteThickness[2.5],
     Dashed, Line[{{0, 0}, {0, 15}}]
     },

    (* x(t) = 0 *)
    {  
     Inset[Style["x(t) = 0", 20, FontFamily -> "Times"], {0, -5}]  
     },

    (* Oscillating Masses *)
    {  
     RGBColor[0, 0, 0.75],
     EdgeForm[
      Directive[AbsoluteThickness[2.5], Dashed, RGBColor[0, 0, 0]]],
     Rectangle[  {  (x[\[Alpha], \[Gamma], \[Omega], tmax]) + 5 , 
       0  }, {  (x[\[Alpha], \[Gamma], \[Omega], tmax]) - 5 , 10  } ]
     },

    (* The Spring *)
    {
     ParametricPlot[  {  
        sex/(
          2 \[Pi])*((x[\[Alpha], \[Gamma], \[Omega], tmax]) + 25) - 
         25, 5 + 2.25*Sin[10*sex]  },

       {sex, 0, 2 \[Pi]},

       PlotStyle -> {
         Directive[RGBColor[0, 0, 0], AbsoluteThickness[2.5]]
         },

       Axes -> None][[1]]
     }

    (* Graphics *)}],

  Style["", Bold, 14, FontFamily -> "Times"],
  {{\[Alpha], -10, Style["\[Alpha]", 16, FontFamily -> "Times"]}, -10,
    10, 0.01, ImageSize -> Medium, Appearance -> "Labeled"},

  Style["", Bold, 14, FontFamily -> "Times"],
  {{\[Gamma], 0, Style["\[Gamma]", 16, FontFamily -> "Times"]}, -10, 
   10, 0.01, ImageSize -> Medium, Appearance -> "Labeled"},

  Style["", Bold, 14, FontFamily -> "Times"],
  {{\[Omega], 1.0, 
    Style["\[Omega] [rad. \!\(\*SuperscriptBox[\(s\), \(-1\)]\)]", 16,
      FontFamily -> "Times"]}, 0, 10, 0.01, ImageSize -> Medium, 
   Appearance -> "Labeled"},

  Style["Axis Parameters", Underlined, 14, FontFamily -> "Times"],
  {{tmax, 10^-20, Style["time", 16, FontFamily -> "Times"]}, 10^-20, 
   120, ControlType -> Trigger, DefaultDuration -> 120.0, 
   DisplayAllSteps -> True, AnimationRate -> 1.0, ImageSize -> Medium,
    Appearance -> "Labeled"},

  ControlPlacement -> Left,
  SaveDefinitions -> False,
  TrackedSymbols :> {\[Alpha], \[Gamma], \[Omega], tmax}

  (* Manipulate *)]


However, writing the function directly into Graphics does work.


Manipulate[

  Graphics[{

    (* Frame *)
    {  
     Directive[AbsoluteThickness[5], RGBColor[0.25, 0.25, 0.25]],
     Line[{{-25, 0}, {50, 0}}],
     Line[{{-25, 0}, {-25, 15}}]
     },

    {
     RGBColor[0.25, 0.25, 0.75],
     AbsoluteThickness[2.5],
     Dashed, Line[{{0, 0}, {0, 15}}]
     },

    (* x(t) = 0 *)
    {  
     Inset[Style["x(t) = 0", 20, FontFamily -> "Times"], {0, -5}]  
     },

    (* Oscillating Masses *)
    {  
     RGBColor[0, 0, 0.75],
     EdgeForm[
      Directive[AbsoluteThickness[2.5], Dashed, RGBColor[0, 0, 0]]],
     Rectangle[  {  (\[Alpha] Cos[tmax \[Omega]] + (\[Gamma] Sin[
            tmax \[Omega]])/\[Omega]) + 5 , 
       0  }, {  (\[Alpha] Cos[tmax \[Omega]] + (\[Gamma] Sin[
            tmax \[Omega]])/\[Omega]) - 5 , 10  } ]
     },

    (* The Spring *)
    {
     ParametricPlot[  {  
        sex/(2 \[Pi])*((\[Alpha] Cos[tmax \[Omega]] + (\[Gamma] Sin[
                tmax \[Omega]])/\[Omega]) + 25) - 25, 
        5 + 2.25*Sin[10*sex]  },

       {sex, 0, 2 \[Pi]},

       PlotStyle -> {
         Directive[RGBColor[0, 0, 0], AbsoluteThickness[2.5]]
         },

       Axes -> None][[1]]
     }

    (* Graphics *)}],

  Style["", Bold, 14, FontFamily -> "Times"],
  {{\[Alpha], -10, Style["\[Alpha]", 16, FontFamily -> "Times"]}, -10,
    10, 0.01, ImageSize -> Medium, Appearance -> "Labeled"},

  Style["", Bold, 14, FontFamily -> "Times"],
  {{\[Gamma], 0, Style["\[Gamma]", 16, FontFamily -> "Times"]}, -10, 
   10, 0.01, ImageSize -> Medium, Appearance -> "Labeled"},

  Style["", Bold, 14, FontFamily -> "Times"],
  {{\[Omega], 1.0, 
    Style["\[Omega] [rad. \!\(\*SuperscriptBox[\(s\), \(-1\)]\)]", 16,
      FontFamily -> "Times"]}, 0, 10, 0.01, ImageSize -> Medium, 
   Appearance -> "Labeled"},

  Style["Axis Parameters", Underlined, 14, FontFamily -> "Times"],
  {{tmax, 10^-20, Style["time", 16, FontFamily -> "Times"]}, 10^-20, 
   120, ControlType -> Trigger, DefaultDuration -> 120.0, 
   DisplayAllSteps -> True, AnimationRate -> 1.0, ImageSize -> Medium,
    Appearance -> "Labeled"},

  ControlPlacement -> Left,
  SaveDefinitions -> False,
  TrackedSymbols :> {\[Alpha], \[Gamma], \[Omega], tmax}

  (* Manipulate *)]

Any suggestions for using the output in Graphics?

POSTED BY: Tim Kirkpatrick
5 Replies

Actually, on my system it is the first version (with functions on the outside) that does not work, but it is easy to fix: just write

Sol2 = Simplify[First@DSolve[{Eqn1, IC1, IC2}, f[t], t]]

instead of

Sol2 = Simplify[DSolve[{Eqn1, IC1, IC2}, f[t], t]]

A part from this, they both show an oscillating mass attached to a spring. At first sight I can't detect a difference or anything wrong.

POSTED BY: Gianluca Gorni

It removes the extra pair of brackets {} that encloses the solutions. With those brackets the x[] becomes of the form {number}, which messes up the coordinates in

 Rectangle[  {  (x[...]) + 5 , 
       0  }, {  (x[...]) - 5 , 10  } ]

In general, DSolve gives solutions in the form {{solution}}. In your case you only want {solution}.

POSTED BY: Gianluca Gorni

It would be easier to help you if you can generate a simpler example of this problem.

POSTED BY: Frank Kampas

Yes, that fixed it! Thank you. What does the "First@" in front of DSolve do?

POSTED BY: Tim Kirkpatrick

Thank you.

POSTED BY: Tim Kirkpatrick
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract