How to; For loop of angle in projectile motion

24 views (last 30 days)
Aston
Aston on 20 Aug 2022
Commented: Aston on 20 Aug 2022
Im trying to get a for loop to run a projectile motion from an angle pi/6 to pi/3 and then run a function that finds the range for each and returns the maximum one (would be at 45 degrees), so far I believe I have the for loop run through the angles correctly but when the range function runs it does not display the corresponding range values, it only returns the value for pi/6, for the increment number of times:
g = 9.82; %gravity
v0 = 100; %inital velocity
theta0 = pi/6;
T = (2*v0*sin(theta0))/g; % Theoretical flight time
t = linspace(0,T,1001);
[r,v] = Projectile(t,v0,theta0); %link to projectile function
figure(1), plot(r(:,1),r(:,2));
xlabel('Distance (m)');
ylabel('Height (m)');
legend('Trajectory path');
R = FindRange(r); %link to find range function
disp("Range = " + (R) + "m");
m = max(r(:,2));%Max height
disp("Maximum Height = " + (m) + "m");
disp("Flight time = " + (T) + "sec");
for theta0 = pi/6:pi/180:pi/3
R1 = FindRange(r);
disp (R1)
end

Answers (2)

KSSV
KSSV on 20 Aug 2022
You are not updating the theta0 to find the range. You need to input theta0 to the function.
clc; clear all ;
g = 9.82; %gravity
v0 = 100; %inital velocity
theta0 = pi/6;
T = (2*v0*sin(theta0))/g; % Theoretical flight time
t = linspace(0,T,1001);
[r,v] = Projectile(t,v0,theta0); %link to projectile function
figure(1), plot(r(:,1),r(:,2));
xlabel('Distance (m)');
ylabel('Height (m)');
legend('Trajectory path');
R = FindRange(r); %link to find range function
disp("Range = " + (R) + "m");
m = max(r(:,2));%Max height
disp("Maximum Height = " + (m) + "m");
disp("Flight time = " + (T) + "sec");
for theta0 = pi/6:pi/180:pi/3
T = (2*v0*sin(theta0))/g; % Theoretical flight time
t = linspace(0,T,1001);
[r,v] = Projectile(t,v0,theta0); %link to projectile function
R1 = FindRange(r);
disp (R1)
end
  1 Comment
Aston
Aston on 20 Aug 2022
Ah okay I see, the problem I have now is in the FindRange function, if isempty is true, which returns the 'Solution end before the projectile hits the ground' error and im not sure why
function R = FindRange_template(r)
x = r(:,1);
y = r(:,2);
indx = find(y<0);
if isempty(indx);
error('Solution ends before projectile hits the ground');
end;
k = indx(1);
x1 = x(k-1);y1 = y(k-1);
x2 = x(k); y2 = y(k);
R = (x1+x2)/2;
end

Sign in to comment.


Sam Chak
Sam Chak on 20 Aug 2022
g = 9.82; % gravity
v0 = 100; % inital velocity
theta0 = pi/6;
T = (2*v0*sin(theta0))/g; % Theoretical flight time
t = linspace(0,T,1001);
[r, v] = Projectile(t,v0,theta0); % link to projectile function
Unrecognized function or variable 'Projectile'.
figure(1),
plot(r(:,1), r(:,2));
xlabel('Distance (m)');
ylabel('Height (m)');
legend('Trajectory path');
R = FindRange(r); %link to find range function
disp("Range = " + (R) + "m");
m = max(r(:,2));%Max height
disp("Maximum Height = " + (m) + "m");
disp("Flight time = " + (T) + "sec");
Suggest the for loop in this structure:
% Range of initial angle
theta0 = pi/6:pi/180:pi/3
for j = 1:length(theta0)
% Call function that uses theta0(j)
end
  1 Comment
Aston
Aston on 20 Aug 2022
what would that function for theta0(j) look like?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!