help calling a function
조회 수: 14 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!