Following on to ODE thread 2nd order to 1st

  • Context: MHB 
  • Thread starter Thread starter Dustinsfl
  • Start date Start date
  • Tags Tags
    2nd order Ode Thread
Click For Summary
SUMMARY

The discussion centers on resolving an error in Mathematica related to the NDSolve function for a system of differential equations modeling two-body motion. The error "Computed derivatives do not have dimensionality consistent with the initial conditions" was addressed by changing curly braces to parentheses and ensuring the correct dependent variables were included in the solution. The variable \[Mu] represents the gravitational parameter, set to 398600, and the issue of plotting results was resolved by adjusting the time variable to a larger range, specifically t > 1 million, to yield meaningful results.

PREREQUISITES
  • Understanding of Mathematica syntax and functions, specifically NDSolve.
  • Familiarity with differential equations and their applications in orbital mechanics.
  • Knowledge of gravitational parameters and their significance in two-body problems.
  • Basic skills in plotting functions in Mathematica, including ParametricPlot3D.
NEXT STEPS
  • Explore the use of NDSolve in Mathematica for solving systems of differential equations.
  • Learn about the gravitational parameter and its role in celestial mechanics.
  • Investigate techniques for debugging Mathematica code, particularly for plotting issues.
  • Study the principles of orbital mechanics, focusing on two-body and restricted three-body problems.
USEFUL FOR

Mathematics and physics students, researchers in orbital mechanics, and developers working with Mathematica for simulating celestial dynamics.

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
 
  • #10
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.
 
  • #11
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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 17 ·
Replies
17
Views
5K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K