Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

OK, I have a nice plot of some oscillating motion. I can read some stuff off the graph, but I thought it would be cool to show a dot where the curve hits the x-axis and a little label showing the coordinates, and thus get a direct measure of the period of oscillation. (I know there are equations for that but that's not quite the point of the exercise).

Anyhow, here's the code I am using

Clear[x1, x2, v1, v2];Clear[x1, x2, v1, v2];
ti = 0; tf = 4.; delta = (1/40);
x1[ti] = -0.04; x2[ti] = -0.04; v1[ti] = 0.0; v2[ti] = 0.0;
m1 = 0.1890; m2 = 0.1898; k = 3.65; kc = 3.5;
Do[ F1[t] = -k*x1[t] - kc*(x1[t] - x2[t]); 
 F2[t] = -k*x2[t] - kc*(x2[t] - x1[t]);
 v1[t + delta] = v1[t] + (F1[t]/m1)*delta; 
 v2[t + delta] = v2[t] + (F2[t]/m2)*delta;
x1[t + delta] = x1[t] + v1[t + delta]*delta;    
 x2[t + delta] = x2[t] + v2[t + delta]*delta,
 {t, ti, tf, delta}] 
x1data = Table[{t, x1[t]}, {t, ti, tf, delta}];
x1plot = ListPlot[x1data, AxesLabel -> {"t", "x1"}, Joined -> True]

and with it I get a nice graph:

enter image description here

I tried using NSolve and didn't get any output at all. So I think NSolve must not be geared to this.

Understand I am a rank beginner with Mathematica. So it's ok to assume I am stupid :-) And I know that there are likely more efficient ways to do the curve and such; this is a lab exercise tho. We're testing a simulation against the real data.

share|improve this question
Code is no complete - copy paste it in notebook - won't work. ti, tf, etc. ? – Vitaliy Kaurov Apr 11 '14 at 4:55
i think i just fixed it... – Jesse Apr 11 '14 at 5:21
closely related – Kuba Apr 11 '14 at 6:39
that looks nice-- but remember, i didn't even know mathematica existed until January. (Well, I did, but I had never actually used it before this semester). So I am at the level of asking "what commands do X?" if you see what I mean. (I'll get deep into coding stuff as I go later on). – Jesse Apr 11 '14 at 6:43
There is a code, so those are commands, and they are in docs. The last method can be undesrstood with help of it. Moreover, since you are using ListPlot you need to Interpolate before Solve. The former needs an idea what construction graphics /. object:>expr does, it is more clear here. Of course feel free to ask if any problems arise. – Kuba Apr 11 '14 at 6:49
up vote 5 down vote accepted

You can display zero crossing using MeshFunctions. Here is a clumsy exploitation from created graphic. The half-periods (difference between consecutive points) are displayed below with mean in red.

x1plot = ListPlot[x1data, AxesLabel -> {"t", "x1"}, Joined -> True, 
  MeshFunctions -> (#2 &), Mesh -> {{0.}}, 
  MeshStyle -> {Red, PointSize[0.02]}];
pts = x1plot[[1, 2, 1]];
me = pts[[First@Cases[x1plot, Point[x_] :> x, Infinity]]];
plt = Column[{ListPlot[x1data, AxesLabel -> {"t", "x1"}, 
    Joined -> True, MeshFunctions -> (#2 &), Mesh -> {{0.}}, 
    MeshStyle -> {Red, PointSize[0.02]}, 
    Epilog -> (Text[Framed[First@#], #, {0, -1.4}] & /@ me), 
    PlotRangePadding -> {{0, 0.5}, {0, 0}}, ImageSize -> 400],
    Join[(diff = Differences[me[[All, 1]]]), {Style[Mean@diff, Red]}]]
   }, Alignment -> Center]

enter image description here

share|improve this answer
Thanks, that is the kind of thing I was looking for. I know it's a bit clumsy but that's the level I am at. :-) – Jesse Apr 12 '14 at 14:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.