Using MATLAB to analyse freefall.

2.5k Views Asked by At

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?

2

There are 2 best solutions below

7
On BEST ANSWER

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.

7
On

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.