How can i add a distance to time plot in Matlab

I have a school project to simulate free fall of an object with air resistance. I'm just having trouble figuring out how to plot a distance / time graph. I dont have any distance variable in my code, do i have to add one or is there maybe some kind of work around?
clear all;
V0=0; % initial speed
m=5; % mass in kg
g=9.81; % gravity acceleration kg/m3
rho=1.2; % Air density
A=0.09; % Object area
cw=0.4; % Numerical drag coefficient
k=0.5*cw*rho*A; % Coefficient
N=200; % Time step
V=zeros(1,N); % Speed
V(1)=V0;
deltat=0.2;
for i=1:N-1
V(i+1)=V(i)+deltat*(g-(k/m)*V(i)^2);
end
t=(0:N-1)*deltat;
plot(t,V);
xlabel('time in sec');
ylabel('velocity in m/s');
legend ('Euler Method','location','south');

1 Comment

NOTE: User is deleting their comments. You can find the full thread here

Sign in to comment.

 Accepted Answer

You have velocity. Has your teacher taught you how to use time and velocity to get distance yet?

7 Comments

Yes i believe its s = vt + 0.5*at^2
clear all;
V0=0; % initial speed
m=0.62369; % mass in kg
g=9.82; % gravity acceleration kg/m3
rho=1.225; % Air density
A=0.18/4; % Object area
cw=0.6; % Numerical drag coefficient
k=0.5*cw*rho*A; % Coefficient
N=10; % Time step
V=zeros(1,N); % Speed
V(1)=V0;
deltat=0.2;
for i=1:N-1
V(i+1)=V(i)+deltat*(g-(k/m)*V(i)^2);
t=(0:N-1)*deltat;
d=8- V(i+1)*t + 0.5*(g-(k/m)*V(i)^2)*t.*t % 8 is the starting height
end
vterminal=sqrt(g*m/k); % Terminal velocity
plot(t,d);
xlabel('time in sec');
ylabel('velocity in m/s');
legend ('Euler Method','location','south');
I think this is what you mean right?
But I dont understand why my results vary when i change the Time step
I appreciate your help

EDIT: OP response:
Yes i believe its s = vt + 0.5*at^2
clear all;
V0=0; % initial speed
m=0.62369; % mass in kg
g=9.82; % gravity acceleration kg/m3
rho=1.225; % Air density
A=0.18/4; % Object area
cw=0.6; % Numerical drag coefficient
k=0.5*cw*rho*A; % Coefficient
N=10; % Time step
V=zeros(1,N); % Speed
V(1)=V0;
deltat=0.2;
for i=1:N-1
V(i+1)=V(i)+deltat*(g-(k/m)*V(i)^2);
t=(0:N-1)*deltat;
d=8- V(i+1)*t + 0.5*(g-(k/m)*V(i)^2)*t.*t % 8 is the starting height
end
d = 1×10
8.0000 7.8036 8.0000 8.5892 9.5712 10.9460 12.7136 14.8740 17.4272 20.3732
d = 1×10
8.0000 7.4128 7.2144 7.4047 7.9836 8.9513 10.3077 12.0528 14.1866 16.7091
d = 1×10
8.0000 7.0302 6.4370 6.2204 6.3804 6.9170 7.8302 9.1200 10.7865 12.8295
d = 1×10
8.0000 6.6633 5.6838 5.0615 4.7965 4.8887 5.3382 6.1448 7.3088 8.8299
d = 1×10
8.0000 6.3187 4.9693 3.9518 3.2662 2.9126 2.8908 3.2010 3.8431 4.8171
d = 1×10
8.0000 6.0016 4.3055 2.9117 1.8202 1.0311 0.5442 0.3597 0.4775 0.8977
d = 1×10
8.0000 5.7153 3.7008 1.9567 0.4828 -0.7208 -1.6541 -2.3172 -2.7100 -2.8324
d = 1×10
8.0000 5.4614 3.1603 1.0967 -0.7293 -2.3178 -3.6688 -4.7822 -5.6581 -6.2965
d = 1×10
8.0000 5.2398 2.6852 0.3362 -1.8074 -3.7453 -5.4777 -7.0046 -8.3259 -9.4416
vterminal=sqrt(g*m/k); % Terminal velocity
plot(t,d);
xlabel('time in sec');
ylabel('velocity in m/s');
legend ('Euler Method','location','south');
I think this is what you mean right?
But I dont understand why my results vary when i change the Time step
I appreciate your help
REPLY
Because you change the accuracy of your numerical approach. Because you have acceleration, your velocity is contantly changing. Larger time steps are less accurate because they assume a constant velocity during each deltat.
But the graph im getting have a decreasing derivatives, which means that the velocity also decreases. but in reality the velocity increases when you drop an object. What have i done wrong?
I don't know what you are modeling, but the velocity profile makes sense for something that is thrown up in the air. Initially the velocity is high, but slows due to gravity until it stops going up (V=0), it then accelerates as it falls back down.
Hmm. If I just copy and paste in your code, executing it, what is the velocity vector that I see?
V
V =
Columns 1 through 10
0 1.962 3.9207 5.8694 7.8016 9.711 11.592 13.437 15.243 17.005
Columns 11 through 20
18.717 20.376 21.979 23.524 25.008 26.43 27.788 29.083 30.314 31.482
Columns 21 through 30
32.588 33.632 34.617 35.544 36.414 37.23 37.995 38.71 39.377 39.999
Columns 31 through 40
40.579 41.118 41.619 42.085 42.517 42.917 43.287 43.63 43.948 44.241
Columns 41 through 50
44.512 44.762 44.993 45.206 45.402 45.583 45.75 45.904 46.045 46.175
Columns 51 through 60
46.295 46.405 46.507 46.6 46.686 46.765 46.837 46.904 46.965 47.021
Columns 61 through 70
47.073 47.12 47.164 47.204 47.241 47.275 47.306 47.334 47.36 47.385
Columns 71 through 80
47.407 47.427 47.445 47.463 47.478 47.493 47.506 47.518 47.529 47.539
Columns 81 through 90
47.549 47.557 47.565 47.572 47.579 47.585 47.591 47.596 47.601 47.605
Columns 91 through 100
47.609 47.613 47.616 47.619 47.622 47.624 47.627 47.629 47.631 47.633
Do you see a velocoty that decreases or one that increses? Strangely, it looks like V is increasing to me, but maybe this is that new math stuff. :) :)
Eventually, it seems to reach an asymptote, roughly at V=47.653.
Perhaps you are missing that you were computing VELOCITY. And that the derivative of velocity is acceleration, which is indeed decreasing with time. Of course, due to air resistance, the velocity will eventually reach a maximum (that is, terminal velocity) so the acceleration will go to zero. And that just means that eventually, the drag due to air resistance will exactly balance the acceleration due to gravity, once the faling object reaches that terminal velocity.
With some effort, one could even compute the terminal velocity directly from those formulas, finding the velocity where the two accelerations must exactly balance out.

