help calling a function

14 visualizaciones (últimos 30 días)
DiamondsRain
DiamondsRain el 11 de Feb. de 2021
Respondida: KSSV el 11 de Feb. de 2021
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

Respuesta aceptada

KSSV
KSSV el 11 de Feb. de 2021
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

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by