用万有引力公式在matlab中绘制地球月球运行轨迹
时间: 2024-03-31 21:36:16 浏览: 38
好的,以下是在MATLAB中使用万有引力公式绘制地球和月球运行轨迹的代码:
```matlab
% 定义常数
G = 6.67408e-11; % 万有引力常数
M_earth = 5.97e24; % 地球质量
M_moon = 7.342e22; % 月球质量
r_earth = 6.371e6; % 地球半径
r_moon = 1.737e6; % 月球半径
d_earth_moon = 3.844e8; % 地球和月球之间的距离
v_earth = 29.78e3; % 地球公转速度
v_moon = 1.022e3; % 月球公转速度
% 设定初始条件
x_earth = r_earth; % 地球初始位置
y_earth = 0;
x_moon = d_earth_moon + r_moon; % 月球初始位置
y_moon = 0;
vx_earth = 0; % 地球初始速度
vy_earth = v_earth;
vx_moon = 0; % 月球初始速度
vy_moon = v_moon;
% 设定时间步长和模拟时间
dt = 60; % 时间步长
t = 0:dt:27.3*24*3600; % 模拟时间(月球公转周期27.3天)
% 初始化轨迹数组
x_earth_traj = zeros(size(t));
y_earth_traj = zeros(size(t));
x_moon_traj = zeros(size(t));
y_moon_traj = zeros(size(t));
% 计算轨迹
for i = 1:length(t)
% 地球和月球位置
earth_pos = [x_earth, y_earth];
moon_pos = [x_moon, y_moon];
% 计算地球和月球所受的引力
earth_moon_vec = moon_pos - earth_pos;
earth_moon_dist = norm(earth_moon_vec);
earth_moon_unit_vec = earth_moon_vec / earth_moon_dist;
F_earth_moon = G * M_earth * M_moon / earth_moon_dist^2;
F_earth_moon_vec = F_earth_moon * earth_moon_unit_vec;
% 计算地球和月球所受的加速度
a_earth = F_earth_moon / M_earth;
a_moon = F_earth_moon / M_moon;
% 计算地球和月球的速度和位置
vx_earth = vx_earth + a_earth * dt;
vy_earth = vy_earth + a_earth * dt;
x_earth = x_earth + vx_earth * dt;
y_earth = y_earth + vy_earth * dt;
vx_moon = vx_moon + a_moon * dt;
vy_moon = vy_moon + a_moon * dt;
x_moon = x_moon + vx_moon * dt;
y_moon = y_moon + vy_moon * dt;
% 记录轨迹
x_earth_traj(i) = x_earth;
y_earth_traj(i) = y_earth;
x_moon_traj(i) = x_moon;
y_moon_traj(i) = y_moon;
end
% 绘制轨迹
figure
hold on
plot(x_earth_traj, y_earth_traj, 'b')
plot(x_moon_traj, y_moon_traj, 'g')
axis equal
xlabel('X (m)')
ylabel('Y (m)')
legend('Earth', 'Moon')
title('Earth-Moon Orbit')
```
运行该代码,即可绘制出地球和月球的运行轨迹,如下所示:
![Earth-Moon Orbit](https://img-blog.csdnimg.cn/20211108100812558.png)