MATLAB Answers

Changing variables within loop equations

1 view (last 30 days)
Jocelyn
Jocelyn on 21 Nov 2020
Commented: VBBV on 22 Nov 2020
Hi,
After 700m in my loop I need to adjust some of the inital conditions within my code. So I made two loops and was expecting that the first loop would display a table from 0 to 700m and the second table would display 800m-2000m.
My second loop is starting from 0 instead of 800m, and I''m not sure why since I set the 'i' starting at 9.
I do like that both of the tables could be printed into one table. I would like to be able to plot this information on a graph in the end and I think having one table with all the data in it may be easier to get matlab to plot it.
Thank you for your assistance.
%Projectile with sabot
mp = 18.2984; % (lbm) (8.30 kg)
dp = 0.393701; % diameter of projectile (feet) (120mm)
Vx_o = 5413.386; % muzzle velocity (ft/s) (1650 m/s)
p = 0.0751; % (lbm/ft^3) standard sea level met data
a = 1120; % (ft/s) standard sea level met data
Cd = 0.247; % (ref table in notes called cd)
% initial conditions
t = 0; % (s)
Vy_o = 0; % (ft/s)
g = 32.17405; %gravitational constant (ft/sec^2) [at sea level]
y_o = 0; % gun altitude
y = 0; % inital altitude of projectile
M = Vx_o/a;
S = (pi*dp^2)/4;
K2 = M*Cd;
k2 = (p*S/(2*mp))*K2*a;
V = Vx_o;
Fd = (0.5*p*V^2*S*Cd)/g; % convert to lbf
%Creating Range Table (Only looking at projectile with Sabot)
for i = 1:8
x(i) = (i-1)*100*3; % x3 to convert from range in yards to feet
Vx(i) = Vx_o - k2*x(i); % x velocity
t(i)= (x(i)/Vx_o)*(1/(1-(Vx(i)/Vx_o)))*log(Vx_o/Vx(i)); % time
phi_o(i) = atand(1/x(i)*(-y_o + (g/2)*t(i).^2*(2/(log(Vx_o/Vx(i))))*(1 - ((1- (Vx(i)/Vx_o))/(log(Vx_o/Vx(i))))))); % gun elevation angle above horizon = inital QE angle
Vy(i) = (Vy_o+(g/k2))*exp(-k2*t(i))-g/k2;
phi(i) = (atand(Vy(i)/Vx(i)));
M(i) = Vx(i)/a;
end
Range_Table = table(x(:)/3, Vx(:), t(:), phi_o(:), phi(:),Vy(:),M(:),...
'VariableNames',{'yards', 'impact velocity (ft/s)', 'time of flight (s)', 'initial QE angle (degrees)', 'angle at impact (degrees)', 'Vy', 'Mach'})
% Sabot discards at 700m
% Below information is for penetrating rod
mpr = 8.8; % (lbm) (4 kg ref download 120mm KE W A1)
dpr = 0.124672; % diameter of projectile (feet) (38mm)
Cd = 0.305; % (ref table in notes called cd)
M = 4.2071;
K2 = M*Cd;
S = (pi*dpr^2)/4;
k2 = (p*S/(2*mpr))*K2*a;
for i = 9:21
x(i) = (i-1)*100*3; % x3 to convert from range in yards to feet
Vx(i) = Vx_o - k2*x(i); % x velocity
t(i)= (x(i)/Vx_o)*(1/(1-(Vx(i)/Vx_o)))*log(Vx_o/Vx(i)); % time
phi_o(i) = atand(1/x(i)*(-y_o + (g/2)*t(i).^2*(2/(log(Vx_o/Vx(i))))*(1 - ((1- (Vx(i)/Vx_o))/(log(Vx_o/Vx(i))))))); % gun elevation angle above horizon = inital QE angle
Vy(i) = (Vy_o+(g/k2))*exp(-k2*t(i))-g/k2;
phi(i) = (atand(Vy(i)/Vx(i)));
M(i) = Vx(i)/a;
end
Range_Table = table(x(:)/3, Vx(:), t(:), phi_o(:), phi(:),Vy(:),M(:),...
'VariableNames',{'yards', 'impact velocity (ft/s)', 'time of flight (s)', 'initial QE angle (degrees)', 'angle at impact (degrees)', 'Vy', 'Mach'})
First Table prints like:
Range_Table =
8×7 table
yards impact velocity (ft/s) time of flight (s) initial QE angle (degrees) angle at impact (degrees) Vy Mach
_____ ______________________ __________________ __________________________ _________________________ _______ ______
0 5413.4 NaN NaN NaN NaN 4.8334
100 5313.2 0.055938 0.009554 -0.019228 -1.783 4.7439
200 5213 0.11294 0.019351 -0.039195 -3.5661 4.6544
300 5112.8 0.17105 0.029401 -0.059944 -5.3491 4.565
400 5012.5 0.23031 0.039717 -0.081523 -7.1321 4.4755
500 4912.3 0.29076 0.050309 -0.10398 -8.9151 4.386
600 4812.1 0.35247 0.061191 -0.12738 -10.698 4.2965
700 4711.9 0.41547 0.072376 -0.15177 -12.481 4.2071
Second Table prints like:
yards impact velocity (ft/s) time of flight (s) initial QE angle (degrees) angle at impact (degrees) Vy Mach
_____ ______________________ __________________ __________________________ _________________________ _______ ______
0 5413.4 NaN NaN NaN NaN 4.2071
100 5313.2 0.055938 0.009554 -0.019228 -1.783 0
200 5213 0.11294 0.019351 -0.039195 -3.5661 0
300 5112.8 0.17105 0.029401 -0.059944 -5.3491 0
400 5012.5 0.23031 0.039717 -0.081523 -7.1321 0
500 4912.3 0.29076 0.050309 -0.10398 -8.9151 0
600 4812.1 0.35247 0.061191 -0.12738 -10.698 0
700 4711.9 0.41547 0.072376 -0.15177 -12.481 0
800 4532.3 0.51931 0.10224 -0.20717 -16.388 4.0467
900 4509.8 0.58567 0.1154 -0.23423 -18.436 4.0266
1000 4487.3 0.65236 0.12865 -0.26155 -20.485 4.0066
1100 4464.9 0.71938 0.14198 -0.28915 -22.533 3.9865
1200 4442.4 0.78674 0.15541 -0.31704 -24.582 3.9664
1300 4420 0.85444 0.16892 -0.3452 -26.63 3.9464
1400 4397.5 0.92249 0.18253 -0.37365 -28.679 3.9263
1500 4375 0.99088 0.19623 -0.4024 -30.727 3.9063
1600 4352.6 1.0596 0.21002 -0.43144 -32.775 3.8862
1700 4330.1 1.1287 0.2239 -0.46078 -34.824 3.8662
1800 4307.7 1.1982 0.23789 -0.49042 -36.872 3.8461
1900 4285.2 1.268 0.25196 -0.52038 -38.921 3.8261
2000 4262.8 1.3382 0.26614 -0.55065 -40.969 3.806

  0 Comments

