0

I'm trying to solve for an equation in Python without using any scipy features. c = 5 and the equation is c = 10 - 20(exp(-0.15*x) - exp(-0.5*x)). How do I solve for x with a tolerance of .0001.

Pardon my intro level programming here guys. This is the first class I've ever taken.

from math import exp c = 5 def x(c): c = 10 - 20(exp*(-0.15*x) - exp*(-0.5*x)) return x(5)

stevenmiller
  • 79
  • 10
  • 1
    That's great, but you forgot to ask a question. This is a question answer site. – Burhan Khalid Nov 06 '17 at 05:13
  • Duly noted. thanks – stevenmiller Nov 06 '17 at 05:15
  • 5
    @stevenmiller Show what you have tried, SO is not a coding service. There are many algorithms like Newton-Raphson that solve this type of problem. – eyllanesc Nov 06 '17 at 05:20
  • 2
    Possible duplicate of [Python - Implementing a numerical equation solver (Newton-Raphson)](https://stackoverflow.com/questions/20659456/python-implementing-a-numerical-equation-solver-newton-raphson) – eyllanesc Nov 06 '17 at 05:21

2 Answers2

1

You might want to have a look at SymPy. It's a dedicated algebraic symbol manipulation library for Python with a BSD license. If you're looking for a "stock"/standard library solution, then as others have mentioned you're going to have to do some homework and potentially implement your own solver.

As a closing thought, unless this is a class assignment or your boss has a pathological hatred of third-party open source libraries, there's really no good reason not to use one of the SciPy packages. IIRC, they're largely implemented as highly-optimized C binaries wrapped in Python modules, so you get blazingly fast performance and the ease-of-use of a Python API.

J. Boley
  • 81
  • 1
  • 9
  • Thanks, I can't use SymPy for this program. I'm trying to come up with my own codes but I'm a very poor coder with this stuff. First class I've taken. – stevenmiller Nov 06 '17 at 05:46
  • Thank you again. This is an assignment and I'm just extremely terrible at coding. – stevenmiller Nov 06 '17 at 06:08
0

It seems like you want to implement this "from scratch." A few hints:

  • We can simplify this a bit with algebra. What you really want is to find x such that exp(-0.15*x) + exp(-0.5*x) - 0.2 = 0
  • For a given value of x, you know how much error you have. For example, if x = 1, then c(1) = 1.267, so your error is 1.267. You need to keep "guessing" values until your error is less than 0.0001.
  • Math tells us that this function is monotonically decreasing; so, there is no point checking answers to the left of 1.

Hopefully you can solve it from these hints. But this is supposed to be an answer, so here is the code:

def theFunction(x): return exp(-0.15*x) + exp(-0.5*x) - 0.2
error = 1.267
x = 1
littleBit = 1
while (abs(error) > 0.0001):
  if error > 0: x += littleBit
  else: x -= littleBit
  oldError = error
  error = theFunction(x)
  if (error*oldError < 0): littleBit *= 0.5 
print x

Note, the last three lines in the loop are a little bit 'clever' -- an easier solution would be to just set littleBit = 0.00001 and keep it constant throughout the program (this will be much slower, but will still do the job). As an exercise, I recommend trying to implement it this simpler way, then time how long it takes both ways, and see if you can figure out where the time savings comes in.

cag51
  • 178
  • 4
  • 14
  • That was great info but I'm honestly not very good at this. I've been trying to come up with the edits and iterations you suggested but I'm not having any luck. – stevenmiller Nov 06 '17 at 07:41
  • OK, I updated the answer to provide more detail, this should actually run in Python. Not sure what to recommend it terms of learning how to do this on your own though, I would have thought the hints were sufficient. – cag51 Nov 06 '17 at 21:35