Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Setting constants for different projectiles with a for loop?

1 Ansicht (letzte 30 Tage)
bml727
bml727 am 28 Apr. 2020
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
I am creating a code that can solve for different projectiles. I am currently using the 'if' statements to set the constants depending on what type of projectile I want to evaluate. Is there a quicker way to accomplish this such as using a for loop?
global m cd A rho g v0x v0y theta x0 y0
m= [0.145 0.42 0.43]; % mass of projectile [kg]
cd= [0.3 0.5 0.25]; % drag coefficients
A= [0.00426 0.02318 0.038]; % cross sectional area of projectile [m^2]
V= [0.0002194 0.00462115 0.00573547]; % volume of projectile
rho= [m(1)/V(1) m(2)/V(2) m(3)/V(3)]; % density
g= 9.8; % gravity [m/s^2]
theta= 45*pi/180; % initial launch angle [rad]
v0y= [18*cos(theta) 18*cos(theta) 24*cos(theta)]; % initial velocity in y direction [m/s], average velocities found online
v0x= [18*cos(theta) 18*cos(theta) 24*cos(theta)]; % initial velocity in x direction [m/s]
x0= [0 0 0]; % initial horizontal position
y0= [1.8288 1.8288 0]; % initial vertical position
%set projectile, 1=baseball, 2=football, 3=soccer ball
proj=3;
if proj == 1;
m= m(1);
cd= cd(1);
A= A(1);
rho= rho(1);
x0= x0(1);
y0= y0(1);
v0x= v0x(1);
v0y= v0y(1);
elseif proj == 2;
m= m(2);
cd= cd(2);
A= A(2);
rho= rho(2);
x0= x0(2);
y0= y0(2);
v0x= v0x(2);
v0y= v0y(2);
elseif proj == 3;
m= m(3);
cd= cd(3);
A= A(3);
rho= rho(3);
x0= x0(3);
y0= y0(3);
v0x= v0x(3);
v0y= v0y(3);
end

Antworten (1)

Raunak Gupta
Raunak Gupta am 2 Mai 2020
Hi,
As mentioned by @darova, you can vectorize the code instead of using if-else loops. Below code might help.
global m cd A rho g v0x v0y theta x0 y0
m= [0.145 0.42 0.43]; % mass of projectile [kg]
cd= [0.3 0.5 0.25]; % drag coefficients
A= [0.00426 0.02318 0.038]; % cross sectional area of projectile [m^2]
V= [0.0002194 0.00462115 0.00573547]; % volume of projectile
rho= [m(1)/V(1) m(2)/V(2) m(3)/V(3)]; % density
g= 9.8; % gravity [m/s^2]
theta= 45*pi/180; % initial launch angle [rad]
v0y= [18*cos(theta) 18*cos(theta) 24*cos(theta)]; % initial velocity in y direction [m/s], average velocities found online
v0x= [18*cos(theta) 18*cos(theta) 24*cos(theta)]; % initial velocity in x direction [m/s]
x0= [0 0 0]; % initial horizontal position
y0= [1.8288 1.8288 0]; % initial vertical position
%set projectile, 1=baseball, 2=football, 3=soccer ball
proj=3;
m= m(proj);
cd= cd(proj);
A= A(proj);
rho= rho(proj);
x0= x0(proj);
y0= y0(proj);
v0x= v0x(proj);
v0y= v0y(proj);

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by