My project was to calculate how long it would take a falling abject(Basketball) to reach the ground from a specefic height in this case 8m.
I think I found why I had decreasing derivatives it was because i had a minus after the 8 in :
d=8- V(i+1)*t + 0.5*(g-(k/m)*V(i)^2)*t.*t
I dont know if im thinking right but this is what I have now

clear all;
V0=0; % initial speed
m=0.62369; % mass in kg
g=9.82; % gravity acceleration kg/m3
rho=1.225; % Air density
A=0.18/4; % Object area
cw=0.6; % Numerical drag coefficient
k=0.5*cw*rho*A; % Coefficient
N=8; % Time step
V=zeros(1,N); % Speed
V(1)=V0;
deltat=0.15;
for i=1:N-1
V(i+1)=V(i)+deltat*(g-(k/m)*V(i)^2); % velocity
t=(0:N-1)*deltat; %Time
d=V(i+1)*t + 0.5*(g-(k/m)*V(i)^2)*t.*t % Distance that the Basketball have fallen
end
plot(t,d);
xlabel('time in sec');
ylabel('Distande in m');
legend ('Euler Method','location','south');
Now im getting that it takes the Ball around 0.7s secound to reach the ground from a height of 8m --> x = 0.7 when Y = 8
when we experimented in school the time was around 0.9s
Have I done anything wrong in my calculations?
I really appreciate your help

EDIT: OP's response:
My project was to calculate how long it would take a falling abject(Basketball) to reach the ground from a specefic height in this case 8m.
I think I found why I had decreasing derivatives it was because i had a minus after the 8 in :
d=8- V(i+1)*t + 0.5*(g-(k/m)*V(i)^2)*t.*t
I dont know if im thinking right but this is what I have now
clear all;
V0=0; % initial speed
m=0.62369; % mass in kg
g=9.82; % gravity acceleration kg/m3
rho=1.225; % Air density
A=0.18/4; % Object area
cw=0.6; % Numerical drag coefficient
k=0.5*cw*rho*A; % Coefficient
N=8; % Time step
V=zeros(1,N); % Speed
V(1)=V0;
deltat=0.15;
for i=1:N-1
V(i+1)=V(i)+deltat*(g-(k/m)*V(i)^2); % velocity
t=(0:N-1)*deltat; %Time
d=V(i+1)*t + 0.5*(g-(k/m)*V(i)^2)*t.*t % Distance that the Basketball have fallen
end
plot(t,d);
xlabel('time in sec');
ylabel('Distande in m');
legend ('Euler Method','location','south');
Now im getting that it takes the Ball around 0.7s secound to reach the ground from a height of 8m --> x = 0.7 when Y = 8
when we experimented in school the time was around 0.9s
Have I done anything wrong in my calculations?
I really appreciate your help
REPLY:
It depends what you want d to be. If it is the current height, then the proper equation is d=8-...
If you just want to know the distance the ball has traveled, then d=V... will give you that.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!