1

Ok, so I am trying to make a program that can find the time of a car when two cars are speeding away from each other at a certain speed, each with a different rate that is only given the rate using time=distance/rate. But I would need a expression with a unsolved, variable, to do problems like the following:

Car1 is going 20mph. Car2 is going 10mph faster than Car1. Car2 left one hour later than Car1. How long would it take Car2 to catch up with Car1?

And my work without a program:

t = Travel time total (in hours)

10*t = 20(t-1) # Note the 1 is for the 1 hour

# rearrange
10*t = 20*t - 20

# subtract 10*t from both sizes
0 = 10*t - 20

# add 20 to both sides
20 = 10*t

#divide both sizes by 10
2 = t

Is there a module that supports operations with undefined variables using the distributive property (i.e. which can solve equations like this one)?

If not, than could I have a small example of how I would use unidentified variables without a module? I just went over this in school.

Tom
  • 846
  • 5
  • 18
  • 30
  • 1
    duplicate for http://stackoverflow.com/questions/10499941/how-can-i-solve-equations-in-python – zenpoy Oct 11 '12 at 12:17
  • As a warning, please stop bouncing between rolling back edits to bump your question to the front page. This is an abuse of the edit system. – Brad Larson Oct 11 '12 at 20:47
  • I am sorry. I actually didn't know it did that. I was just happy cuz my question was answered. Sorry. – Tom Oct 11 '12 at 22:12

2 Answers2

6

The module you are looking for is called SymPy.

import sympy
t = sympy.Symbol('t') # set t as a variable/symbol
sympy.solve(sympy.Eq(10*t, 20*(t-1)), t) # which reads: solve the equation 10*t == 20*(t-1) for t.
# returns [2] (list of solutions to the equation)

(see some more quick examples of using SymPy).

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
1

Is this what you want?

def get_time(v1, v2, dt):
    t = (v2*dt)/(v2 - v1)
    return t

Then you can call the function with the velocities of car1, car2 and the offset, and it will return the time you want.

dvreed77
  • 2,217
  • 2
  • 27
  • 42