Make an array from function input and output

I have a function that inputs velocity and angle, and outputs the distance the ball travels. Function below:
function distance = DTask1_f (v, angle)
h = 1.5;
g = 9.8;
t = linspace (0,10,1000);
x=v*cos(angle*pi/180)*t;
y=h+v*sin(angle*pi/180)*t-0.5*g*t.^2;
ind = find (x == min(x(y<0)));
fprintf ('The ball hits the ground at a distance of %5.4f meters.', x(ind));
distance = x(ind);
end
I now need to write a script that uses the function, and while using a set value for velocity, will graph angle and distance over a set of angles. My script so far:
for angle = 10:13
v = 4;
y = DTask1_f (v, angle);
x = [angle];
a = [x;y]
end
figure
plot(x,y)
xlabel('Angle (deg)');
ylabel('Distance (m)');
title('Ball Trajectory');
I think I need to first make an array using the angle (input) and distance (output), and then graph the array, but im not sure how to make the array. At the moment, it only gives me the last two points.
Apologies if this is a simplistic question, but thanks for the help.

 Risposta accettata

Stephan
Stephan il 1 Set 2018
Modificato: Stephan il 1 Set 2018
Hi,
use:
angle_range = 10:1:13
for k = 1:length(angle_range)
v = 4;
a(k,2) = DTask1_f (v, angle_range(k))
a(k,1) = angle_range(k)
end
figure
plot(a(:,1),a(:,2))
xlabel('Angle (deg)');
ylabel('Distance (m)');
title('Ball Trajectory');
If you want to extend or vary the range of angle or make other stepsize for angle modify first line.
Best regards
Stephan

3 Commenti

Hey, thanks that worked.
The last thing im trying to do, is display a different message if the ball is in the air for more than 10 seconds. I think i need to do this by editing the function, so i have tried to add in an if loop:
function distance = DTask1_f (v, angle)
h = 1.5;
g = 9.8;
t = linspace (0,10,50000);
x=v*cos(angle*pi/180)*t;
y=h+v*sin(angle*pi/180)*t-0.5*g*t.^2;
if t > 10
fprintf ('Nan');
else ind = find (x == min(x(y<0)));
fprintf ('The ball hits the ground at a distance of %5.4f meters.\n', x(ind));
distance = x(ind);
end
end
This is my script, slightly edited the answer you provided:
angle = 0:60;
for k = 1:length(angle)
v = 60;
a(k,2) = DTask1_f (v, angle(k));
a(k,1) = angle(k);
end
figure
plot(a(:,1),a(:,2))
xlabel('Angle (deg)');
ylabel('Distance (m)');
legend('v = 4 m/s')
title('Ball Trajectory');
Thank you
Stephan
Stephan il 2 Set 2018
Modificato: Stephan il 2 Set 2018
Hi,
this part has a dynamic legend now for v - No need to write it by hand everytime it is changed:
angle = 0:60;
for k = 1:length(angle)
v = 60;
a(k,2) = DTask1_f (v, angle(k));
a(k,1) = angle(k);
end
figure
plot(a(:,1),a(:,2))
xlabel('Angle (deg)');
ylabel('Distance (m)');
legend(['v = ', num2str(v), ' m/s'])
title('Ball Trajectory');
The function should now work as you wanted it to do, with some more informations when fprinf command gives output:
function distance = DTask1_f (v, angle)
h = 1.5;
g = 9.81;
t = linspace (0,15,50000);
x=v*cos(angle*pi/180)*t;
y=h+v*sin(angle*pi/180)*t-0.5*g*t.^2;
ind = find (x == min(x(y<0)));
distance = x(ind);
if t(ind) <= 10
fprintf ('Angle: %1.0f Degrees - The ball hits the ground at a distance of %5.4f meters in %5.4f seconds. \n',angle, x(ind), t(ind));
else
fprintf ('NaN - Angle: %1.0f Degrees - Time: %5.4f seconds \n', angle, t(ind));
end
end
Best regards
Stephan
Thanks, that worked :)

Accedi per commentare.

Più risposte (1)

angle = 10:13 ;
x = angle ;
y = zeros(size(angle)) ;
for i = 1:length(angle)
v = 4;
y(i) = DTask1_f (v, angle(i));
end
figure
plot(x,y)
xlabel('Angle (deg)');
ylabel('Distance (m)');
title('Ball Trajectory');

3 Commenti

Your answer also worked, but i found the above answer easier to understand.
5 years later...I found this answer to be easier to follow. Thanks for the help, if you are still out there.
@Zach Russell yes I am here... :) you can acknowledge by voting the answer.

Accedi per commentare.

Categorie

Scopri di più su 2-D and 3-D Plots in Centro assistenza e File Exchange

Richiesto:

il 1 Set 2018

Commentato:

il 12 Ott 2023

Community Treasure Hunt

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

Start Hunting!

Translated by