0
|
9215 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
GROUPS:

Plotting DSolve outputs?

Posted 10 years ago
 I'm creating a template for differential equation solving. I'm trying to write a script that takes the output from the solution of the differential equation and write it as a new function to be used in Manipulate [ Plot[...] ]. However, when I do this, Plot will not recognize that I've set the output as a function. Can someone spot my mistake? Code: Eqn1 = D[f[t], {t, 2}] + \[Omega]^2*f[t] == 0; Sol1 = DSolve[Eqn1, f[t], t]; IC1 = f[0] == \[Alpha]; IC2 = (D[f[t], {t, 1}] == \[Gamma]) /. t -> 0; Sol2[t_] = Simplify[ DSolve[{Eqn1, IC1, IC2}, f[t], t] ]; x[t_] = Replace[ f[t], Sol2[t] ]; Manipulate[ Plot[ { x[t] }, { t, 0, 10 } (* Plot *)], 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"}, ControlPlacement -> Left, SaveDefinitions -> False, TrackedSymbols :> {\[Alpha], \[Gamma], \[Omega]} (* Manipulate *)] 
4 Replies
Sort By:
Posted 10 years ago
 I realized I made a terrible mistake from the very beginning. The variable replacement in Plot does work, but the easiest solution is to establish all variables to be used in Manipulate in the defined function; i.e.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 ];With this, Plot within the Manipulate command will run:Manipulate[  Plot[ { x[\[Alpha], \[Gamma], \[Omega], t] }, { t, 0, 10 } (* Plot *)], {{[Alpha], -10, Style["[Alpha]", 16, FontFamily -> "Times"]}, -10, 10, 0.01, ImageSize -> Medium, Appearance -> "Labeled"},{{[Gamma], 0, Style["[Gamma]", 16, FontFamily -> "Times"]}, -10, 10, 0.01, ImageSize -> Medium, Appearance -> "Labeled"},{{[Omega], 1.0, Style["[Omega] [rad. \!(*SuperscriptBox[(s), (-1)])]", 16, FontFamily -> "Times"]}, 0, 10, 0.01, ImageSize -> Medium, Appearance -> "Labeled"},ControlPlacement -> Left, SaveDefinitions -> False, TrackedSymbols :> {[Alpha], [Gamma], [Omega]}(* Manipulate *)]However, this output { x[[Alpha], [Gamma], [Omega], t] = Replace[ f[t], Sol2 ]; } does not work in Graphics[]. See below. Manipulate[ Graphics[{ { RGBColor[0, 0, 0.75], EdgeForm[ Directive[AbsoluteThickness[2.5], Dashed, RGBColor[0, 0, 0]]], Rectangle[ { (x[\[Alpha], \[Gamma], \[Omega], time]) + 5 , 0 }, { (x[\[Alpha], \[Gamma], \[Omega], time]) - 5 , 10 } ] } (* Graphics *) }], {{[Alpha], -10, Style["[Alpha]", 16, FontFamily -> "Times"]}, -10, 10, 0.01, ImageSize -> Medium, Appearance -> "Labeled"},{{[Gamma], 0, Style["[Gamma]", 16, FontFamily -> "Times"]}, -10, 10, 0.01, ImageSize -> Medium, Appearance -> "Labeled"},{{[Omega], 1.0, Style["[Omega] [rad. \!(*SuperscriptBox[(s), (-1)])]", 16, FontFamily -> "Times"]}, 0, 10, 0.01, ImageSize -> Medium, Appearance -> "Labeled"},{{time, 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], time}(* Manipulate *)]Does anyone have a recommendation for getting the function to work in Manipulate [ Graphics[....] ] ??? It doesn't make sense that defining the function from the output doesn't work, but defining the function explicitly does allow it to run in Graphics. See below. Manipulate[ Graphics[{ { RGBColor[0, 0, 0.75], EdgeForm[ Directive[AbsoluteThickness[2.5], Dashed, RGBColor[0, 0, 0]]], Rectangle[ { ( \[Alpha] Cos[time \[Omega]] + (\[Gamma] Sin[time \[Omega]])/\[Omega] ) + 5 , 0 }, { ( \[Alpha] Cos[time \[Omega]] + (\[Gamma] Sin[time \[Omega]])/\[Omega] ) - 5 , 10 } ] } (* Graphics *) }], {{[Alpha], -10, Style["[Alpha]", 16, FontFamily -> "Times"]}, -10, 10, 0.01, ImageSize -> Medium, Appearance -> "Labeled"},{{[Gamma], 0, Style["[Gamma]", 16, FontFamily -> "Times"]}, -10, 10, 0.01, ImageSize -> Medium, Appearance -> "Labeled"},{{[Omega], 1.0, Style["[Omega] [rad. \!(*SuperscriptBox[(s), (-1)])]", 16, FontFamily -> "Times"]}, 0, 10, 0.01, ImageSize -> Medium, Appearance -> "Labeled"},{{time, 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], time}(* Manipulate *)]
Posted 10 years ago
 Hi Tim,the reason is that variables controlled by Manipulate are local within Manipulate. To see this try: f[x_] := Sin[a x] Manipulate[a f[x], {x, 0, Pi}, {a, 1, 2}] Consequently the 'a' inside Sin and 'a' inside Manipulate are different variables.Cheers Henrik
Posted 10 years ago
 That definitely fixed the problem. Thank you! I have absolutely no idea why that variable replacement in the Plot command corrects the issue though. Can you offer any feedback as to why this is the solution? Is the issue with Plot or Manipulate?
Posted 10 years ago
 In the first part of the code you can replace the alphas, etc, by adding a 0 to the end: Eqn1 = D[f[t], {t, 2}] + \[Omega]0^2*f[t] == 0; Sol1 = DSolve[Eqn1, f[t], t]; IC1 = f[0] == \[Alpha]0; IC2 = (D[f[t], {t, 1}] == \[Gamma]0) /. t -> 0; Sol2[t_] = Simplify[DSolve[{Eqn1, IC1, IC2}, f[t], t]]; x[t_] = Replace[f[t], Sol2[t]]; Then this works: Manipulate[{Plot[ x[t] /. {\[Omega]0 -> \[Omega], \[Gamma]0 -> \[Gamma], \[Alpha]0 \ -> \[Alpha]} , {t, 0, 10}], x[1] /. {\[Omega]0 -> \[Omega], \[Gamma]0 -> \[Gamma], \[Alpha]0 -> \ \[Alpha]}}, {\[Omega], 1, 5, 1}, {\[Gamma], 1, 5, 1}, {\[Alpha], 1, 5, 1}] Should help you figure what was wrong.