Issue splitting a scirpt into a script and function
4 views (last 30 days)
Show older comments
So I currently have a script that throws a ball at a set inital height h, velovity v and angle theta. It also returns the value of x at which the ball hits the ground and plots the path it takes when it is finished. I now must turn it into a function and have the user input the starting velocity and angle and return a value called distance and plot the in a seperate script. I'm having issues with getting the returned value back and am unsure how to use it effectivly. g is to print a black dashed line All I know is I am not suppsoed to plot the graph inside the function. Help would be much appreciated. My old code lookes like
h=1.5;
a=9.8;
v=4;
theta=pi/4;
t=0:0.001:1;
x=v*cos(theta).*t;
y=h+v*sin(theta).*t-0.5*a*t.^2;
ypos=find(y < 0, 1);
xpos=x(:,ypos);
g=zeros(1,length(x));
fprintf('The ball hit the ground at %0.4f meters. \n', xpos);
figure
hold on
plot(x,y)
plot(x,g,'--', 'color', 'black')
xlabel('Distance (m)')
ylabel('Height (m)')
title('Ball trajectory')
0 Comments
Accepted Answer
Birdman
on 26 Mar 2018
Simply write this:
function distance = throwBall(v,theta)
h=1.5;
a=9.8;
t=0:0.001:1;
x=v*cos(theta).*t;
y=h+v*sin(theta).*t-0.5*a*t.^2;
ypos=find(y < 0, 1);
distance=x(:,ypos);
g=zeros(1,length(x));
fprintf('The ball hit the ground at %0.4f meters. \n', distance);
figure
hold on
plot(x,y)
plot(x,g,'--', 'color', 'black')
xlabel('Distance (m)')
ylabel('Height (m)')
title('Ball trajectory')
end
and call it from command line as
distance=throwBall(4,pi/4)
and observe the results.
7 Comments
A M Saif Ben Kabir Amit
on 21 Apr 2020
How can I implemet this ques after the afterr the completing of function distance = throwBall(v,theta)
h=1.5;
a=9.8;
t=0:0.001:1;
x=v*cos(theta).*t;
y=h+v*sin(theta).*t-0.5*a*t.^2;
ypos=find(y < 0, 1);
distance=x(:,ypos);
g=zeros(1,length(x));
fprintf('The ball hit the ground at %0.4f meters. \n', distance);
figure
hold on
plot(x,y)
plot(x,g,'--', 'color', 'black')
xlabel('Distance (m)')
ylabel('Height (m)')
title('Ball trajectory')
end
If you wrote the ball throwing script for question 2 in Credit Task 1, turn it into a function. Write a function file named DTask1_f.m and make the function declaration that takes v and theta as inputs and returns the distance at which the ball hits the ground: distance = DTask1_f(v, theta). The initial height of the ball is at 1.8 m. We generally don’t want functions to plot things every time they run, so remove the figure command and any plotting commands from the function. To be able to simulate a wide range of v and theta, make the time go until 10 sec and add an if statement that will display the warning ‘The ball does not hit the ground in 10 seconds.’ if that turns out to be the case (use isempty). Also, if the ball doesn’t hit the ground in 10 seconds, you should return NaN as the distance. To test your function, write a script DTask1.m to throw the ball with the same velocity v=4 m/s but different angles theta = 0:60 and plot the distance as a function of angle theta. The plot should look something like the figure below. You will need to run DTask1_f within a loop in order to calculate the distances for various thetas. Change velocity v=60 m/s, test if your program displays the warnings and plot the figure.
John Liew
on 18 Apr 2022
Edited: John Liew
on 22 Apr 2022
function distance = fileName(v,theta)
h = 1.2;
g = 9.8;
t = linspace(0,10);
x = v.*cos(theta .*(pi./180)).*t;
y = h+ v.*sin(theta.*(pi./180)).*t - 0.5.*g_acc.*(t.^2);
index = find(y<0,1);
if isempty (index)
warning("The ball does not hit the ground in 10s.");
end
end
clear
v = 3;
theta = 0:1:60;
for k = 1:61;
distance(k) = DTask1_f(v,theta(k));
end
plot(theta,distance);
More Answers (0)
See Also
Categories
Find more on Parallel Computing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!