% 参数设置
T = 300; % 运动时间  
Tt = 0.01; % 仿真时间间隔
P = [0,0]; % 初始发射位置
M = 20; % 炮弹质量
C = 0.45; % 空气阻力系数
rou = 1.29; % 空气密度
D = 0.12; % 炮弹直径
S = pi*D^2/4; % 炮弹迎风面积
k = 0.5*C*rou*S/M; % 空气阻力加速度总系数
g = 9.8; % 重力加速度

% 已知落点距离
dist = 1670;

% 初始化变量
foundMatch = false;
lastTraj = [];
velocities = [];

% 初始速度范围
v0_start = 300;
v0_end = 500;
v0_step = 10;

% 遍历初始速度
for v0 = v0_start:v0_step:v0_end
    % 遍历发射角度  
    for angle = 0:0.05:1.1
    
        V = v0 * sqrt(2) * [cos(angle*pi/180), sin(angle*pi/180)]; % 计算飞行速度
        P = [0,0];

        % 存储轨迹点
        traj = []; 
        
        % 存储速度变化
        velocities = [];
        
        for t = 0:Tt:T
            if P(2) < 0
                break;
            end

            af = -k*[V(1)^2, V(2)^2];
            a = af + [0, -g];

            V = V + a*Tt;
            P = P + V*Tt;
            
            % 存储轨迹点
            traj = [traj; P]; 
            
            % 存储速度变化
            velocities = [velocities; norm(V)];
        end

        if abs(P(1) - dist) < 1
            foundMatch = true;
            break
        end
        
        lastTraj = traj;
    end 
    
    if foundMatch
        break
    end
end  

% 如果未找到匹配的角度,绘制最后一个角度下的轨迹
if ~foundMatch
    lastAngle = angle;
    traj = lastTraj;
end

% 绘制轨迹 
figure;
plot(traj(:,1), traj(:,2), '*');
axis equal; % 设置坐标轴间距相同
title('弹道轨迹');
xlabel('距离/m');
ylabel('高度/m');

% 绘制速度变化曲线
figure;
time = 0:Tt:T;
plot(time(1:length(velocities)), velocities);
title('炮弹速度变化');
xlabel('时间/s');
ylabel('速度/m/s');

% 打印结果
fprintf('落点距离:%d米;\n误差:%d米;\n对应发射角度:%d度\n对应初始速度:%d m/s\n', P(1),abs(P(1)-dist),lastAngle, v0);

原文地址: https://www.cveoy.top/t/topic/BEi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录