free fall graph project

4 次查看(过去 30 天)
DiamondsRain
DiamondsRain 2021-2-12
I've been trying to get this to output a graph of free fall position vs time, but I just can't seem to get it right and was hoping someone could show me what i'm doing wrong.(for example if y0=25 at about 2 seconds ypos of the object should be 0)
If I left anything out let me know! thank you!
function Assignment5pt2()
%Graphing position in free fall vs time(by 1 sec)-
%-(in respect to the ground)
prompt='Input intial height in meters: ';
y0 = input(prompt);
Y = findypos(y0);
plot(Y)
title('Positions vs time')
xlabel('time(sec)')
ylabel('distance from ground(meters)');
end
function Y=findypos(y0)
t=0; %Time
g=9.81; %m/s^2
ypos=y0;
count=1;
Y(count)=y0;
while ypos >= 0
y=0.5*g*t.^2; %Y position from initial y
ypos=y0-y; %y pos from ground
t=t+0.5; %time step
count=count+1;
Y(count)=ypos;
end
end

回答(1 个)

Walter Roberson
Walter Roberson 2021-2-12
y=0.5*g*t.^2; %Y position from initial y
ypos=y0-y; %y pos from ground
g is negative, so when you do the subtraction you end up going up.
plot(Y)
No, your Y values are calculated every 1/2 second, you should not be plotting them as if you had calculated them every second. You should be keeping track of the times and return them along with the Y positions and using the times as the x values in the plot.
I also recommend that you change your 0.5 time increment to something smaller.
Also, your current code has already done the calculus to get the time/distance formula directly. That is not exactly wrong but probably the spirit of the assignment is that you should be modelling step by step, keeping track of current velocity, altering it by acceleration applied for a time, altering position by velocity applied for a time. Your current code would not, for example, be easily adaptable to having drag added in.
  3 个评论
DiamondsRain
DiamondsRain 2021-2-12
Thank you for the advice it helped a lot!
Walter Roberson
Walter Roberson 2021-2-12
If you do go step by step, keep in mind that you should be applying the average velocity of the step to the simulated position change, not the final velocity of the step.
delta velocity = acceleration times delta time
new velocity = old velocity plus delta velocity
delta position = (mean of old velocity and new velocity) times delta time
new position = old position plus delta position
which can be simplified

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by