目前我的任务是编写一个 Matlab 脚本,以数值方式解决从各种角度发射炮弹的问题,无论是否有空气摩擦。当然,我正在尝试思考如何解决空气摩擦分量,但首先我需要创建没有空气摩擦的模型,我终生无法弄清楚它是如何分解的。这是我的代码:
clc;
clear all;
close all;
% Setting initial conditions such as angle, number of sampling intervals,
% initial x- and y-positions, etc.
N_points=10000;
total_time=200;
angle_initial=20;
gravity_initial=9.81;
velocity_initial=700;
y_initial=0;
x_initial=0;
% Set time vector, x-position and y-position vectors, their initial values,
% and sampling interval.
delta_t=total_time/N_points;
y=zeros(N_points,1);
x=zeros(N_points,1);
time=zeros(N_points,1);
y(1)=y_initial;
x(1)=x_initial;
%Begin loop to actually calculate y-position through time.
%
for step=1:N_points-1
time(step+1)=time(step)+delta_t;
y(step+1)=y(step)+((-gravity_initial*sind(angle_initial)*time(step+1)+velocity_initial*sind(angle_initial))*delta_t);
end
% Find all values of y-position vector less than zero and eliminate them
% from vector, thus giving us only points before the projectile 'hits' the
% ground.
a_1=find(y<0);
r_1=[a_1];
y(r_1)=[];
b_1=length(y)
%Create x-position vector starting from zero to maximum possible range of
%projectile, separated into equal intervals corresponding to number of
%positive elements in the y-position vector. This way, the x- and y-vectors
%will match when they are plotted together.
x=linspace(0,((velocity_initial^(2))/gravity_initial)*sind(2*angle_initial),b_1);
%plot x- and y-position vectors.
plot(x,y,'r')
我无法弄清楚为什么该功能会“炸毁”;运行此代码时,射弹的范围是正确的,但高度不正确。我真的看不出我在哪里出错了;我在网上浏览了很多例子和其他东西,无论我尝试什么变化,它们都表现出太高的轨迹高度。谁能提供关于我哪里出错的线索?