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?
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.
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).
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.
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.