Prob with MATLAB code (Planetary motion)

In summary, to troubleshoot inaccurate results in MATLAB code for planetary motion, check initial conditions and parameters, look for coding errors, and ensure appropriate numerical methods are used. To plot a planet's trajectory, calculate its position at different time intervals and use the "plot" function. Multiple planet motion can be simulated by defining parameters and using nested for loops. Gravitational forces can be incorporated using the universal law of gravitation equation. MATLAB has built-in functions such as "plot", "ode45", and "quiver" for simulating planetary motion.
  • #1
cloudy14
3
0
Hi,

I am writing a code for planetary motion using ODE with runge-kutta.

Currently it has only two body, however, I could not get the initial values to fit in properly, the bodies will not move in circular orbital motion. They are moving towards each other.
at the v=[10 0;-10 0], whenever I try to add a value for the vy, the second body will disappear completely from the screen. Help please!

Code:
function orbit
global X v n G m       % Make these params. global.

n=2;    %No. of planets
G = 4*pi^2; % Grav. const
m=[4 4];  %m=[400 1.23e-2]; 
X=[0 0;50 0]; %initial positions
v=[10 0;-10 0]; %initail velocities %17.9979
state=zeros(n,4); %Empty the state memory
for k=1:n, %Forming the state matrix from the previous values
    state(k,:)=[X(k,1) X(k,2) v(k,1) v(k,2)];
end

nStep = 2000; %number of steps
tau = 0.01; %time step (yr)
time = 0;

for iStep=1:nStep
    for i=1:n
        state(i,:) = rk4(state(i,:),time,tau,@dEqs,G,i);
        %update new values of X and v
        X(i,:)=[state(i,1) state(i,2)];
        v(i,:)=[state(i,3) state(i,4)];
    end
time = time + tau;
xpos1(iStep)=X(1,1);
ypos1(iStep)=X(1,2);
xpos2(iStep)=X(2,1);
ypos2(iStep)=X(2,2);
%xpos3(iStep)=X(3,1);
%ypos3(iStep)=X(3,2);
clf('reset');
hold on;
axis on
axis equal
axis([-150 150 -150 150]);
plot(X(1,1),X(1,2),'bo','MarkerFaceColor','r','MarkerSize',12);
plot(X(2,1),X(2,2),'bo','MarkerFaceColor','y','MarkerSize',12);
%plot(X(3,1),X(3,2),'bo','MarkerFaceColor','r','MarkerSize',12);
plot(xpos1(1:iStep),ypos1(1:iStep));
plot(xpos2(1:iStep),ypos2(1:iStep));
%plot(xpos3(1:iStep),ypos3(1:iStep));

pause(0.001)


%  clf reset
%    set(gcf,'numbertitle','off','name',' n-body')
%    n = size(state,1);
%    s = max(sqrt(diag(state*state')));
%    if n <= 3, s = 2*s; end
%    axis([-s s -s s])
%    axis square

end


function F = dEqs(x,t,G,num) % First-order differential
global n m X

a = zeros(1,2); 
tiny=1e-20;
    for j=1:n,
        if j~=num;
        a(1) =G* (a(1)+ m(j)* (X(j,1)-x(1,1))/norm((X(j,1)-x(1,1))+tiny)^3 );%x component
        a(2) =G* (a(2)+ m(j)* (X(j,2)-x(1,2))/norm((X(j,2)-x(1,2))+tiny)^3 );%x component        
        end
    end

F = [x(1,3) x(1,4) a(1) a(2)]; 

% Runge-Kutta integrator (4th order)
function xout = rk4(x,t,tau,derivsRK,param,count)
half_tau = 0.5*tau;
F1 = feval(derivsRK,x,t,param,count);
t_half = t + half_tau;
xtemp = x + half_tau*F1;
F2 = feval(derivsRK,xtemp,t_half,param,count);
xtemp = x + half_tau*F2;
F3 = feval(derivsRK,xtemp,t_half,param,count);
t_full = t + tau;
xtemp = x + tau*F3;
F4 = feval(derivsRK,xtemp,t_full,param,count);
xout = x + tau/6.*(F1 + F4 + 2.*(F2+F3));
return;
 
Physics news on Phys.org
  • #2
It looks like you are trying to use the Runge-Kutta method to simulate the motion of two planets around a central point. When you set the initial velocities of the two planets to [10 0; -10 0], they will be moving towards each other and it is not possible to get them to move in a circular orbit. In order for two planets to move in a circular orbit around each other, their initial velocities must be such that the centripetal force balances the gravitational force. This can be calculated using the equations of motion. The acceleration of the two planets due to gravitational forces can be calculated by: a_1 = F_grav(m_2 / r^2)a_2 = F_grav(m_1 / r^2)Where F_grav is the gravitational force between the two planets and r is the distance between them. The centripetal acceleration of the two planets is given by: a_1 = v_1^2 / ra_2 = v_2^2 / rWhere v_1 and v_2 are the velocities of the two planets. The initial velocities of the two planets can then be calculated by setting the gravitational and centripetal accelerations equal to each other and solving for the velocities: v_1 = sqrt(G*m_2 / r)v_2 = sqrt(G*m_1 / r)Once you have the initial velocities, you can use the Runge-Kutta method to simulate the orbital motion of the two planets. I hope this helps.
 

1. Why is my MATLAB code for planetary motion not producing accurate results?

There could be several reasons for this. First, check to make sure that your initial conditions and parameters are accurately defined. Small errors in these values can greatly affect the results. Secondly, check your code for any errors or bugs, as even a small mistake can lead to incorrect results. Lastly, consider the numerical methods used in your code and make sure they are appropriate for the problem at hand.

2. How can I plot the trajectory of a planet in MATLAB?

To plot the trajectory of a planet, you will need to calculate the position of the planet at different time intervals using the equations of motion. Once you have the position data, you can use the "plot" function in MATLAB to create a plot of the planet's trajectory. Make sure to label your axes and add a title to your plot for clarity.

3. Can I simulate the motion of multiple planets in MATLAB?

Yes, it is possible to simulate the motion of multiple planets in MATLAB. You will need to define the initial conditions and parameters for each planet, and then use nested for loops to iterate through each planet's position and calculate the forces acting on it. You can then plot the trajectories of each planet using the "plot" function.

4. How can I incorporate gravitational forces into my MATLAB code for planetary motion?

To incorporate gravitational forces into your code, you will need to use the universal law of gravitation equation, F = (G*m1*m2)/r^2, where G is the gravitational constant, m1 and m2 are the masses of the two objects, and r is the distance between them. You will need to calculate the force on each planet due to all other planets in the system and use this force to update the position and velocity of each planet at each time step.

5. Are there any built-in functions in MATLAB that can help with simulating planetary motion?

Yes, MATLAB has several built-in functions that can be helpful for simulating planetary motion. These include the "plot" function for creating plots, the "ode45" function for solving differential equations, and the "quiver" function for visualizing vector fields. You can also use the "hold" function to overlay multiple plots on the same figure.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
955
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
888
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
27
Views
7K
Back
Top