Message Boards Message Boards

0
|
11444 Views
|
4 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Plotting data from simulation of springs with masses

Posted 12 years ago
Below I have code for springs with masses attached to them. How can I plot it so that the position (X) is on the vertical axis and time (T) is on the horizontal axis?
 kp = 40; (* Spring constant *)
 kc = 250;(* Spring constant *)
 deltaT = .03; (* Time Step *)
 m = 1; (* Mass *)
 
 iini = 0;
 ilast = 25;
 nini = 0;
 nlast = 300;


X = 0;
V = .01;
F = 50;
T[nini] = 0;



For[i = iini, i <= ilast, i++,

  For[n = nini, n <= nlast, n++'

      X[i + 1] = X[i] + V[i]*deltaT;

    T[n + 1] = T[n] + deltaT;

    V[i + 1]  =   
     V[i] -   (kc (X[i + 1] + X[i - 1] - 2*X[i] +
            kp (V[i]*T[n + 1] - X[i]) + F[i])/m)*deltaT;
    ];
  ];
POSTED BY: sean roubion
4 Replies
Posted 12 years ago
Sean,

you can visualize data of type {{t0, x0}, {t1, x1}, {t2, x2}, ...} with ListPlot, see

http://reference.wolfram.com/mathematica/ref/ListPlot.html

The function or interpolation function you would get from Nick's suggestion can be visualized with Plot:

http://reference.wolfram.com/mathematica/ref/Plot.html

If you are not required to use any particular numerical method then I would also strongly recommend you to use either DSolve or NDSolve to find your answer. Hope this helps.
POSTED BY: Peter Fleck
Posted 12 years ago
Hello Sean,

I am not that good at physics, so I apologies if my idea does to fit to the purpose. 

I would suggest loosing the "For" loop. It is my experience with Mathematica, that whenever you use loops, there is a more clean way of doing things. In your case, I might consider using Recurrence Table, and then merge the lists using Transpose. Then you can plot the data via ListPlot. :-)

For instance:
 kp = 40;(*Spring constant*)
 kc = 250;(*Spring constant*)
 
 deltaT = .03;(*Time Step*)
 m = 1;(*Mass*)
 
 iini = 0;
 ilast = 25;
 nini = 0;
nlast = 300;

X = 0;
V = .01;
F = 50;
T = 0;

lstTime =
  RecurrenceTable[{time[n + 1] == time[n] + deltaT, time[1] == nini},
   time, {n, nini + 1, nlast}];

Hope it helps, or that someone can help you with a better answer :-)

Edit: As I am sure you recognized, I left out writing the "V[n+1]". Simply because I fear I would write the expression wrong when I do not know the physics behind it in detail. So it is for you to fill out :-)

Edit 2: I completely forgot to say that RecurrenceTable allows for more systems of equations than just a single one. In which case you will not need to Transpose prior plotting, I presume. Check out http://reference.wolfram.com/mathematica/ref/RecurrenceTable.html
POSTED BY: Simon P.
I would recommend looking through numerous Demonstrations we have - there are quite a few on spring and mass system simulations. The Mathematica code is freely available for download - that could be your starting point.



POSTED BY: Vitaliy Kaurov
Hi Sean,
It seems you are trying to solve some diffferential equations. Mathematica can do this directly using the DSolve function (for exact solutions), or the NDSolve function (for numerical solutions).
POSTED BY: Nick James
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract