Bike model with and without air resistance using matlab

1 view (last 30 days)
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.

Accepted Answer

pfb
pfb on 11 Apr 2015
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.

More Answers (1)

göker akdag
göker akdag on 4 Mar 2019
salak

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!