Sign in to comment.

Accepted Answer

VBBV
VBBV on 22 Nov 2020
Edited: VBBV on 22 Nov 2020
L = 1:21; % use a vector for iteration /counter
for i = 1:1:L(8) % access them via L
... % your code
end
for i = L(9):1:L(end) % access counter via L
... % your code
end
Try this

  2 Comments

Jocelyn
Jocelyn on 22 Nov 2020
I tried your suggested way and it did not resolve the issue. The exact same tables that I included with my inital post/question were generated.
Do you have any other suggestions?
VBBV
VBBV on 22 Nov 2020
For the second table you need some changes as below
for i = 9:21
xx(i) = (i-1)*100*3; % x3 to convert from range in yards to feet
Vxx(i) = Vx_o - k2*xx(i); % x velocity
tt(i)= (xx(i)/Vx_o)*(1/(1-(Vxx(i)/Vx_o)))*log(Vx_o/Vxx(i)); % time
phii_o(i) = atand(1/xx(i)*(-y_o + (g/2)*tt(i).^2*(2/(log(Vx_o/Vxx(i))))*(1 - ((1- (Vxx(i)/Vx_o))/(log(Vx_o/Vxx(i))))))); % gun elevation angle above horizon = inital QE angle
Vyy(i) = (Vy_o+(g/k2))*exp(-k2*tt(i))-g/k2;
phii(i) = (atand(Vyy(i)/Vxx(i)));
MM(i) = Vxx(i)/a;
end
xx(xx==0) = [];
Vxx(Vxx == 0) = [];
tt(tt==0) =[];
phii(phii==0) =[];
Vyy(Vyy==0) = [];
phii_o(phii_o ==0) = [];
MM(MM==0) = [];
Range_Table = table(xx(:)/3, Vxx(:), tt(:), phii_o(:), phii(:),Vyy(:),MM(:),...
'VariableNames',{'yards', 'impact velocity (ft/s)', 'time of flight (s)', 'initial QE angle (degrees)', 'angle at impact (degrees)', 'Vy', 'Mach'})

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!