Mathematica - using NIntegrate within NDSolve

  • Context: Mathematica 
  • Thread starter Thread starter maves
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary
SUMMARY

This discussion focuses on using Mathematica's NIntegrate and NDSolve functions to compute orbits of constant energy in a gravitational potential represented by a toroidal shape. The user transformed a system of second-order differential equations into first-order equations and encountered issues with the trajectory plots, which appeared linear instead of circular or elliptical. Key corrections included ensuring the gravitational force was attractive and properly defining the integrals for the gravitational potential. The final equations for NDSolve were adjusted to reflect these corrections, aiming to achieve accurate orbital representations.

PREREQUISITES
  • Familiarity with Mathematica 12.0 syntax and functions
  • Understanding of differential equations and their numerical solutions
  • Knowledge of gravitational potential theory and integration techniques
  • Basic experience with plotting in Mathematica
NEXT STEPS
  • Explore advanced features of NDSolve in Mathematica for complex systems
  • Learn about numerical integration techniques in Mathematica, focusing on NIntegrate
  • Study gravitational potential theory, specifically for non-spherical bodies
  • Investigate methods for visualizing trajectories in Mathematica, including ParametricPlot
USEFUL FOR

Researchers, physicists, and mathematicians working on orbital mechanics, computational physics, or anyone utilizing Mathematica for solving complex differential equations in gravitational contexts.

maves
Messages
17
Reaction score
0
Hello,
I am computing orbits of constant energy in a gravitational potential of a complicated planet (sth like a torus).

I need to solve a system of 2nd order differential equations, that look something like
\ddot x=-\frac{\partial \phi}{\partial x} and the same for y and z. The problem is that ∅ is a complicated integral that can not be solved analitically. I transformed the system of equations to a system of 1st order equations using new variables x1, x2, y1, y2, z1, z2 (is that even necessary?) and tried to solve them using NDSolve. The solutions I am looking for should be lying in particular planes, so I put for instance z=0 and then defined the integrals (already partially derived):

int1[x1_?NumericQ, y1_?NumericQ] :=
NIntegrate[((R (x1 - R Cos[\[Phi]]))/((x1 - R Cos[\[Phi]])^2 + (y1 - R Sin[\[Phi]])^2 + (0 -
Z)^2))^(3/2), {\[Phi], 0, 2 Pi}, {R, b, b + c}, {Z, R-(b+c), -R + (b + c)}];
int2[x1_?NumericQ, y1_?NumericQ] :=
NIntegrate[((R (y1 - R Sin[\[Phi]]))/((x1 - R Cos[\[Phi]])^2 + (y1 - R Sin[\[Phi]])^2 + (0 -
Z)^2))^(3/2), {\[Phi], 0, 2 Pi}, {R, b, b + c}, {Z, R-(b+c), -R + (b + c)}];

and then used these in NDSolve:
s = NDSolve[{
x1'[t] == x2[t],
y1'[t] == y2[t],
x2'[t] == int1[x1[t], y1[t]],
y2'[t] == int2[x1[t], y1[t]],
x1[0] == 9.6, x2[0] == 0, y1[0] == 0, y2[0] == 1.52}, {x1, x2, y1,
y2}, {t, 0, 100}][[1]],

having chosen some random initial values (well, not exactly random, I believe x1-the initial position in direction x - should be close to the planet (9.5), y1 is not needed and y2 - the initial velocity in direction y - should be "something") .

I plotted the solutions {x(t),y(t)}:
ParametricPlot[Evaluate[{x[t], y[t]} /. s], {t, 0, 100},
PlotRange -> All, AxesOrigin -> {0, 0},
AxesLabel -> {"x1[t]", "y1[t]"}]

which is the trajectory of a particle in my gravitational potential ∅. The initial instruction tells me to find the orbits of lowest (constant) energy (so, as I understand, by changing the initial conditions/values), but when I plot my solutions, I always get a linear line, now matter what i put into the initial values. I don't know, whether the problem is in my understanding of the problem, the part written in Mathematica or somewhere else. I would be very glad if anyone could help me out, and I apologize for the long post, I hope I made it clear, what the problem is.
 
Physics news on Phys.org
So I still have hope, that maybe someone could help me :) What I figured out until now is, that i forgot to take into account one minus sign, which made everything wrong, as the gravitational force in my calculations was repulsive and so the trajectory was hyperbolic (looked like linear on first sight). I also left the expression in the second-order form.

So, the right expressions for integrals in xy plane are:
1) Firstly, I define the first integral, which is equal to -\frac{\partial \phi}{\partial x}: (This "?NumericQ" must be here, if I want the program to proceed without evaluating the integral for a specific value of x and y. I integrate over \Phi, R, Z and leave x and y "free", so that I will be able to evaluate the integrals at the points that NDSolve will calculate.

int1[x_?NumericQ, y_?NumericQ] :=
NIntegrate[((R (x - R Cos[\[Phi]]))/((x - R Cos[\[Phi]])^2 + (y - R Sin[\[Phi]])^2 + (0 -
Z)^2))^(3/2), {\[Phi], 0, 2 Pi}, {R, b, b + c}, {Z, R-(b+c), -R + (b + c)}];

2) Similarly, I define the second integral, which is equal to -\frac{\partial \phi}{\partial y}:

int2[x_?NumericQ, y_?NumericQ] :=
NIntegrate[((R (y - R Sin[\[Phi]]))/((x - R Cos[\[Phi]])^2 + (y - R Sin[\[Phi]])^2 + (0 -
Z)^2))^(3/2), {\[Phi], 0, 2 Pi}, {R, b, b + c}, {Z, R-(b+c), -R + (b + c)}];

Now I can write my set of equations and set the initial values, for instance:

s = NDSolve[{
x''[t] == int1[x[t], y[t]],
y''[t] == int2[x[t], y[t]],
x[0] == 9.6, x'[0] == 0, y[0] == 0, y'[0] == 1.5}, {x, x', y,
y'}, {t, 0, 10}][[1]]



And plot my solutions:
ParametricPlot[Evaluate[{x[t], y[t]} /. s], {t, 0, 10}]

x[0] has been chosen close to the toroid (9.5), because the potential is lowest near the toroid. The orbits in xy plane should be circular and go "around" the torus (like a satellite).
For comparison, I also plotted the torus in the xy plane, as the orbits should be circular or at least elliptical, and they seem completely wrong - like if there was no torus at all, they cross the planet, which is of course wrong - something like the case shown in attachment, remember, this is the xy plane!

I would be very glad if anyone could help me here - is there anything wrong with my code? but what could it be, as the code is not exactly rocket science -; or at least tell me, if you think my thread was posted to the wrong section.
 

Attachments

  • tor21.jpg
    tor21.jpg
    9.6 KB · Views: 739
Last edited:

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 10 ·
Replies
10
Views
6K
Replies
8
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K