Re: Manipulate a Plot of Evaluate DSolve
- To: mathgroup at smc.vnet.net
- Subject: [mg127523] Re: Manipulate a Plot of Evaluate DSolve
- From: Bob Hanlon <hanlonr357 at gmail.com>
- Date: Wed, 1 Aug 2012 04:56:31 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-newout@smc.vnet.net
- Delivered-to: mathgroup-newsend@smc.vnet.net
- References: <20120729070521.C7CB3684D@smc.vnet.net>
Your diff eqn can be solved exactly with DSolve. Why use NDSolve? ClearAll["Global`*"]; func[coef_, c_, t_] = (y[t] /. DSolve[{y'[t] == Cos[coef*t], y[0] == c}, y[t], t][[1]]) (c*coef + Sin[coef*t])/coef Manipulate[ Plot[Evaluate[{n0*Sin[coef*x + time], -H2, H1, Table[func[coef, c, x], {c, -5, -1}]}], {x, -XMax, XMax}, Frame -> True, Axes -> False, FrameLabel -> {"X", "Y"}, Filling -> {1 -> {{2}, LightBlue}}, PlotRange -> {-H2, H1}, PlotLabel -> "Floating object in Sea Wave Model"], {{time, 0, "Time"}, 0, 10 Pi, 0.1, Appearance -> "Labeled", ImageSize -> Tiny}, {{coef, 1, "Wave Frequency"}, 0.1, 5, Appearance -> "Labeled", ImageSize -> Tiny}, {{n0, 1, "Wave Amplitude"}, 0.1, 5, Appearance -> "Labeled", ImageSize -> Tiny}, {{H1, 3, "Air Height"}, 1, 10, Appearance -> "Labeled", ImageSize -> Tiny}, {{H2, 10, "Water Depth"}, 1, 50, Appearance -> "Labeled", ImageSize -> Tiny}, {{XMax, 10}, 1, 50, Appearance -> "Labeled", ImageSize -> Tiny}, ControlPlacement -> Right] Bob Hanlon On Tue, Jul 31, 2012 at 5:34 PM, Juan Barandiaran <barandiaran.juan at gmail.com> wrote: > Thanks again Bob, > > It looks a promising approach as it works and has the differential equation > outside the Manipulate and Plot blocks. > The only problem that I find now is that this solution is incredibly slow as > soon as you include it in a list of Plots. > I'll give a simple example, of the model I'm building of some objects moving > in the sea waves. > > First a simple user interface taking advantage of the great options we have > in Manipulate and Plot: > > ClearAll["Global`*"]; > > Manipulate[ > Plot[ > {n0*Sin[coef*x + time], -H2, H1, > n0*Sin[coef*x + time] + c /. c -> Range[-5, -1]}, > {x, -XMax, XMax}, AxesLabel -> {"X", "Y"}, > Filling -> {1 -> {{2}, LightBlue}}, PlotRange -> {-H2, H1}, > AspectRatio -> Automatic, > PlotLabel -> "Floating object in Sea Wave Model"], > {{time, 0, "Time"}, 0, 5*2*Pi, > Appearance -> "Labeled"}, {{coef , 1, "Wave Frequency"}, 0.1, 5, > Appearance -> "Labeled"}, {{n0, 0.5, "Wave Amplitude"}, 0.1, 5, > Appearance -> "Labeled"}, {{H1, 3, "Air Height"}, 1, 10, > Appearance -> "Labeled"}, {{H2, 10, "Water Depth"}, 1, 50, > Appearance -> "Labeled"}, {{XMax, 10}, 1, 50, > Appearance -> "Labeled"}, ControlPlacement -> Right] > > If you move the sliders of Manipulate it works fast and is responsive. > > Then we add a simple differential equation with the proposed solution: > > func[coef_?NumericQ, c_?NumericQ, > xLim_?NumericQ] := (y[t] /. > NDSolve[{y'[t] == Cos[coef*t], y[0] == c}, y, {t, -xLim, xLim}][[ > 1]]) > > Manipulate[ > Plot[Evaluate[ > Table[func[coef, c, 10] /. t -> x, {c, -5, -1}]], {x, -10, 10}, > PlotRange -> {-10, 5}], {{coef, 1}, 0.1, 5, 0.01, > Appearance -> "Labeled"}] > > It continues to work fast. So far so good. > > Now we try to integrate both things in a Manipulate block (or a Plot, I > don't think the problem is with manipulate): > > Manipulate[ > Plot[ > {n0*Sin[coef*x + time], -H2, H1, > Evaluate[Table[func[coef, c, XMax] /. t -> x, {c, -5, -1}]]}, > {x, -XMax, XMax}, AxesLabel -> {"X", "Y"}, > Filling -> {1 -> {{2}, LightBlue}}, PlotRange -> {-H2, H1}, > AspectRatio -> Automatic, > PlotLabel -> "Floating object in Sea Wave Model"], > {{time, 0, "Time"}, 0, 5*2*Pi, > Appearance -> "Labeled"}, {{coef , 1, "Wave Frequency"}, 0.1, 5, > Appearance -> "Labeled"}, {{n0, 1, "Wave Amplitude"}, 0.1, 5, > Appearance -> "Labeled"}, {{H1, 3, "Air Height"}, 1, 10, > Appearance -> "Labeled"}, {{H2, 10, "Water Depth"}, 1, 50, > Appearance -> "Labeled"}, {{XMax, 10}, 1, 50, > Appearance -> "Labeled"}, ControlPlacement -> Right] > > And at least in my system, the interface becomes incredibly slow... WHY ???? > Any workaround? Is it necessary to use a Table? > > Thanks once more and best regards, > > Juan > > > > 2012/7/31 Bob Hanlon <hanlonr357 at gmail.com> >> >> Not sure what happened. Try this >> >> Clear[func] >> >> func[coef_?NumericQ, c_?NumericQ] := >> (y[t] /. NDSolve[ >> {y'[t] == Cos[coef*t], y[0] == c}, >> y, {t, -10, 10}][[1]]) >> >> Manipulate[ >> Plot[ >> Evaluate[ >> Table[ >> func[coef, c] /. t -> x, >> {c, 5}]], >> {x, -10, 10}, >> PlotRange -> {-5, 10}], >> {{coef, 1}, 0.1, 5, 0.01, >> Appearance -> "Labeled"}] >> >> >> Bob Hanlon >> >> >> On Mon, Jul 30, 2012 at 9:17 PM, Juan Barandiaran >> <barandiaran.juan at gmail.com> wrote: >> > Thanks Bob, >> > >> > Yes, I could use NDSolve instead of DSolve, in my case I'm only getting >> > numerical outputs, so it should do the job. >> > >> > But in the example that you send me, why is nothing plotted? >> > >> > Thanks again and best regards, >> > >> > Juan >> > >> > >> > 2012/7/30 Bob Hanlon <hanlonr357 at gmail.com> >> >> >> >> If DSolve cannot solve the equations then use NDSolve. >> >> >> >> func[coef_?NumericQ, c_?NumericQ, x_?NumericQ] := >> >> y[t] /. NDSolve[{y'[t] == Cos[coef*t], y[0] == c}, >> >> y[t], {t, -10, 10}][[1]] /. t -> x >> >> >> >> Manipulate[Plot[ >> >> Evaluate[func[coef, c, x] /. c -> Range[5]], >> >> {x, -10, 10}, >> >> PlotRange -> {-5, 10}], >> >> {{coef, 1}, 0.1, 5, 0.01, >> >> Appearance -> "Labeled"}] >> >> >> >> >> >> Bob Hanlon >> >> >> >> >> >> On Sun, Jul 29, 2012 at 6:12 PM, Juan Barandiaran >> >> <barandiaran.juan at gmail.com> wrote: >> >> > Thanks for your answer Bob, >> >> > Of course your solution works, but I still don't understand why mine >> >> > doesn't >> >> > and I cannot use your proposed approach because the way you write the >> >> > problem it is easy for Mathematica to solve the DSolve. >> >> > And this is just a simple example, in my real problem the DSolve >> >> > cannot >> >> > be >> >> > solved analytically. >> >> > This is why I tried to express the function as: >> >> > >> >> > {{y -> Function[{x}, DSolve[y'[x] == Cos[coef *x], y, x]]}} >> >> > >> >> > , which is something like the output I get from my DSolve. >> >> > >> >> > Thanks for your help. >> >> > >> >> > Juan >> >> > >> >> > >> >> > 2012/7/29 Bob Hanlon <hanlonr357 at gmail.com> >> >> >> >> >> >> Clear[func]; >> >> >> >> >> >> func[coef_, c_, x_] = >> >> >> y[x] /. DSolve[{y'[x] == Cos[coef*x], y[0] == c}, y[x], x][[1]] // >> >> >> Simplify >> >> >> >> >> >> c + Sin[coef*x]/coef >> >> >> >> >> >> >> >> >> Manipulate[Plot[Evaluate[ >> >> >> func[coef, c, x] /. >> >> >> c -> Range[5]], >> >> >> {x, -10, 10}, >> >> >> PlotRange -> {-5, 10}], >> >> >> {{coef, 1}, 0.1, 5, 0.01, >> >> >> Appearance -> "Labeled"}] >> >> >> >> >> >> >> >> >> Bob Hanlon >> >> >> >> >> >> >> >> >> On Sun, Jul 29, 2012 at 3:05 AM, <barandiaran.juan at gmail.com> >> >> >> wrote: >> >> >> > Hi, >> >> >> > >> >> >> > I'm trying to Manipulate a Plot of a quite difficult function >> >> >> > which >> >> >> > involves solving a differential equation, but cannot be solved >> >> >> > analytically. >> >> >> > >> >> >> > To try to simplify the example and simulate it, let's assume that >> >> >> > we >> >> >> > have the following function: >> >> >> > >> >> >> > func[coef_] = {{y -> Function[{x}, DSolve[y'[x] == Cos[coef *x], >> >> >> > y, >> >> >> > x]]}} >> >> >> > >> >> >> > Manipulate[Plot[{{Evaluate[y[x] /. func[coef] /. C[1] -> >> >> >> > {Range[-5, >> >> >> > 0]}]}}, {x, -10, 10}], {{coef , 1}, 0.1, 5}] >> >> >> > >> >> >> > I get an error: DSolve::dsvar: "-9.99959 cannot be used as a >> >> >> > variable" >> >> >> > >> >> >> > I think that this is because Manipulate assigns a value to x (= >> >> >> > -9.99959) BEFORE solving the DSolve, even though to avoid it I'm >> >> >> > using the >> >> >> > Evaluate function, which should process the function before >> >> >> > assigning >> >> >> > a >> >> >> > value to x. >> >> >> > >> >> >> > But the thing is that the "coef" to be Manipulated is at the same >> >> >> > "level" as the x in the Manipulate block, so probably if I need >> >> >> > the >> >> >> > coef to >> >> >> > solve the DSolve, I also have the x that gives me an error. >> >> >> > >> >> >> > Is there any workaround? I guess I'm not understanding properly >> >> >> > how >> >> >> > Mathematica processes these simple expressions. >> >> >> > >> >> >> > Thanks, Juan >> >> >> > >> >> > >> >> > >> > >> > > >