Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: problems with DSolve

  • To: mathgroup at smc.vnet.net
  • Subject: [mg99418] Re: [mg99375] problems with DSolve
  • From: "David Park" <djmpark at comcast.net>
  • Date: Tue, 5 May 2009 05:42:55 -0400 (EDT)
  • References: <7247615.1241432020517.JavaMail.root@n11>

Let's see what happens when the Plot statement tries to get its first point,
say at t = 0 (although it probably doesn't actually start there.) Then you
get

Plot[(x[0] /. DSolve[{x'[0] == y[0], y'[0] == -x[0], x[0] == 1, y[0]
== 2}, {x, y}, 0]), at t = 0]

and your Dsolve equations and statement are shot to pieces because t has
been everywhere replaced by 0.

Whenever you are plotting it is better to calculate the plotting functions
FIRST instead of trying to cram all calculations into the plot statement.

In your example the equations can be solved for the generic case of initial
values so:

Clear[x, y]; sols = 
 First@DSolve[{x'[t] == y[t], y'[t] == -x[t], x[0] == x0, 
    y[0] == y0}, {x, y}, t];
x[x0_, y0_][t_] = x[t] /. sols
y[x0_, y0_][t_] = y[t] /. sols

Giving

x0 Cos[t] + y0 Sin[t]
y0 Cos[t] - x0 Sin[t]

Then, the Manipulate statement would be:

Manipulate[
 Plot[{x[x0, y0][t], y[x0, y0][t]}, {t, 0, 2 \[Pi]},
  PlotRange -> {{0, 2 \[Pi]}, {-6, 6}},
  Frame -> True],
 {{x0, 0}, -5, 5, .1, Appearance -> "Labeled"},
 {{y0, 0}, -5, 5, .1, Appearance -> "Labeled"}]


In a case like this you should always specify a PlotRange so the scale won't
be jumping around as you vary the conditions, "The Manipulation Jitters". 

If your differential equations are such that you can't obtain a generic
solution with DSolve, but have to use NDSolve on each set of initial
conditions, then you can use the following custom dynamic presentation. At
first, it looks more difficult than Manipulate because it is longer. But it
is actually easier because you have complete control of the layout and the
calculation of intermediate results - in this case the solutions to the
differential equations. The main trick here is the use of the second
argument in Dynamic that appears in the controls. For example, the second
argument, (x0 = #; calcsolution[x0, y0]) &, says to set x0 to the current
value of the slider and then calculate the solutions to the differential
equations using the current values of x0 and y0.

Module[
 {x0 = -5, y0 = -5,
  x, y, calcsolution},
 
 (* Routine to calculate the x and y solutions *)
 calcsolution[xi_, yi_] :=
  Module[{sols},
   Clear[x, y]; 
   sols = First@
     NDSolve[{x'[t] == y[t], y'[t] == -x[t], x[0] == xi, 
       y[0] == yi}, {x, y}, {t, 0, 2 \[Pi]}];
   x[t_] = x[t] /. sols;
   y[t_] = y[t] /. sols;
   ];
 (* Initialize the solutions *)
 calcsolution[x0, y0];
 
 (* Set up the display and controls *)
 Panel[
  Column[
   {(* x0 Slider and InputField *)
    Row[{"x0: ", 
      Slider[Dynamic[x0, (x0 = #; calcsolution[x0, y0]) &], {-5, 
        5, .1}], Spacer[10], 
      InputField[Dynamic[x0, (x0 = #; calcsolution[x0, y0]) &], 
       FieldSize -> {5, 1}]}],
    (* y0 Slider and InputField *)
    Row[{"y0: ", 
      Slider[Dynamic[y0, (y0 = #; calcsolution[x0, y0]) &], {-5, 
        5, .1}], Spacer[10], 
      InputField[Dynamic[y0, (y0 = #; calcsolution[x0, y0]) &], 
       FieldSize -> {5, 1}]}],
    (* Plot *)
    Dynamic@
     Plot[{x[t], y[t]}, {t, 0, 2 \[Pi]},
      PlotRange -> {{0, 2 \[Pi]}, {-8, 8}},
      Frame -> True,
      ImageSize -> 400]
    }](* Column *),
  Style["Differential Equations with Dynamic Initial Conditions", 16]
  ](* Panel *)
 ]


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  


From: szymon.stoma at gmail.com [mailto:szymon.stoma at gmail.com] 

hello,

i am new to mathematica, therefore sorry if my question is trivial. I
would like to understand why the following line does not work:

Plot[(x[t] /. DSolve[{x'[t] == y[t], y'[t] == -x[t], x[0] == 1, y[0]
== 2}, {x, y}, t]), {t, 0, 5}]

whereas after splitting into two lines it works:
sol = DSolve[{x'[t] == y[t], y'[t] == -x[t], x[0] == 1, y[0] == 2},
{x, y}, t]
Plot[(x[t] /. sol), {t, 0, 5}]

i would like to use Manipulate to play with boundary conditions, so
the first notation would be highly useful.

thanks a lot for any feedback.




  • Prev by Date: Re: Diffusion Model using NDSolve - Advice needed
  • Next by Date: Re: why does DownValues not return all downvalues for a
  • Previous by thread: Re: problems with DSolve
  • Next by thread: Re: problems with DSolve