![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/288237/image.png)
Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Setting constants for different projectiles with a for loop?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
1 commentaire
Réponses (1)
Raunak Gupta
le 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);
0 commentaires
Cette question est clôturée.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!