Description
In software engineering, it is a good practice to comment your code. I would advise you to utilise comments to partition your code in a following way. Additionally, if you have ever worked with any other programming languages, you could employ indentation. Alternatively, you could modularize your application as to develop it in smaller, more manageable "chunks". Please see sample below
Example #1 - Indentation + Comments (Full Program)
Code
f[y_] = 1/4 y (3 - y);
solution = DSolve[{y'[t] == f[y[t]], y[0] == 1}, y[t], t][[1]];
ϕ[t_] = y[t] /. solution;
Manipulate[
Grid[
Show[
(*Show Component Declaration*)
VectorPlot[
(*Vector Plot Declaration*)
{1, f[y]}, {t, -8, 8}, {y, -4, 4},
(*Vector Plot Options*)
VectorStyle -> {Thick, Red},
VectorScale -> {.04, 1.5, None},
VectorPoints -> 20
],
Plot[
(*Plot Declaration*)
ϕ[t] /. c -> y0, {t, -8, b},
(*Plot Option Declaration*)
PlotStyle -> {Thick, Blue}
],
(*Show Option Declaration*)
Frame -> False,
Axes -> True,
AxesStyle -> Thick,
AxesLabel -> {Text[Style["t", Black, Large, Italic]],
Text[Style["y", Black, Large, Italic]]},
TicksStyle -> {{Large, Black}, {Large, Black}}, ImageSize -> Medium
],
Show[
(*Show Component Declaration*)
Plot[
(*Plot Declaration*)
f[y], {y, -1, 4},
(*Plot Option Declaration*)
PlotStyle -> {Thick, Orange},
AxesLabel -> {Text[Style["y", Large, Italic]],
Text[Style["f(y)", Large, Italic]]}, ImageSize -> Medium],
Graphics[
(*Graphics Component Declaration*)
{Red, PointSize[.03], Point[{ϕ[b] /. c -> y0, 0}]}
]
]
],
(*Manipulate Control Declaration*)
{b, -7.99, 8},
{{y0, 2}, -1, 4},
(*Manipulate Option Declaration*)
LabelStyle -> Large
]
Example #2 - Indentation + Comments + Modularization (Sample)
Code
DynamicModule[
{
c, y, f,
y0 = 2,
b = 1,
solution, \[Phi],
plotVector, plotPlot
},
(*Input/Output Description*)
f[y_] := 1/4 y (3 - y);
(*Input/Output Description*)
solution = DSolve[{y'[t] == f[y[t]], y[0] == c}, y[t], t][[1]];
(*Input/Output Description*)
\[Phi][t_] := y[t] /. solution;
(*Main Column*)
Panel @ Column[{
(*Output Description*)
Row @ {
Dynamic @ Show[{
(*Output Description*)
plotVector = VectorPlot[{1, f[y]}, {t, -8, 8}, {y, -4, 4}, VectorStyle -> {Thick, Red}, VectorScale -> {.04, 1.5, None}, VectorPoints -> 20];
(*Output Description*)
plotPlot = Plot[\[Phi][t] /. c -> y0, {t, -8, b}, PlotStyle -> {Thick, Blue}];
plotVector,
plotPlot
},
(*Show Options*)
Frame -> False, Axes -> True, AxesStyle -> Thick,
AxesLabel -> {Text[Style["t", Black, Large, Italic]], Text[Style["y", Black, Large, Italic]]}, TicksStyle -> {{Large, Black}, {Large, Black}},
ImageSize -> Medium
]
},
(*Output Description*)
Column @ {
Row @ {Style["y0 ", 16], Slider[Dynamic @ y0, {-1, 4}]},
Row @ {Style["b", 16], Slider[Dynamic @ b, { -7.99, 8}]}
}
},
(*Main Column Options*)
Alignment -> Center]
]
Notes
N.1 It is always a good idea to give your variables a descriptive name. Given that I am not aware what you are trying to solve, I left variable names as in OP and any additional variables been given dummy names which SHOULD be changed
N.2 I wanted to move both plotVector
and plotPlot1
definition outside Show
, but couldn't figure out how to introduce Dynamic
updating of plotPlot1
in this configuration*)
VectorPlot
and the secondPlot
can be evaluated outsideManipulate
and inserted inShow
, otherwise you are recreating them each time. $\endgroup$ – Kuba♦ Jun 22 '16 at 11:47Dynamic
to prevent unnecessary evaluation. Moreover, completely outside may not be the best place because one may need to have this definition preserved across FE sessions. Possible solutions are:SaveDefinitions
or defining it inInitialization
or injecting that graphics withWith
. $\endgroup$ – Kuba♦ Jun 22 '16 at 13:28