# Dynamic plot with synchronization of real time

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?
• Is UpdateInterval->1, TrackedSymbols:>{} an answer? 9766 – Kuba Dec 27 '15 at 10:51
• 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 – Sascha Dec 27 '15 at 10:56
• 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. – Sascha Dec 27 '15 at 11:03
• @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. – Nasser Dec 27 '15 at 11:33
• My goal is to read data from an Arduino connected using USB. The data change with time, for example, temperature (voltage) along time. – LeoRon7 Dec 27 '15 at 12:52

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[]];


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

• What is the best strategy (fastest) to store the values in data, not using AppendTo? – LeoRon7 Dec 27 '15 at 13:23
• @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 – Nasser Dec 27 '15 at 13:33

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]}]


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