MATLAB: Bike model with and without air resistance using matlab

bicycle model; air resistance

Hi I'm trying to follow along in a computational physics book (Computational Physics; Nicholas Giordano 2nd edition) that attempts to model a bike with and without air resistance. I did everything that the book did but I got a completely different plot at the end.This is my code for the bike with air resistance.
function bike(v0,dt,tf)
t = 0:dt:tf;
v(1) = v0;
P = 400;
m = 70;
p = 1.225
A = 0.33
for i = 1:length(t)-1
v(i+1) = v(i) + (P/m*v(i)-(p*A*v(i)^2/2*m))*dt;
t(i+1) = t(i) + dt;
end
plot(t,v,'b');
title('Comparison of Euler approximation to actual solution')
xlabel('time')
ylabel('v')
disp(v(end));
P stands for power, m is mass, p is density of air, and A is the frontal area of the rider.
This is my code for the bike without air resistance.
function bike(v0,dt,tf)
t = 0:dt:tf;
v(1) = v0;
P = 400;
m = 70;
for i = 1:length(t)-1
v(i+1) = v(i) + (P/m*v(i))*dt;
t(i+1) = t(i) + dt;
end
plot(t,v,'b');
title('Comparison of Euler approximation to actual solution')
xlabel('time')
ylabel('v')
disp(v(end));
If someone could please help me out, I would really appreciate it.

Best Answer

The dimensions in your formulas do not look right. I think it is a matter of operator precedence. 1/2*3 is not the same as 1/(2*3).
Also, note that you build the time vector twice: once at the beginning
t = 0:dt:tf
and then in the loop
t(i+1) = t(i) + dt;
However this should be no problem.
Related Question