Issue splitting a scirpt into a script and function

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')

 採用された回答

Birdman
Birdman 2018 年 3 月 26 日

0 投票

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 件のコメント

Kyle Oswin-Inglis
Kyle Oswin-Inglis 2018 年 3 月 26 日
Thanks for fast reply, but I just realise I forgot a crucial detail. The plotting must take place in a seperate script. Anyway you can help with this?
Birdman
Birdman 2018 年 3 月 26 日
Then do this:
1-Save your function like this:
function [distance,x,y,g] = 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);
end
and then
2- write this
[distance,x,y,g]=throwBall(4,pi/4);
figure
hold on
plot(x,y)
plot(x,g,'--', 'color', 'black')
xlabel('Distance (m)')
ylabel('Height (m)')
title('Ball trajectory')
Kyle Oswin-Inglis
Kyle Oswin-Inglis 2018 年 3 月 26 日
Didn't relaise it was so simple. I was told you can only get one output from the function. Thank you again!!
Birdman
Birdman 2018 年 3 月 26 日
You are welcome :)
Stephen23
Stephen23 2018 年 3 月 26 日
"I was told you can only get one output from the function."
It is a pity that people spread so much bad/incorrect advice about MATLAB. Don't believe anything people tell you about MATLAB, even if it is your professor! The MATLAB is the best place to learn how MATLAB works and what it does:
A M Saif Ben Kabir Amit
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
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 件)

カテゴリ

ヘルプ センター および File ExchangeParallel Computing Toolbox についてさらに検索

質問済み:

2018 年 3 月 26 日

編集済み:

2022 年 4 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by