因此,我尝试使用Matlab中的ode45求解器对简单的弹丸运动进行建模(无空气阻力等)。到目前为止,这是我的代码:
function [x,y] = trajectory_without_AR_45(v0,theta, dt)
%Path of mortar without air resistance using ode45
g = 9.81;
t_start = 0;
t_end = 100;
%Initial Conditions
y01 = 0; %initial x
y02 = v0 * cos(theta); %finding initial velocity in x direction
y03 = 0; %initial y
y04 = v0 * sin(theta); % finding intial velocity in y direction
y0 = [y01;y02;y03;y04];
%Derivatives
dy1 = y0(2); %vx
dy2 = 0; %ax
dy3 = y0(4); %vy
dy4 = -g; %ay
dy = [dy1;dy2;dy3;dy4];
%Using ODE45
f = @ (t, y) (dy);
solution = ode45(f, [t_start, t_end], y0);
t = t_start : 0.01: t_end;
y = deval(solution, t);
plot (y(:,1), y(:,3)); %plotting trajectory
end
但是,我得到的图只是一条直线,显然是不正确的。任何帮助将不胜感激。
答案 0 :(得分:2)
我认为您没有正确调用ode求解器,您似乎只是在使用初始条件来计算dy
,这是错误的。最好将ode函数放在单独的函数(文件)中。例如,以下内容位于missile.m
中:
function dY = missile(t,Y)
g = 9.81;
dY(1) = Y(2); %vx
dY(2) = 0; %ax
dY(3) = Y(4); %vy
dY(4) = -g; %ay
end
然后您将按以下方式致电:
t_start = 0;
t_end = 2.3; % changed to something more consistent with when y<0
v0 = 10; % made up
theta=pi/4; % made up
y01 = 0; %initial x
y02 = v0 * cos(theta); %finding initial velocity in x direction
y03 = 0; %initial y
y04 = v0 * sin(theta); % finding intial velocity in y direction
y0 = [y01;y02;y03;y04];
opts = odeset('MaxStep',0.01,'InitialStep',0.001);
[t,y] = ode45(@missile, [t_start, t_end], y0, opts);
plot(t,y)
legend('x','dx','y','dy')
给出以下结果: