To solve a problem with a recursion, it is needed to state that problem in recursion manner. That means to define it in self similar way.
In this case there are variables: time, acceleration and velocity. So, main problem has to be defined with sub-problems with same variables. There are 2 (quite similar) problems we can use:
- what is acceleration and velocity after t seconds from start.
- if we have given acceleration and velocity, what will be acceleration and
velocity after t seconds.
With that there are two (similar) recursion solutions:
def start_av(t):
if t == 0: return 0, 0
acc, vel = start_av(t-1)
thr = 10
c = -0.1
fric = c*vel
acc = thr + fric
return (acc, vel + acc)
def given_av(t, acc=0, vel=0):
if t == 0: return acc, vel
thr = 10
c = -0.1
fric = c*vel
acc = thr + fric
return given_av(t-1, acc, vel + acc)
print start_av(10)
print given_av(10)