For loops for 2 variables. (Trajectory)
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
When I write this code, the result is:
Angle(d) V(m/s) Height(m) Range(m)
10.00 9.90 0.15 11.71
20.00 9.90 0.58 13.15
30.00 9.90 1.25 14.01
40.00 9.90 2.07 14.03
50.00 9.90 2.93 13.02
60.00 9.90 3.75 10.94
70.00 9.90 4.42 7.91
80.00 9.90 4.85 4.15
90.00 9.90 5.00 0.00
I'd like the velocity (V(m/s)) to not be constant, but to appear the same way the angle variable appears.
Please let me know if there is anything wrong wiht my code.
%{
Possible height and range, assuming that angle of projection
and vertical position are constant.
%}
%g)
%Both initial velocity and and angles are independent variables
g = -9.8;
xi = 0;
yi = 5;
vMAX = sqrt(-yi.*g*2);
fprintf('Angle(d)\t V(m/s)\t height(m)\t Range(m)\n');
for theta2=10:10:90 %Nested for loops to compute each individual value for the plot
for vi =linspace(0,vMAX,10);
% Velocity components from the first problem
vix = vi.*cosd(theta2);
viy = vi.*sind(theta2);
%Total time from first problem
a = (1/2)*g;
b = viy;
c = yi;
tFinal2 = roots([a, b, c]);
tFinal2 = max(tFinal2);
t = (linspace(0, tFinal2, 10))';
%Compute x and y values(copied from problem 1)
x2 = xi+vix.*t;
y2 = yi+viy.*t+(1/2)*g.*t.^2;
%Range of trajectory
R = Range2(xi,vix,tFinal2);
%Max Height
yMAX = MaxHeight2(viy);
end
fprintf("%.2f \t\t %.2f\t\t %.2f\t\t %.2f\n",theta2,vi,yMAX,R)
%Plot
subplot(3,2,4)
plot(x2, y2,'LineWidth',1.5);
hold on
grid on;
xlabel('X');
ylabel('Y');
title ('Projectile with varying angle & initial velocity (h)')
axis equal;
%Create boundaries in the first quadrant
y2(y2 < 0) = 0;
end
%{
yMAX function
function yMAX = MaxHeight2(viy)
yMAX = (viy.^2)/(9.8*2);
end
%Range function
function R = Range2(xi,vix,tFinal)
R = xi+vix*tFinal;
end
%}
2 commentaires
Réponse acceptée
darova
le 3 Nov 2019
Piece of a code
n = 10;
theta2 = linspace(10,90,n);
vi = linspace(0,vMAX,n);
for i = 1:n
% Velocity components from the first problem
vix = vi(i).*cosd(theta2(i));
viy = vi(i).*sind(theta2(i));
%% ...
fprintf("%.2f \t\t %.2f\t\t %.2f\t\t %.2f\n",theta2(i),vi(i),yMAX,R)
% Plot ...
end
0 commentaires
Plus de réponses (1)
RICARDO DE BRAGANCA
le 2 Nov 2019
Modifié(e) : RICARDO DE BRAGANCA
le 2 Nov 2019
Your "vi" variable is varying for each value of "theta2" but your "fprintf" is outside the loop on "vi" so it prints only the last value of "vi" for each "theta2" (MATLAB prints 9.90).
Swap the "fprintf" code line with the "end" code line above it. You will see all values of "vi" for each value of "theta2".
Besides, when asking a question, please post the code of all used function, so your code can be reproduced.
Best regards,
Ricardo de Braganca
1 commentaire
Voir également
Catégories
En savoir plus sur Get Started with Phased Array System Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!