1
$\begingroup$

This question already has an answer here:

I am an beginner in using dynamics as well as mathematica. I am trying to ask mathematica to solve and plot the solution of X'=AX with dynamic every time my matrix A updates. However the Dsolve behaves weirdly and giving a bunch of errors.

Attached is my code

 DynamicModule[{a = 3, b = 2, c = 2, d = 3, X, A, system, sol, f},
 Row[{
   A = {{Dynamic[a], Dynamic[b]}, {Dynamic[c], Dynamic[d]}};
   Column[{
     Row[{Text["a :   "], InputField[Dynamic[a]]}],
     Row[{Text["b :   "], InputField[Dynamic[b]]}],
     Row[{Text["c :   "], InputField[Dynamic[c]]}],
     Row[{Text["d:   "], InputField[Dynamic[d]]}],
     Dynamic[A]
     }],

   X[t_] = {x[t], y[t]};
   system = Dynamic[X'[t] == A.X[t]];

   (*system=X'[t]\[Equal]{Dynamic[a]*X[t][[1]]+Dynamic[b]*X[t][[2]],
   Dynamic[c]*X[t][[1]]+Dynamic[d]*X[t][[2]]}*),
   Print["  
      "],
   sol = DSolve[system, {x, y}, t];



   particularsols = 
    Partition[
     Flatten[Table[{x[t], y[t]} /. sol /. {C[1] -> i, 
         C[2] -> j}, {i, -2, 8, 3}, {j, -2, 8, 3}]], 2];
   ParametricPlot[Evaluate[particularsols], {t, -3, 3}, 
    PlotRange -> {-3, 3}]

   }]
 ]
$\endgroup$

marked as duplicate by Kuba May 14 at 6:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1
$\begingroup$

Problems arise when trying to use expressions defined with Dynamic in downstream manipulations. For example, a list inside Dynamic has Head Dynamic, and not List as one might expect.

test = Dynamic[{i, j}];
Head[test]
(* Out[13]= Dynamic *)

test2 = {i, j};
Head[test2]
(* Out[14]= List *)

Trying to use test can then yield unexpected results. For example

test[[1]]
(* Out[28]= {i, j} *)
test2[[1]]
(* Out[29]= i *)

This tutorial has a lot of useful info about Dynamic.

In this case, you could do DSolve once, symbolically, then substitute the dynamic values in at the end.

DynamicModule[{a = 3, b = 2, c = 2, d = 3},
 A = {{aa, bb}, {cc, dd}};
 X[t_] = {x[t], y[t]};
 sol = DSolve[X'[t] == A.X[t], {x[t], y[t]}, t][[1]];
 Row[{
   Column[{
     Row[{Text["a :   "], InputField[Dynamic[a]]}],
     Row[{Text["b :   "], InputField[Dynamic[b]]}],
     Row[{Text["c :   "], InputField[Dynamic[c]]}],
     Row[{Text["d:   "], InputField[Dynamic[d]]}],
     Dynamic[{{a, b}, {c, d}}]
     }],
   Dynamic[
    ParametricPlot[
     Evaluate@
      Flatten[Table[{x[t], y[t]} /. sol /. {aa -> a, bb -> b, cc -> c,
            dd -> d} /. {C[1] -> i, C[2] -> j}, {i, -2, 8, 3}, {j, -2,
          8, 3}], 1], {t, -3, 3}, PlotRange -> {-3, 3}, ImageSize -> 300]]
   }]]

enter image description here

Edited to include a version using Manipulate. This is generally easier to use, especially if you want to use your dynamic values as inputs to other functions.

Manipulate[
 A = {{a, b}, {c, d}};
 X[t_] = {x[t], y[t]};
 sol = DSolve[X'[t] == A.X[t], {x[t], y[t]}, t];
 Column[{
   A,
   ParametricPlot[
    Evaluate@
     Flatten[Table[{x[t], y[t]} /. sol /. {C[1] -> i, 
         C[2] -> j}, {i, -2, 8, 3}, {j, -2, 8, 3}], 1], {t, -3, 3}, 
    PlotRange -> {-3, 3}, ImageSize -> 300]
   }], {a, 3}, {b, 2}, {c, 2}, {d, 3}]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you!! this is makes a lot more sense to me now. I have an additional question towards the place where you put Dsolve. what is the thinking behind putting dynamic at the top and have it evaluate only once? I was trying to put an dynamic around dsolve but it also didn't work. What i was thinking was, everytime i updates the coefficients, I should re-do the ODE again, as in put a Dynamic[Desolve[....]] or Dsolve[Dynamic[...]]. Do you by any chance know why this also doesn't work and why i shouldn't do it this way? $\endgroup$ – Mudi QinChuan May 15 at 6:00
  • $\begingroup$ @MudiQinChuan: The thinking behind evaluating DSolve once is efficiency - it should be faster to substitute in coefficients than to re-solve every time. For the second part of your question, the tutorial I linked probably has better answers. I guess it could be done that way, with some fixes, though I couldn't figure it out. If you want multiple things to re-evaluate automatically, Manipulate is generally much easier to use. I will update the answer to include a version using Manipulate. $\endgroup$ – MelaGo May 15 at 21:27
  • $\begingroup$ Thank you so much!! $\endgroup$ – Mudi QinChuan May 17 at 4:45

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