help calling a function

14 Ansichten (letzte 30 Tage)
DiamondsRain
DiamondsRain am 11 Feb. 2021
Beantwortet: KSSV am 11 Feb. 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

Akzeptierte Antwort

KSSV
KSSV am 11 Feb. 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

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by