Following on to ODE thread 2nd order to 1st

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
10 replies · 6K views
Dustinsfl
Messages
2,217
Reaction score
5
I am getting this error in Mathematica from the code below:
Computed derivatives do not have dimensionality consistent with the initial conditions

Code:
ClearAll["Global`*"]
\[Mu] = 398600;
s = NDSolve[{x1'[t] == x2[t],
    y1'[t] == y2[t],
    z1'[t] == z2[t], 
    x2'[t] == -\[Mu]*x1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^{3/2}, 
    y2'[t] == -\[Mu]*y1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^{3/2}, 
    z2'[t] == -\[Mu]*z1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^{3/2}, 
    x1[0] == -201000*Sqrt[3],
    y1[0] == 201000,
    z1[0] == 0,
    x2[0] == -2.04119,
    y2[0] == 0.898024,
    z2[0] == 0}, {x, y, z}, {t, 0, 50}];
 
Physics news on Phys.org
I don't have MMA handy to use, but a few things I see you might change:

Code:
ClearAll["Global`*"]
\[Mu] = 398600;
s = NDSolve[{x1'[t] == x2[t],
    y1'[t] == y2[t],
    z1'[t] == z2[t], 
    x2'[t] == -\[Mu]*x1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^(3/2), 
    y2'[t] == -\[Mu]*y1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^(3/2), 
    z2'[t] == -\[Mu]*z1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^(3/2), 
    x1[0] == -201000*Sqrt[3],
    y1[0] == 201000,
    z1[0] == 0,
    x2[0] == -2.04119,
    y2[0] == 0.898024,
    z2[0] == 0}, {x1,x2, y1,y2, z1,z2}, {t, 0, 50}];

So I changed the curly braces to parentheses, as well as added the correct dependent variables in the list at the bottom. Does that work?
 
Ackbach said:
I don't have MMA handy to use, but a few things I see you might change:

Code:
ClearAll["Global`*"]
\[Mu] = 398600;
s = NDSolve[{x1'[t] == x2[t],
    y1'[t] == y2[t],
    z1'[t] == z2[t], 
    x2'[t] == -\[Mu]*x1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^(3/2), 
    y2'[t] == -\[Mu]*y1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^(3/2), 
    z2'[t] == -\[Mu]*z1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^(3/2), 
    x1[0] == -201000*Sqrt[3],
    y1[0] == 201000,
    z1[0] == 0,
    x2[0] == -2.04119,
    y2[0] == 0.898024,
    z2[0] == 0}, {x1,x2, y1,y2, z1,z2}, {t, 0, 50}];

So I changed the curly braces to parentheses, as well as added the correct dependent variables in the list at the bottom. Does that work?

It fixed the error but nothing plots:
Code:
ParametricPlot3D[
 Evaluate[{x1[t], y1[t], z1[t], x2[t], y2[t], z2[t]} /. s], {t, 0, 
  50}, Boxed -> False, PlotRange -> All, PlotStyle -> {Red}]
 
Try something simpler: a 1-D plot of just x2[t]/.s:

Plot[x1[t]/.s,{t,0,50}]

Does that plot?
 
Ackbach said:
Try something simpler: a 1-D plot of just x2[t]/.s:

Plot[x1[t]/.s,{t,0,50}]

Does that plot?

I am working on making a little setup that evaluates trajectories for 2 body and restricted 3 body problem. That is why I wanted to change the 2nd order equation of motion for planetary bodies in two 1st order equations.
 
Well, I understand that you want to plot a parametric 3D plot, but I'm just trying to debug your code. Start small, and build up to the big plot.
 
Ackbach said:
Well, I understand that you want to plot a parametric 3D plot, but I'm just trying to debug your code. Start small, and build up to the big plot.

I have done many of smaller plots. You can view my notes in the ODE section to see. Everything seems correct so I don't see the issue.

That smaller plot won't run.
 
I'm a little unclear what the \[Mu] notation means. Why not try this:

ClearAll["Global`*"]
Mu = 398600;
s = NDSolve[{x1'[t] == x2[t],
y1'[t] == y2[t],
z1'[t] == z2[t],
x2'[t] == -Mu*x1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^(3/2),
y2'[t] == -Mu*y1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^(3/2),
z2'[t] == -Mu*z1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^(3/2),
x1[0] == -201000*Sqrt[3],
y1[0] == 201000,
z1[0] == 0,
x2[0] == -2.04119,
y2[0] == 0.898024,
z2[0] == 0}, {x1,x2,y1,y2,z1,z2}, {t, 0, 50}];

Does a small plot work on this?
 
Ackbach said:
I'm a little unclear what the \[Mu] notation means. Why not try this:

ClearAll["Global`*"]
Mu = 398600;
s = NDSolve[{x1'[t] == x2[t],
y1'[t] == y2[t],
z1'[t] == z2[t],
x2'[t] == -Mu*x1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^(3/2),
y2'[t] == -Mu*y1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^(3/2),
z2'[t] == -Mu*z1[t]/(x1[t]^2 + y1[t]^2 + z1[t]^2)^(3/2),
x1[0] == -201000*Sqrt[3],
y1[0] == 201000,
z1[0] == 0,
x2[0] == -2.04119,
y2[0] == 0.898024,
z2[0] == 0}, {x1,x2,y1,y2,z1,z2}, {t, 0, 50}];

Does a small plot work on this?

\[Mu] = $\mu$ symbol in Mathematica
 
Well, my only advice is to start stripping things out of your model (like dimensions) until you get something that works. Then start adding things back in.
 
Ackbach said:
Well, my only advice is to start stripping things out of your model (like dimensions) until you get something that works. Then start adding things back in.

The problem was t. Since I am dealing with space flight, t of 250 is only 250 seconds. Taking t > 1million yields results.
 
Last edited: