Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit c5e459e

Browse files
committed
Initial commit.
1 parent e17724f commit c5e459e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

ballistic.m

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function [T,motionTotal] = ballistic(p0, theta, phi, velocity) % angles are in radians
2+
% BALLISTIC(ICS, azimuth, elevation, v0)
3+
% Calculate the path of a ballistic object with air resistance.
4+
% aizimuth is in degrees along xy plane from the x-axis, elevation is in
5+
% degrees from the xy plane to the initial velocity vector.
6+
%
7+
% Equations via: http://www.cs.cornell.edu/~bindel/class/cs3220-s12/notes/lec27.pdf
8+
9+
g = [0,0,9.81]'; % m/s % xyz
10+
c = 0.45; %1/m; % scaled drag coefficient
11+
12+
% decompose heading & pitch into xyz magnitudes times velocity scalar
13+
v0 = velocity.*[cos(theta)*cos(phi), sin(theta)*cos(phi), sin(phi)]';
14+
ICS = [p0';v0];
15+
tFinal = v0(3)/g(3)+sqrt((v0(3)/g(3)).^2+2*p0(3)/g(3));
16+
% for testing: v = v0
17+
18+
% Ballistic model, considering air drag
19+
function rhs = dragModel(t,motion)
20+
v = motion(4:6); % last 3 terms are velocity
21+
rhs = [v; (-g-c.*abs(v).*v)]; %[vx,vy,vz,ax,ay,az]'
22+
end
23+
24+
% ODE options: stop when we hit zero
25+
function [value,isterminal, direction ] = hitground(t,y)
26+
value = y(3); % Check for zero crossings of z position
27+
isterminal = 1; % Terminate on a zero crossing
28+
direction = -1; % We only care about crossing y > 0 to y < 0
29+
end
30+
31+
odeopt = odeset('Events', @hitground);
32+
[T,XV] = ode45(@dragModel,[0,tFinal.^2],ICS,odeopt);
33+
34+
% add in acceleraton data.. because I can.
35+
motionTotal = horzcat(XV,[-c.*abs(XV(:,4:5)).*XV(:,4:5),-g(3)-c.*abs(XV(:,6)).*XV(:,6)]);
36+
37+
end

0 commit comments

Comments
 (0)