1
$\begingroup$

I'm currently trying to make an educational aid that would graph the position of an object as derived from a second order differential equation. What I would like is for the graph to change as I change the various constants of the equation.

What I currently have looks like this:

a = Manipulate[
  DSolve[{m*x''[t] + b*x'[t] + k*x[t] == 0, x[0] == 5, x'[0] == 0}, 
   x[t], t], {b, 1, 20, .1}, {m, 1, 20, .1}, {k, 1, 20, .1}]

f[t_] = x[t] /. a[[1]];

Dynamic[Plot[f[t], {t, 0, 20}]]

The manipulate works fine, it provides sliders and will output a correctly solved x(t) for the differential for whatever constants I change the sliders to. However, the plot doesn't update as the sliders update. Any advice on how to get the plot to correctly update would be greatly appreciated

$\endgroup$
  • $\begingroup$ Does this Q&A answer your question? $\endgroup$ – bobthechemist Mar 3 '17 at 2:49
2
$\begingroup$

Using Manipulate. It is best to keep everything inside Manipulate, and not have separate functions outside.

enter image description here

code

Manipulate[
 tick;
 sol = x[t] /. 
   First@DSolve[{m*x''[t] + b*x'[t] + k*x[t] == 0, x[0] == x0, 
      x'[0] == xd0}, x[t], t];
 Plot[sol, {t, 0, 20}, PlotRange -> All, ImageSize -> 300, 
  ImagePadding -> All, Frame -> True, 
  FrameLabel -> {{"y(t)", None}, {"t (sec)", "Solution to ODE"}}, 
  GridLines -> Automatic, GridLinesStyle -> LightGray, 
  BaseStyle -> 14],

 Grid[{
   {"damping b",
    Manipulator[Dynamic[b, {b = #; tick = Not[tick]} &], {1, 20, .1}, 
     ImageSize -> Tiny], Dynamic[b]
    },

   {"mass m",
    Manipulator[Dynamic[m, {m = #; tick = Not[tick]} &], {1, 20, .1}, 
     ImageSize -> Tiny], Dynamic[m]
    },

   {"stifness k",
    Manipulator[Dynamic[k, {k = #; tick = Not[tick]} &], {1, 20, .1}, 
     ImageSize -> Tiny], Dynamic[k]
    },

   {"x(0)",
    Manipulator[
     Dynamic[x0, {x0 = #; tick = Not[tick]} &], {0, 20, .1}, 
     ImageSize -> Tiny], Dynamic[x0]
    },

   {"x'(0)",
    Manipulator[
     Dynamic[xd0, {xd0 = #; tick = Not[tick]} &], {0, 20, .1}, 
     ImageSize -> Tiny], Dynamic[xd0]
    }
   }, Alignment -> Center, Spacings -> {1, 1}, Frame -> True],

 {{tick, False}, None},
 {{m, 1}, None},
 {{b, 1}, None},
 {{k, 1}, None},
 {{x0, 5}, None},
 {{xd0, 0}, None},
 TrackedSymbols :> {tick},
 SynchronousUpdating -> False,
 ControlPlacement -> Left
 ]
$\endgroup$
1
$\begingroup$

How about this:

Manipulate[
 With[{func = 
    x[t] /. DSolve[{m*x''[t] + b*x'[t] + k*x[t] == 0, x[0] == 5, 
        x'[0] == 0}, x[t], t][[1, 1]]}, Plot[func, {t, 0, 20}]], {b, 
  1, 20, .1}, {m, 1, 20, .1}, {k, 1, 20, .1}]

I use With as a quick and easy way to avoid substitution problems with putting DSolve in Plot.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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