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?
Something like this should work:
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.