Matlab simulation for planet motion
조회 수: 4 (최근 30일)
이전 댓글 표시
There were some attemps simulating planetary motion already, but I think mine is straightforward by solving and updating position via with Euler Cromers method:
t = 0;
while t < 10
pos1 = [1 2 3];
pos2 = [4 5 6];
m1 = 1;
m2 = 2;
G = 1;
r1 = pos1-pos2;
r2 = pos2-pos1;
F1 = G*m1*m2/norm(r1).^2.*r1/norm(r1);
F2 = G*m1*m2/norm(r2).^2.*r2/norm(r2);
dt = 0.1;
p1 = [0 100 0];
p2 = [0 100 0];
p1 = p1+F1.*dt;
p2 = p2+F2.*dt;
pos1 = pos1+p1/m1;
pos2 = pos2+p2/m2;
t = t+dt;
hold all;
plot3(pos1(1),pos1(2),pos1(3),'rx')
plot3(pos2(1),pos2(2),pos2(3),'bx')
end
However I don't really receive a plot of multiple data points, just 2 crosses remaining stationary. Also I get a 2-D plot even though I reverted to plot3
댓글 수: 1
KSSV
2022년 3월 16일
You can change it to 3D using view.
plot3(pos1(1),pos1(2),pos1(3),'rx')
plot3(pos2(1),pos2(2),pos2(3),'bx')
view(3)
채택된 답변
James Tursa
2022년 3월 16일
The initial condition for position and velocity need to be outside the loop, prior to loop entry.
추가 답변 (1개)
KSSV
2022년 3월 16일
t = 0;
m1 = 1;
m2 = 2;
G = 1;
pos01 = [1 2 3];
pos02 = [4 5 6];
pos1 = zeros([],3) ;
pos2 = zeros([],3) ;
iter = 0 ;
while t < 10
iter = iter+1 ;
r1 = pos01-pos02;
r2 = pos02-pos01;
F1 = G*m1*m2/norm(r1).^2.*r1/norm(r1);
F2 = G*m1*m2/norm(r2).^2.*r2/norm(r2);
dt = 0.1;
p1 = [0 100 0];
p2 = [0 100 0];
p1 = p1+F1.*dt;
p2 = p2+F2.*dt;
pos1(iter,:) = pos01+p1/m1;
pos2(iter,:) = pos02+p2/m2;
pos01 = pos1(iter,:) ;
pos02 = pos2(iter,:) ;
t = t+dt;
end
figure
hold on
plot3(pos1(:,1),pos1(:,2),pos1(:,3),'rx')
plot3(pos2(:,1),pos2(:,2),pos2(:,3),'bx')
view(3)
참고 항목
카테고리
Help Center 및 File Exchange에서 Earth and Planetary Science에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!