1
$\begingroup$

A ball is thrown up into the air at an initial speed v of $30$ $m /s $. Assume the acceleration due to friction against the air is directed opposite to the direction of the velocity, and proportional to the speed of the ball, such that the total acceleration of the ball is $ a = -g - kv$, where $g$ is the acceleration due to gravity and $v$ is the speed of the ball. How long does it take for the ball to reverse its motion? Take $ k = 0.1$ and solve the problem using a while loop.

So I wrote the following algorithm in order to solve this problem in MATLAB. I defined a function $T$ that takes the variables $v, k, dt$ as arguments and returns the time $T$ until the ball reverses motion; with the help of the while loop as described below:

MATLAB

function T = freefallX(v,k,dt); %defining the function
time = 0;                       %the start time
k = 0.1;                        %some constant of proportionality
dt = 0.01;                      %fixed time step
v = 30;                         %initial velocity of the ball
while v >= 0                    %defining the while-condition
    a = -9.81 -(k*v);           %net acceleration of ball
    dv = a*(dt);                %velocity steps
    v = v + dv;                 %velocity of ball at any instant
    time = time + (dt);         %increase time in steps of dt 
end
T = time                        %give the time until reversal of motion, i.e, v=0

My Problem

The result I get from the above algorithm is dependent on the choice of the time-step "$dt$". For different time-steps I get different values of $T$. Now I wanted to see how these different $T$'s vary with time-step. To this end, I tried to plot a graph of the different $T$'s against the corresponding $dt$. All attempts were in vain. How can I go about achieving this?

$\endgroup$
0

2 Answers 2

2
$\begingroup$

Something like this should work:

Ts=zeros(6,1); 
dts=10.^[-6 -5 -4 -3 -2 -1]; 
for i=1:6 
  Ts(i)=freefallX(30,0.1,dts(i)); 
end 
plot(dts,Ts).

This computes $T$ for $dt=10^{-6},10^{-5},\dots,10^{-1}$. Note that if you're going to take $v$ and $k$ as arguments to the function "freefall" then you don't need to define them within the body of the function, they get defined in the outer scope.

$\endgroup$
7
  • $\begingroup$ this does not seem to yield the desired result. If I run it it asks me to input the values for k, v and dt, then it returns one value for Ts, followed by a string of 5 zeros. No plot. $\endgroup$
    – 2good4this
    Sep 20, 2015 at 22:10
  • 1
    $\begingroup$ @2good4this If your freefallX function is defined exactly as in the OP, except with the $v$ and $k$ assignments removed, that doesn't make any sense. $\endgroup$
    – Ian
    Sep 20, 2015 at 22:12
  • $\begingroup$ @2good4this Indeed, I just wrote up a quick version myself. I get a plot with slightly different values of T for each value of dt (ranging from about 2.67 to about 2.7). MATLAB sets the y scale to be fine enough that I can see the differences. $\endgroup$
    – Ian
    Sep 20, 2015 at 22:17
  • $\begingroup$ I thought the same. I have tried to run it several times it still does the same thing. And yes, my function is defined exactly as in the OP. One question, what is the purpose of that 's' on "Ts" and "dts"? $\endgroup$
    – 2good4this
    Sep 20, 2015 at 22:21
  • $\begingroup$ @2good4this I don't know what to tell you, then. Also, the "s" is not essential, I just put it there to remind me that these are vectors, not single numbers. $\endgroup$
    – Ian
    Sep 20, 2015 at 22:22
1
$\begingroup$

Once you have your function defined, you can store different values of the function for different values of $dt$. For instance, let's say you've already defined your function, and have values of $v$ and $k$ stored. Let's say you also want to vary $dt$ from $.01$ to $.1$ in increments of $.01$. Then you can do:

dt = .01:.01:.1;
T = zeros(size(dt));

for ii = 1:length(dt)
  T(ii) = freefallX(v,k,dt(ii));
end

plot(dt,T);

EDIT: Change your function to:

function T = freefallX(v,k,dt); %defining the function
time = 0;                     %the start time
% DELETE THIS k = 0.1;        %some constant of proportionality
% DELETE THIS dt = 0.01;      %fixed time step
% DELETE THIS v = 30;         %initial velocity of the ball
while v >= 0                  %defining the while-condition
  a = -9.81 -(k*v);           %net acceleration of ball
  dv = a*(dt);                %velocity steps
  v = v + dv;                 %velocity of ball at any instant
  time = time + (dt);         %increase time in steps of dt 
end

T = time                        %give the time until reversal of motion, i.e, v=0

With those three lines, you're overwriting the user-input values of $v$, $k$ and $dt$, so you need to delete them.

$\endgroup$
7
  • $\begingroup$ The block of code you suggested plots a straight horizontal line of T against dt. This must be because it doesn't calculate a corresponding value of T for each dt, but plots one value of T against all the dt's. $\endgroup$
    – 2good4this
    Sep 20, 2015 at 21:54
  • 1
    $\begingroup$ @2good4this It isn't one value, there is a vector of $T$ values here. But they should all be rather close together, so the scale of the figure may not resolve the differences. $\endgroup$
    – Ian
    Sep 20, 2015 at 22:12
  • $\begingroup$ @Marcus , The vector of T values has entries that are the same value throughout, regardless of the values of dt. I made the scale on the figure as fine as it could get and I still got a horizontal line. $\endgroup$
    – 2good4this
    Sep 20, 2015 at 22:49
  • 1
    $\begingroup$ @2good4this, You overwrite the inputs of $v,k$ and $dt$ in the first few lines of freefallX(). As written, this function has zero dependency on $v,k$ and $dt$. $\endgroup$
    – Marcus M
    Sep 20, 2015 at 23:04
  • $\begingroup$ @Marcus, I could use a bit of elaboration on your last comment. $\endgroup$
    – 2good4this
    Sep 21, 2015 at 8:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .