8
$\begingroup$

I want to do a dynamic plot with synchronization of real time, that is, I have a function, f[t], that is a function of time (seconds), so I want to see the curve changing not so fast or not so slow, just in real time (seconds).

I tried this:

data = {};
t = 0;
freq = .5 Pi;
f[t] := Sin[freq t] + Sin[1.5 freq t];
Dynamic[
    current=f[t];
    AppendTo[data,current];
    t = t + .05;
    ListPlot[data, PlotRange->{{t-200, t}, {-5, 5}}, ImageSize->Medium],
    UpdateInterval->0]

Issues:

  1. When I plot, the time is not synchronized, that is, it is too fast or too slow. How do I do it?
  2. As time goes on, the plot speed get slower. data vector is too big???
  3. How do I stop the "shacking" of the plot along time?
$\endgroup$
  • $\begingroup$ Is UpdateInterval->1, TrackedSymbols:>{} an answer? 9766 $\endgroup$ – Kuba Dec 27 '15 at 10:51
  • $\begingroup$ Do you really need near real time data (e.g. constantly polling some sensor data/ querying computable data) or do you just want to visualise some basic function? If your case is the latter have a look at Animate $\endgroup$ – Sascha Dec 27 '15 at 10:56
  • $\begingroup$ Concerning your second question about performance: A good read might be an answer from Leonid Shifrin concerning performance in Mathematica. Constructing lists in a Matlab-esque fashion like you do with AppendTo has terrible performance in Mathematica. $\endgroup$ – Sascha Dec 27 '15 at 11:03
  • 1
    $\begingroup$ @Kuba I looked at updateInterval, but was not sure if it is more "strict" on getting timing exactly as needed than scheduled task. It says if possible, be done at least every t seconds. This whole subject of "real time" and Mathematica dynamics is very tricky. $\endgroup$ – Nasser Dec 27 '15 at 11:33
  • $\begingroup$ My goal is to read data from an Arduino connected using USB. The data change with time, for example, temperature (voltage) along time. $\endgroup$ – LeoRon7 Dec 27 '15 at 12:52
6
$\begingroup$
data = {};   
freq = .5 Pi;
f[t_] := Sin[freq t] + Sin[1.5 freq t];
t = 0;
Grid[{{
   Row[{"t=", Dynamic@t}]},
  {Dynamic[
    ListPlot[data, PlotRange -> {{t - 200, t}, {-5, 5}}, ImageSize -> Medium],
     TrackedSymbols :> {t}]}
  }] 

RunScheduledTask[{t += .5; current = f[t]; AppendTo[data, current]}, {.5, 100}];

Then

RemoveScheduledTask[ScheduledTasks[]];

See RunScheduledTask

ps. I would not use append as you are doing. But this is what you had.

$\endgroup$
  • $\begingroup$ What is the best strategy (fastest) to store the values in data, not using AppendTo? $\endgroup$ – LeoRon7 Dec 27 '15 at 13:23
  • 1
    $\begingroup$ @LeoRon7 the standard method for allocating a buffer when one does not know how much to preallocate, is to allocate it using fixed sizes. (one chunk at a time). Start with say size = n, where is n is some number you decide on. Use Table to allocate it. Then use [[ ]] to write to it. When it fills up, you can allocate another list of size n. And so on. If you need all those chunks in memory, you make a linked list of them. And so on. Here is an example of linked list mathprogramming-intro.org/book/node525.html $\endgroup$ – Nasser Dec 27 '15 at 13:33
9
$\begingroup$

You can use Clock as a dynamic variable that corresponds to the time elapsed since the start of the evaluation.

Dynamic@Plot[Sin[x], {x, 0, Clock[Infinity]}]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ How to solve the "shaking" of the plot? The input line moves up and down... even if I fix the plot dimensions with ImageSize. $\endgroup$ – LeoRon7 Dec 27 '15 at 13:26
  • $\begingroup$ @LeoRon7 Try fixing the vertical PlotRange. $\endgroup$ – Szabolcs Dec 27 '15 at 14:45
  • $\begingroup$ I tried to fix the vertical PlotRange values, but there is still a shaking... $\endgroup$ – LeoRon7 Dec 28 '15 at 14:26
  • $\begingroup$ @LeoRon7 I don't know how to make it go away if you want to keep the animation running within Mathematica. $\endgroup$ – Szabolcs Dec 28 '15 at 16:35

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.