TransWikia.com

Controlling plots with sliders without using Manipulate

Mathematica Asked by Alel on April 9, 2021

Why are the sliders in the following code not affecting the plot? (Without using Manipulate)

Module[{t, o, z}, f[t]];
{Slider[ Dynamic[o], {1, 5, 0.1}], Dynamic[o]}
{Slider[ Dynamic[z], {0.1, 1.4, 0.1}], Dynamic[z]}
{Slider[ Dynamic[Ts], {5, 20, 1}], Dynamic[Ts]}
tf[o_, z_] := TransferFunctionModel[ω^2/(s^2 + 2 z o s + o^2), s];
f[t] = OutputResponse[tf[o, z], UnitStep[t], t];
Plot[f[t], {t, 0, T}, PlotRange -> {{0, T}, {0, 2}}]

2 Answers

One of the problems is that you use Set (=) to define f[t]. This means that f[t] will be equal to the evaluated form of the right-hand side of its definition where o and z are replaced by their values as they were at the time that f was defined.

What you want is SetDelayed (:=) which leaves the right-hand side in its unevaluated form. In that case the right-hand side will be re-evaluated with the current values for o and z every time f is called.

The second issue is that to get a plot that is updated dynamically, you would need to do something Dynamic[Plot[f[t], ...]]. I don't have access to Mathematica at the moment but I suspect that since o and z don't appear explicitly in Plot[f[t],...] you would also need to set TrackedSymbols -> {o, z} in Dynamic[Plot[...]].

Answered by Heike on April 9, 2021

Your code modified and with syntax errors fixed

DynamicModule[{o, z, capTs, ω, s},
tf[o_, z_, ω_, s] := TransferFunctionModel[ω^2/(s^2 + 2 z o s + o^2), s];
f[t_, o_, z_, ω_] := OutputResponse[tf[o, z, ω, s], UnitStep[t], t];
Grid[{{"o", Slider[Dynamic[o], {1, 5, 0.1}], Dynamic[o]},
 {"z", Slider[Dynamic[z], {0.1, 1.4, 0.1}], Dynamic[z]},
 {"ω", Slider[Dynamic[ω], {0.1, 1.4, 0.1}], Dynamic[ω]},
 {"Ts", Slider[Dynamic[capTs], {5, 20, 1}], Dynamic[capTs]}, 
 {Dynamic@ Plot[Evaluate@f[t, o, z, ω], {t, 0, capTs}, 
  PlotRange -> {{0, capTs}, {0, Automatic}}], SpanFromLeft}}]]

gives

enter image description here

Answered by kglr on April 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP