For loops for 2 variables. (Trajectory)
이전 댓글 표시
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
darova
2019년 11월 2일
You want 10 velocities for each angle (9*10 curves)?
Ahmed M'sallem
2019년 11월 2일
채택된 답변
추가 답변 (1개)
RICARDO DE BRAGANCA
2019년 11월 2일
편집: RICARDO DE BRAGANCA
2019년 11월 2일
0 개 추천
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
Ahmed M'sallem
2019년 11월 2일
편집: Ahmed M'sallem
2019년 11월 2일
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!