Issue splitting a scirpt into a script and function
26 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Birdman
2018-3-26
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 个评论
A M Saif Ben Kabir Amit
2020-4-21
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
2022-4-18
编辑:John Liew
2022-4-22
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel Computing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!