2
我試圖在Matlab中通過拖動來模擬子彈運動。一切都完美無缺......除非我無法弄清楚當「子彈」擊中地面時如何讓它停止。在Matlab中使用ode45的子彈運動
我最初嘗試了一個迭代循環,定義了一個數據數組,並清空了該數組的單元格,以便y值爲負值時....不幸的是,頌歌解算器並不太喜歡那樣。
這裏是我的代碼
function [ time , x_position , y_position ] = shell_flight_simulator(m,D,Ve,Cd,ElAng)
rho=1.2; % kg/m^3
g=9.84; % acceleration due to gravity
A = pi.*(D./2).^2; % m^2, shells cross-sectional area (area of circle)
function [lookfor,stop,direction] = linevent(t,y);
% stop projectile when it hits the ground
lookfor = y(1); %Sets this to 0
stop = 1; %Stop when event is located
direction = -1; %Specify downward direction
options = odeset('Events',@event_function); % allows me to stop integration at an event
function fvec = projectile_forces(x,y)
vx=y(2);
vy=y(4);
v=sqrt(vx^2+vy^2);
Fd=1/2 * rho * v^2 * Cd * A;
fvec(1) = y(2);
fvec(2) = -Fd*vx/v/m;
fvec(3) = y(4);
fvec(4) = -g -Fd*vy/v/m;
fvec=fvec.';
end
tspan=[0, 90]; % time interval of interest
y0(1)=0; % initial x position
y0(2)=Ve*cos(ElAng); % vx
y0(3)=0; % initial y position
y0(4)=Ve*sin(ElAng); % vy
% using matlab solver
[t,ysol] = ode45(@projectile_forces, tspan, y0);
end
end
x = ysol(:,1);
vx = ysol(:,2);
y = ysol(:,3);
vy = ysol(:,4);
plot(x,y, 'r-');
xlabel('X Position (m)');
ylabel('Y Position (m)');
title ('Position Over Time');
end
我想,當Y = 0和停止彈,這將定義一個事件,但它不會做任何事情。我究竟做錯了什麼?
謝謝,我看了看,並試了一下。不幸的是,我仍然無法得到它。我用我的新嘗試更新了我的問題。 – Erica
不應該使用'lookfor = y(3)'作爲事件函數中的一個條件嗎? y(3)是y軸,當彈丸擊中地面時,它是y軸去零... – bla
哦!哇,多麼愚蠢的錯誤。謝謝! – Erica