help calling a function
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
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
0 Kommentare
Weitere Antworten (0)
Siehe auch
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!