MHB Following on to ODE thread 2nd order to 1st

AI Thread Summary
The discussion revolves around troubleshooting an error in Mathematica related to the dimensionality of computed derivatives in a system of differential equations modeling planetary motion. The original code encountered an error due to incorrect syntax and missing dependent variables in the NDSolve function. Suggestions included changing curly braces to parentheses and ensuring all relevant variables were included in the output list. After making these adjustments, the error was resolved, but issues with plotting persisted. Participants advised starting with simpler plots to debug the code effectively. Ultimately, it was determined that the time variable 't' was set too low for the simulation, and increasing it resolved the plotting issues, allowing for successful trajectory evaluations in the context of a two-body and restricted three-body problem.
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
Views
1K
Replies
1
Views
4K
Replies
1
Views
2K
Replies
2
Views
7K
Replies
3
Views
7K
Replies
2
Views
1K
Replies
3
Views
3K
Back
Top