# DSolve behaves weirdly with dynamic [duplicate]

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][]+Dynamic[b]*X[t][],
Dynamic[c]*X[t][]+Dynamic[d]*X[t][]}*),
Print["
"],
sol = DSolve[system, {x, y}, t];

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

}]
]


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}];
(* Out= Dynamic *)

test2 = {i, j};
(* Out= List *)


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

test[]
(* Out= {i, j} *)
test2[]
(* Out= 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][];
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 -> i, C -> j}, {i, -2, 8, 3}, {j, -2,
8, 3}], 1], {t, -3, 3}, PlotRange -> {-3, 3}, ImageSize -> 300]]
}]] 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 -> i,
C -> 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}] • 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? – Mudi QinChuan May 15 '19 at 6:00
• @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. – MelaGo May 15 '19 at 21:27
• Thank you so much!! – Mudi QinChuan May 17 '19 at 4:45