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

Discussion Overview

The discussion revolves around troubleshooting a Mathematica code for solving a system of ordinary differential equations (ODEs) related to planetary motion. Participants explore issues with dimensionality, plotting results, and debugging code, focusing on both theoretical and practical aspects of numerical solutions in the context of celestial mechanics.

Discussion Character

  • Technical explanation
  • Debugging
  • Exploratory

Main Points Raised

  • One participant reports an error in Mathematica regarding dimensionality consistency with initial conditions in their ODE setup.
  • Another participant suggests changes to the code, including modifying curly braces to parentheses and correcting the list of dependent variables, which resolves the initial error but leads to issues with plotting.
  • Several participants recommend starting with simpler plots to debug the code, emphasizing a step-by-step approach to building up to more complex visualizations.
  • There is a discussion about the notation for the gravitational parameter, with some participants expressing confusion over the use of \[Mu] versus Mu.
  • One participant mentions the need to adjust the time variable to achieve meaningful results, indicating that the time frame initially set was too short for the context of space flight.

Areas of Agreement / Disagreement

Participants generally agree on the need to simplify the problem for debugging purposes, but there are differing views on the specific changes needed to the code and the implications of the time variable on the results.

Contextual Notes

Some participants note that the dimensionality of the variables and the time frame are critical to obtaining valid results, but these aspects remain unresolved in terms of their specific impacts on the code's functionality.

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