## Velocity profile of a cyclist in the absence of drag

## Define the constants of the problem in the correct units
v0 = 4;             ## initial velocity of the cyclist [m/s]
P  = 400;           ## power output of the cyclist [Watts]
mc = 70;            ## weight of cyclist [kg]
mb = 15;            ## weight of bike [kg]
m = mb+mc;          ## total weight [kg]
ttotal = 300;       ## total time of simulation [sec] 
dt = 0.1;           ## time step [sec]
C = 0.5;            ## drag coefficient(unitless)
A = 0.33;           ## frontal area of bike + cyclist [m^2]
rho = 1.29;         ## density of air at sea level [kg/m^3]

## Calculate the number of steps that need to be taken to cover a time
## interval of ttotal. Make it an integer by rounding down (or up, it
## doesn't make a difference)
Nsteps = floor(ttotal/dt);

## Initialize the velocity array. At the moment it's a single number but it
## will become an array as e loop 
v=v0;

## Iterate the finite difference loop
for n=1:Nsteps
  v = [v; P*dt/(m*v(n))+ v(n)-0.5*C*A*rho*v(n)*v(n)*dt/m];
endfor

## For plotting purposes, define the time axis.
t=[0:Nsteps]*dt;

## We'd like to plot out result against the analytical result
vanalytic = sqrt(2*P*t/m+v0^2);

## Plot vanalytic and v superimposed
plot(t,vanalytic,';analytic result;',t,v,';numeric result;');
