## 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 = 55;            ## 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]

## 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)];
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);

## Make two plots
##    1) vanalytic and v superimposed
##    2) vanalytic-v for better visualization

figure(1);
plot(t,vanalytic,';analytic result;',t,v,';numeric result;');
figure(2);
plot(diff(vanalytic-v'),';difference;');
