Solving Orbital Motion Equations with RK4 Using MATLAB

In summary: What is x4? x4 is the vector of outputs. You should be plotting xvar4,yvar4, and dtheta.What is dx? dx is the vector of inputs. What is k1_4, k2_4, k3_4, and k4_4? k1_4, k2_4, k3_4, and k4_4 are the coefficients of the first, second, third, and fourth order ODEs, respectively.
  • #1
Jordan_Tusc
3
0
I'm trying to plot the solutions of the second order differential equation d^2R/dt^2 = GM/R^2 + Lz^2/R^3. I'm reducing this to a system of first order ODEs and then using RK4 to solve this system.

My code is given by
Code:
function RK4system()                                      
Tsim   = 10;           % simulate over Tsim seconds
h      = 0.001;         % step size
N      = Tsim/h;       % number of steps to simulate
x4      = ones(2,N);
t4      = zeros(1,N);
theta  = ones(1,N);
G      = 6.67*power(10,-11);
M      = 1.5*1.989*power(10,41);
Lz     = 2.623*power(10,2);
R_1    = 2.623*power(10,20);
U_1    = 0.5;
x(1,1) = U_1;          %IC_ODE_1
x(2,1) = (G*M/(power(R_1,2))+power(Lz,2)/(power(R_1,3)));  %IC_ODE_2

x4 = ones(2,N);
t4 = zeros(1,N);

for n = 1:N-1
   t4(n+1)      = t4(n) + h;
   k1_4         = Orbit(t4(n), x4(:,n));
   k2_4         = Orbit(t4(n)+0.5*h, x4(:,n)+k1_4*0.5*h);
   k3_4         = Orbit(t4(n)+0.5*h, x4(:,n)+0.5*k2_4*h);
   k4_4         = Orbit(t4(n)+h, x4(:,n)+k3_4*h);
   phi4         = (1/6)*(k1_4 + 2*k2_4 + 2*k3_4 + k4_4);
   x4(:,n+1)    = x4(:,n) + phi4*h;
   theta(:,n+1)  = theta(:,n) + h*Angle(t(n), theta(:,n));
   xvar4(:,n+1) = x2(:,n+1)*cos(t4(n));
   yvar4(:,n+1) = x2(:,n+1)*sin(t4(n));
end

figure()
plot(xvar4,yvar4, 'b-', 'LineWidth', 1)
end

function dtheta = Angle(t,R)
Lz = 100000*2.623*power(10,20);
dtheta(1) = Lz/(power(R(1),2));
end

function dx = Orbit(t,R)
G           = 6.67*power(10,-11);
M           = 1.5*1.989*power(10,41);
Lz          = 100000*2.623*power(10,20);
% Equations of motion
dx          = zeros(2,1);
dx(1)       = R(2);
dx(2)       = (G*M/(power(R(1),2))+power(Lz,2)/(power(R(1),3)));
end

I'm not getting the plot I want, If I set V = sqrt(GM/R) and Lz = R*sqrt(GM/R) I should get a circular orbit. If V > sqrt(GM/R) I should get a parabolic orbit. This is not what I'm getting.

Please help
 
Physics news on Phys.org
  • #2
Can you attach some plots for those cases?
Images can help us spot some common errors more quickly than reading code.

One obvious question, what does Orbit() mean?
Finally I usually refrain from using functions like power and for example dot.
Once you start running longer simulations this will slow down your program significantly due to checks built into those functions.
In one case I was able to decrease the runtime of a simulation by a factor 10 by using ##a^\prime.a## for finding the length of a vector.
 
  • #3
did you try running this rk4 routine against something like a harmonic oscillator to check and see if it gives correct answers. I also suspect that the constants are giving it problems. R1 is on the order of [itex] 10^{20}[/itex] and when you square and cube it then divide, your essentially getting zero for some of the terms.
 
  • #4
[itex]T_{sim} [/itex] is only 10 seconds... it takes a year to orbit the sun..., you barely moved.
 
  • #5
Where are you defining t?
 

FAQ: Solving Orbital Motion Equations with RK4 Using MATLAB

1. How do I plot an orbit on MATLAB?

To plot an orbit on MATLAB, you will need to first define the necessary parameters such as the position and velocity of the object, the gravitational constant, and the time step. Then, you can use the built-in functions such as "plot" and "hold" to create a plot of the orbit.

2. Can I plot multiple orbits on the same graph?

Yes, you can plot multiple orbits on the same graph by using the "hold" function to keep the previous plot and then adding the new orbit data using the "plot" function. You can also use different colors or line styles to differentiate between the orbits.

3. How do I add labels and a title to my orbit plot?

To add labels and a title to your orbit plot, you can use the "xlabel", "ylabel", and "title" functions. These functions allow you to specify the text and formatting for the labels and title of your plot.

4. Can I change the scale of my orbit plot?

Yes, you can change the scale of your orbit plot by using the "xlim" and "ylim" functions. These functions allow you to specify the range of values shown on the x-axis and y-axis of your plot, respectively.

5. How can I save my orbit plot as an image file?

To save your orbit plot as an image file, you can use the "saveas" function. This function allows you to specify the file type and name of the image file that you want to save your plot as.

Similar threads

Replies
2
Views
2K
Replies
2
Views
7K
Replies
1
Views
5K
Replies
2
Views
10K
Replies
4
Views
3K
Replies
1
Views
4K
Replies
10
Views
10K
Back
Top