For loops for 2 variables. (Trajectory)

2 visualizzazioni (ultimi 30 giorni)
Ahmed M'sallem
Ahmed M'sallem il 2 Nov 2019
Risposto: darova il 3 Nov 2019
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 Commenti
darova
darova il 2 Nov 2019
You want 10 velocities for each angle (9*10 curves)?
Ahmed M'sallem
Ahmed M'sallem il 2 Nov 2019
no i would like the first angle to go with first velocity, and the second angle to go with the second velocity and so on.

Accedi per commentare.

Risposta accettata

darova
darova il 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

Più risposte (1)

RICARDO DE BRAGANCA
RICARDO DE BRAGANCA il 2 Nov 2019
Modificato: RICARDO DE BRAGANCA il 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 Commento
Ahmed M'sallem
Ahmed M'sallem il 2 Nov 2019
Modificato: Ahmed M'sallem il 2 Nov 2019
yet in that case, it shows this.
Angle(d) V(m/s) height(m) Range(m)
10.00 0.00 0.00 0.00
10.00 1.10 0.00 1.12
10.00 2.20 0.01 2.27
10.00 3.30 0.02 3.48
10.00 4.40 0.03 4.73
10.00 5.50 0.05 6.02
10.00 6.60 0.07 7.37
10.00 7.70 0.09 8.76
10.00 8.80 0.12 10.21
10.00 9.90 0.15 11.71
and it repeats for each angle, which is not what I want. I'd like each value of each variable to appear on their respective row. (1st velocity first row, 2nd velocity second row, same goes for angle).

Accedi per commentare.

Categorie

Scopri di più su Get Started with Phased Array System Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by