【问题标题】:Modelling Projectile Motion using Matlab ode45使用 Matlab ode45 对弹丸运动进行建模
【发布时间】:2018-12-18 16:24:05
【问题描述】:

所以我尝试使用 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

但是,我得到的情节只是一条显然不正确的直线。任何帮助将不胜感激。

【问题讨论】:

  • am304 的回答是正确的,这里的根本是你的f 实际上并不依赖于ty,它是constant 向量@987654325 @。你可以定义ff = @(t,Y) [Y(2); 0; Y(4); -9.81];

标签: matlab


【解决方案1】:

我认为您没有正确调用 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')

给出以下结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多