tan verses tand error in loop

1 view (last 30 days)
Jocelyn on 24 Nov 2020
Answered: Steven Lord on 24 Nov 2020
Hello,
I'm trying to figure out why the 'y' column in the table produced by my code is mainly zeros with two random numbers. There should be a value in every row except the first in the 'y' column and these numbers should either increase or decrease at a fairly consistent rate.
Below is my the table produced by my code for reference:
yards impact velocity (ft/s) time of flight (s) initial QE angle (degrees) angle at impact (degrees) y
_____ ______________________ __________________ __________________________ _________________________
0 2296 0 NaN 0 NaN
200 1955.2 0.28318 0.11685 -0.24691 0
400 1641.8 0.61807 0.26322 -0.59233 8.8818e-16
600 1355.7 1.0202 0.4507 -1.0906 0
800 1097 1.5122 0.69742 -1.8366 0
1000 865.71 2.1279 1.0331 -3.0045 0
1200 661.74 2.9206 1.5093 -4.9374 0
1400 485.12 3.9796 2.2225 -8.3629 0
1600 335.87 5.466 3.3713 -14.94 0
1800 213.98 7.7041 5.4225 -28.369 0
2000 119.45 11.457 9.6942 -52.786 2.2737e-13
I was trying to figure out what is causing this to happen in the 'y' column. I found that if I wrote 'tan(phi_o(i))' verses 'tand(phi_o(i))' than values were produced in the y - column.
I want the whole code to generate numbers in the table produced to be in degrees. If I remove the 'd' from the tan, then my code is no longer in degrees? Is the tan verse tand my issue? Or is there another error somewhere?
Below is my code for your reference.
mb = 198/7000; % mass of bullet /7000 IOT convert grains to lbs
db = 0.319/12; % diameter of bullet /12 IOT convert inch to feet
Vx_o = 2296; % muzzle velocity (ft/s)
K3 = 0.5;
p = 0.0751; % (lbm/ft^3) row - standard sea level met data
a = 1120; % (ft/s) standard sea level met data
% initial conditions
t = 0; % (s)
Vy_o = 0;
g = 32.174; %gravitational constant (ft/sec^2) [at sea level]
y_o = 0; % gun altitude
y = 0; % inital altitude of projectile
% find small k3
S = (pi*db^2)/4;
k3 = ((p*S)/(2*mb))*K3*sqrt(a);
% find Mach # to determine what set of flat fire equations to use
M = Vx_o/a; % M = 2.05 (low to moderate supersonic flight)
for i = 1:11
x(i) = (i-1)*200*3; % x3 = converting range from yards to feet
y(i) = (i-1)*200*3; % x3 = converting range from yards to feet
Vx(i) = (sqrt(Vx_o) - ((k3/2)*x(i)))^2; % striking velocity = x direction velocity
t(i) = (x(i)/Vx_o)*sqrt(Vx_o/Vx(i)); % time of flight
Vy(i) = (4*Vy_o)/((2+(k3*sqrt(Vx_o)*t(i))).^2) - g*(4*t(i)+(2*k3*sqrt(Vx_o)*t(i).^2)+ ((k3.^2*Vx_o*t(i).^3)/3))/((2+(k3*sqrt(Vx_o)*t(i))).^2); % y direction velocity
phi(i) = (atand(Vy(i)/Vx(i))); % impact angle ---> x60 to convert from degrees to minutes
phi_o(i) = (atand((0.5*g*t(i).^2*((1/3)*(2*sqrt(Vx(i)/Vx_o)+1)))/x(i))); % gun elevation angle above horizon = inital QE angle (minutes)
y(i) = y_o + x(i)*tand(phi_o(i))-(0.5*g*t(i).^2)*((1/3)*(1+(2*sqrt(Vx(i)/Vx_o))));
end
Range_Table = table(x(:)/3, Vx(:), t(:), phi_o(:), phi(:), y(:),...
'VariableNames',{'yards', 'impact velocity (ft/s)', 'time of flight (s)', 'initial QE angle (degrees)', 'angle at impact (degrees)', 'y'})

Steven Lord on 24 Nov 2020
phi_o(i) = (atand((0.5*g*t(i).^2*((1/3)*(2*sqrt(Vx(i)/Vx_o)+1)))/x(i)));
y(i) = y_o + x(i)*tand(phi_o(i))-(0.5*g*t(i).^2)*((1/3)*(1+(2*sqrt(Vx(i)/Vx_o))));
Part of the computation of y(i) is to take the tand of something that was computed by calling atand. If you look at the numerator of the body of the atand call that computes phi_o(i) it looks awfully similar to what you're subtracting from x(i)*tand(phi_o(i)) doesn't it? So if we define an intermediate variable we can write the expression for y(i) in terms of that temporary variable:
temp = 0.5*g*t(i).^2*((1/3)*(1+2*sqrt(Vx(i)/Vx_o)));
phi_o(i) = (atand(temp/x(i)));
y(i) = y_o + x(i)*tand(phi_o(i))-temp;
So y(i) looks like, for "nice" values of z = temp/x(i) where tand(atand(z)) is equivalent to z, to be y_o plus something that's effectively 0.
I would define temporary variables with descriptive names for some of the pieces of your calculations that you use repeatedly. This may help your code look closer to the equations in your textbook and/or may make it possible to simply read the code as English text and get a sense of what it's doing. For instance, it looks like some form of this quantity appears several times in your code. What does it represent?
sqrt(Vx_o/Vx(i))