The equation requires the previous output to calculate the next value.
Vi = V(i-1) t((c*(V(i-1)^2)/m)-g). With V(0)=0 and all other values being defined I am not sure how to write a loop code that would store the previous value and then calculate the next iteration. I've tried a def approach but I could never figure out how to recall the previous calculation to use in the next step.
Vi = Vo Dt*((c*(Vo**2)/m)-g)
return Vi
CodePudding user response:
Assuming t, c, m, and g are constants you can pass them to the recursive function:
def V(input, t, c, m, g) -> int:
if input == 0: return 0
return V(input - 1, t, c, m, g) t( (c * (V(input - 1, t, c, m, g) ** 2) / m) -g)
CodePudding user response:
A recursive solution, where you define each value of v relative to the prior value, might look like this:
def calculate_v(i):
if i == 0:
return 0
v = calculate_v(i - 1)
return v t * (c * v ** 2 / m - g)
whereas an iterative solution, where you just re-calculate v i times, would look like:
def calculate_v(i):
v = 0
for _ in range(i):
v = v t * (c * v ** 2 / m - g)
return v
