-3
$\begingroup$

I started using Mathematica few days ago. I'm running this code:

r1 = {0, 500, 0};
r2 = {0, 0, 0};
r3 = {0, 250, 1000};
v1 = {20, 20, 0};
v2 = {-20, -20, -0};
v3 = {0, 0, 0};
m1 = 5000000000000000;
m2 = 5000000000000000;
m3 = 5000000000000000;

G = 6.674*10^(-11);

l1 = {};
l2 = {};
l3 = {};

dT = 0.001;

For[i = 0, i < 2000001, i++,


  If[Mod[i, 100] == 0, l1 = Append[l1, r1]; l2 = Append[l2, r2]; 
   l3 = Append[l3, r3]; Print[i/2000000]];

  force12 = (G/Norm[r2 - r1]^3)*(r2 - r1);
  force13 = (G/Norm[r3 - r1]^3)*(r3 - r1);
  force23 = (G/Norm[r3 - r2]^3)*(r3 - r2);

  a1 = force12*m2 + force13*m3;
  a2 = -force12*m1 + force23*m3;
  a3 = -force13*m1 - force23*m3;

  f1[t_] := r1 + v1*t + a1*t^2/2;
  f2[t_] := r2 + v2*t + a2*t^2/2;
  f3[t_] := r3 + v3*t + a3*t^2/2;

  r1 = N[f1[dT], 12];
  r2 = N[f2[dT], 12];
  r3 = N[f3[dT], 12];

  v1 = N[f1'[dT], 12];
  v2 = N[f2'[dT], 12];
  v3 = N[f3'[dT], 12];
  ListPointPlot3D[{l1, l2, l3}]

  ];

ListPointPlot3D[{l1, l2, l3}]
TableForm[l1];
Export["sh01.tsv", l1];
TableForm[l1];
Export["sh02.tsv", l1];
TableForm[l1];
Export["sh03.tsv", l1];

Obviously this process take a lot of time.

Initially Mathematica works, but after some minutes of computer inactivity (I don't use computer) it stops running: I observe that CPU isn't working and there isn't any output on notebook. When I click on notebook Mathematica restarts computation, my CPU work at 30% and code restarts to print the state of work (Print[i/2000000]).

I'm using Mathematica 10 on Windows 7, I had the some problem with Mathematica 10 on Windows 8.

What I have to do for keeping Mathematica running?

P.S.: I don't know if my problem is with Mathematica or with my computer setting, however I tried this work on two computers and I observed the some thing.

$\endgroup$

closed as off-topic by Sjoerd C. de Vries, bbgodfrey, Michael E2, Bob Hanlon, m_goldberg Apr 27 '15 at 1:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Sjoerd C. de Vries, Michael E2, Bob Hanlon, m_goldberg
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ This is really an extraordinary claim, which is probably why your question was downvoted. People are probably not going to run your time-consuming code to try to observe a problem that they most likely don't believe exists, so could you please provide some concrete and convincing evidence for what you observe as part of the question? Specifically, why should we believe it is a problem with Mathematica, rather than with your computer? $\endgroup$ – Oleksandr R. Apr 26 '15 at 14:47
  • $\begingroup$ And which version of Mathematica, and which operating system, are you using? $\endgroup$ – Oleksandr R. Apr 26 '15 at 15:16
  • $\begingroup$ Mathematica 10, on windows 7 and 8 $\endgroup$ – Davide Rattacaso Apr 26 '15 at 15:34
  • 2
    $\begingroup$ Not directly relevant to your question, but have you considered removing the ListPointPlot3D from the loop? Why compute 2 million graphics expressions which you never display? $\endgroup$ – Simon Woods Apr 26 '15 at 15:36
  • 1
    $\begingroup$ I do observe the same behavior (10.0.0.0, Windows 8.1 x86-64). $\endgroup$ – 2012rcampion Apr 26 '15 at 15:38
7
$\begingroup$

Instead of doing the integration yourself, why not have Mathematica do it for you?

g = 6.674*^-11;
dt = 0.001;
tStop = 2000;

soln = First@NDSolve[{
    x1''[t] == 
     g (m2/Norm[x2[t] - x1[t]]^3 (x2[t] - x1[t]) + 
        m3/Norm[x3[t] - x1[t]]^3 (x3[t] - x1[t])), 
    x2''[t] == 
     g (m3/Norm[x3[t] - x2[t]]^3 (x3[t] - x2[t]) + 
        m1/Norm[x1[t] - x2[t]]^3 (x1[t] - x2[t])), 
    x3''[t] == 
     g (m1/Norm[x1[t] - x3[t]]^3 (x1[t] - x3[t]) + 
        m2/Norm[x2[t] - x3[t]]^3 (x2[t] - x3[t])),
    x1[0] == r1, x1'[0] == v1, x2[0] == r2, x2'[0] == v2, x3[0] == r3,
     x3'[0] == v3
    }, {x1, x2, x3}, {t, 0, tStop}]

ParametricPlot3D[
 Evaluate[Through[{x1, x2, x3}[t]] /. soln], {t, 0, tStop}]

enter image description here

This takes under a second to evaluate. You can still generate your tables:

Export["sh01.tsv", Table[x1[t] /. soln, {t, 0, tStop, dt}]]

But you may want to choose a larger timestep, since the table will currently have 2 million lines!

$\endgroup$
  • $\begingroup$ Thank you! I didn't know Mathematica can approssimate this kind of sistems! Do you know what algorithm it uses? $\endgroup$ – Davide Rattacaso Apr 26 '15 at 16:00
  • $\begingroup$ @Davide It can use a lot of different algorithms depending on the problem. Read the documentation to get an idea of NDSolve's complexity. $\endgroup$ – 2012rcampion Apr 26 '15 at 16:06

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