help calling a function

14 次查看(过去 30 天)
DiamondsRain
DiamondsRain 2021-2-11
回答: KSSV 2021-2-11
I'm working on a project where I graph the position vs time of a object in free fall and I seem to be stuck calling the function and am a bit lost. I am required to use both a function and loop aswell. Thank you!
%Graphing position in free fall vs time(by 1 sec)
%(in respect to the ground)
prompt = 'Please give intial velocity in m/s: ';
v0 = input(prompt);
prompt = 'Input intial height in meters: ';
y0 = input(prompt);
[plot(t,ypos)] = height(v0,y0)
function height = findypos(v0,y0)
t = 0; %Time
g = 9.81; %m/s^2
while ypos >= 0
y = y0+v0*t-0.5*g*t.^2; %Y position from initial y
ypos = y0-y; %y pos from ground
t=t+1; %time step
end
plot(t,ypos)
end

采纳的回答

KSSV
KSSV 2021-2-11
You need to proceed something like shown below:
function main()
%Graphing position in free fall vs time(by 1 sec)
%(in respect to the ground)
prompt = 'Please give intial velocity in m/s: ';
v0 = input(prompt);
prompt = 'Input intial height in meters: ';
y0 = input(prompt);
Y = findypos(v0,y0) ;
plot(Y)
end
function Y = findypos(v0,y0)
t = 0; %Time
g = 9.81; %m/s^2
ypos = y0 ;
count = 1 ;
Y(count) = y0 ;
while ypos >= 0
y = y0+v0*t-0.5*g*t.^2; %Y position from initial y
ypos = y0-y; %y pos from ground
t=t+1; %time step
count = count+1 ;
Y(count) = ypos ;
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by