473,378 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How do I handle mixed input to a Python function, ie: 5x?

TMS
119 100+
I'm working through the bisection method to find the root of a function. I'm asking patience (yet again) because I haven't used this method for a while. I found the formula on wikipedia.

Pseudocode is as follows:

start loop
Do While (xR - xL) > epsilon

'calculate midpoint of domain
xM = (xR + xL) / 2

'Find f(xM)
If ((f(xL) * f(xM)) > 0 Then
'throw away left half
xL = xM
else
'throw away right half
xR = xM
End If
Loop

I'm trying to make my way through this and I have some questions about how Python handles an equation like 5x^8 - 3x^4 + x - 2? If I try it from the shell I get an error message from the x since its with the 5, so I assume it confused about what I'm asking.

A friend told me to use eval(function) but I'm not sure how to use it in my code, or if it is even the best way to go.

Mind you, this is a starting point. There will be MANY more questions before I'm done with this assignment.

The function is to look like this:

def root_find(f, xLo, xHi, eps)

So, if I test the above so that f = 5x**8 - 3x**4 + x - 2. by saying y0 = f(xLo) I will get an error, right? How do I format the input so the function will use it? Am I even asking the right question? (I'm so lost!)


thank you for helping
Jan 21 '07 #1
24 1941
bartonc
6,596 Expert 4TB
I'm working through the bisection method to find the root of a function. I'm asking patience (yet again) because I haven't used this method for a while. I found the formula on wikipedia.

Pseudocode is as follows:

start loop
Do While (xR - xL) > epsilon

'calculate midpoint of domain
xM = (xR + xL) / 2

'Find f(xM)
If ((f(xL) * f(xM)) > 0 Then
'throw away left half
xL = xM
else
'throw away right half
xR = xM
End If
Loop

I'm trying to make my way through this and I have some questions about how Python handles an equation like 5x^8 - 3x^4 + x - 2? If I try it from the shell I get an error message from the x since its with the 5, so I assume it confused about what I'm asking.

A friend told me to use eval(function) but I'm not sure how to use it in my code, or if it is even the best way to go.

Mind you, this is a starting point. There will be MANY more questions before I'm done with this assignment.

The function is to look like this:

def root_find(f, xLo, xHi, eps)

So, if I test the above so that f = 5x**8 - 3x**4 + x - 2. by saying y0 = f(xLo) I will get an error, right? How do I format the input so the function will use it? Am I even asking the right question? (I'm so lost!)


thank you for helping
I always start off by assuming the x == 1 in order to simplify things at the outset. This makes 5x == 5, etc. Then, after the function is working, you can add the x to the parameter list and do things like (5 * x)**8. Of course, if you are trying to solve for x then things are a lot more complicated, but if you can work it out on paper you should be able to forulate the appropriate algorithm.
Whether you use eval() or not (eval() is just another way of giving the interpeter text to work on) you basically have two things that go into the formula: variables which have names and values (ie a = 1) and literals (ie "hello") all of which must be assigned before they can be evaluated.
Jan 21 '07 #2
TMS
119 100+
thank you... as always you are a life saver!!! And you make it look so easy (never mind that it is).

TMS
Jan 21 '07 #3
bartonc
6,596 Expert 4TB
thank you... as always you are a life saver!!! And you make it look so easy (never mind that it is).

TMS
Any time... really!
Jan 21 '07 #4
TMS
119 100+
ok, wait.

If my function is (and I'm only trying to see that 5*x is handled properly)

Expand|Select|Wrap|Line Numbers
  1. def root_find(f, xLo, xHi, eps):
  2.     y1 = f(xLo)
  3. print y1
  4.  
  5.  
and I test it with:

s = root_find(5*x, -1, 1, .0001)
print s
Nothing happens. It doesn't print, it doesn't do anything. Is there something else I must do to get it to process the function that is entered for f? Again, thank you for your patience with me.

TMS
Jan 21 '07 #5
bartonc
6,596 Expert 4TB
ok, wait.

If my function is (and I'm only trying to see that 5*x is handled properly)

Expand|Select|Wrap|Line Numbers
  1. def root_find(f, xLo, xHi, eps):
  2.     y1 = f(xLo)
  3. print y1
  4.  
  5.  
and I test it with:

s = root_find(5*x, -1, 1, .0001)
print s
Nothing happens. It doesn't print, it doesn't do anything. Is there something else I must do to get it to process the function that is entered for f? Again, thank you for your patience with me.

TMS
That's funny. You should have gotten two errors. First:

>>> def root_find(f, xLo, xHi, eps):
... y1 = f(xLo)
... print y1
...
>>> s = root_find(5*x, -1, 1, .0001)
>>> s = root_find(5*x, -1, 1, .0001)
File "<console>", line 1, in ?
''' exceptions.NameError : name 'x' is not defined '''

because x has not yet been assigned a value.
>>> x = 1
Fixed error #1. Then:

>>> s = root_find(5*x, -1, 1, .0001)
File "<console>", line 1, in ?
File "<console>", line 3, in root_find
''' exceptions.TypeError : 'int' object is not callable '''
>>>

Because
... y1 = f(xLo)
syntax says "call f with xLo as the argument".
you want f**xLo, right?

also you missed one indent and the return:

Expand|Select|Wrap|Line Numbers
  1. def root_find(f, xLo, xHi, eps):
  2.     y1 = f**xLo
  3.     return y1
  4.  
If you don't put the return in there, the function will return None.

>>> s = root_find(5*x, -1, 1, .0001)
>>> print s
0.2
>>>

By the way, are using IDLE to edit and run this?
Jan 21 '07 #6
TMS
119 100+
yes. I am just running the module.

What I'm trying to do is start with 5*x and then go to 5*x**3. So, xLo should be x for one iteration, then when that works, xHi would be next. In otherwords, I'm starting really small to get the find_root algorithm going.

When I say f(xLo) I am wanting it to map xLo into x, so if f = 5x and xLo = -1, I should get a -5 back. It is my understanding of the bisection method that you solve using xLo in the equation, then try it using xHi. Then you divide the two answers by 2 to get the mid. That allows you to get closer to epsilon. Am I way off base?

tms
Jan 21 '07 #7
bartonc
6,596 Expert 4TB
yes. I am just running the module.

What I'm trying to do is start with 5*x and then go to 5*x**3. So, xLo should be x for one iteration, then when that works, xHi would be next. In otherwords, I'm starting really small to get the find_root algorithm going.

When I say f(xLo) I am wanting it to map xLo into x, so if f = 5x and xLo = -1, I should get a -5 back. It is my understanding of the bisection method that you solve using xLo in the equation, then try it using xHi. Then you divide the two answers by 2 to get the mid. That allows you to get closer to epsilon. Am I way off base?

tms
No you're not off base. I had read "5x^8 - 3x^4 + x - 2" not the algorithm when I wrote that. Psuedocode translates very nicely into python. In fact, I write in python now where I used to use psuedocode. Although the parentheses are a bit confusing, in the example,
start loop
Do While (xR - xL) > epsilon

'calculate midpoint of domain
xM = (xR + xL) / 2

'Find f(xM)
If ((f(xL) * f(xM)) > 0 Then
'throw away left half
xL = xM
else
'throw away right half
xR = xM
End If
Loop
the python would look something like this:
Expand|Select|Wrap|Line Numbers
  1. def f(arg):
  2.     arg = arg * 1   # re-assignment is perfectly fine.
  3.     return arg   # do any math you want to here
  4.  
  5. def FindRoot(f, x, L, R, e):
  6.     xL = x * L
  7.     xR = x * R
  8.     while (xR  - xL) > e:
  9.         xM = (xR  + xL) / 2.0   # python is a bit funny aboud dividing ints vs floats
  10.         if (f(xL) * f(xM) > 0:
  11.             xL = xM
  12.         else:
  13.             xR = xM
  14.  
  15. print FindRoot(f, 5, -1, 1, .0001)
Jan 21 '07 #8
bvdet
2,851 Expert Mod 2GB
A recent thread http://www.thescripts.com/forum/thread583573.html may provide a partial solution to you. I wrote a simple script that incorporates some of the functions and classes from it:
Expand|Select|Wrap|Line Numbers
  1. # 5x^3-3x^2+x-2
  2. d = Doublet(Monome(5,3,0),Doublet(Monome(-3,2,0),Doublet(Monome(1,1,0),Doublet(Monome(-2,0,0), None))))
  3. print d
  4.  
  5. def solve(d, vx, vy = 1.0):
  6.     r = 0.0
  7.     for m in iterDoub(d):
  8.         r += m.coeff*float(vx)**m.expoX*float(vy)**m.expoY
  9.     return r        
  10.  
  11. def bisection(d, U, L, e):
  12.     if solve(d, U) > 0 and solve(d, L) < 0:
  13.         while abs(U - L) > e:
  14.             M = (U+L)/2
  15.             if solve(d, M)*solve(d,U) > 0:
  16.                 U = M
  17.             else:
  18.                 L = M
  19.             print U, L
  20.         return U,L
  21.     else:
  22.         return "Invalid arguments. f(U) must be > 0 and f(L) must be < 0."
  23.  
  24. print bisection(d, 1.0, -1.0, 0.00001)
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> 5(x^3)-3(x^2)+x-2
  2. 1.0 0.0
  3. 1.0 0.5
  4. 1.0 0.75
  5. 1.0 0.875
  6. 0.9375 0.875
  7. 0.90625 0.875
  8. 0.890625 0.875
  9. 0.890625 0.8828125
  10. 0.88671875 0.8828125
  11. 0.88671875 0.884765625
  12. 0.8857421875 0.884765625
  13. 0.88525390625 0.884765625
  14. 0.885009765625 0.884765625
  15. 0.884887695313 0.884765625
  16. 0.884887695313 0.884826660156
  17. 0.884857177734 0.884826660156
  18. 0.884857177734 0.884841918945
  19. 0.884857177734 0.88484954834
  20. (0.884857177734375, 0.88484954833984375)
Jan 21 '07 #9
TMS
119 100+
Ok, but I'm still confused about how Python handles the 5x**8. The x is not defined, so there is an error. You defined f with a function, but later you added a variable to FindRoot(f, x, L, R, e). My function is assigned by the teacher and is as follows:
find_root(f, xLo, xHi, eps). I can only have 4 variables. Does your def f(arg) attempt to solve that problem?


Expand|Select|Wrap|Line Numbers
  1. def f(arg):
  2.     arg = arg * 1   # re-assignment is perfectly fine.
  3.     return arg   # do any math you want to here
  4.  
  5. def FindRoot(f, x, L, R, e):
  6.     xL = x * L
  7.     xR = x * R
  8.     while (xR  - xL) > e:
  9.         xM = (xR  + xL) / 2.0   # python is a bit funny aboud dividing ints vs floats
  10.         if (f(xL) * f(xM) > 0:
  11.             xL = xM
  12.         else:
  13.             xR = xM
  14.  
  15. print FindRoot(f, 5, -1, 1, .0001)
Jan 21 '07 #10
TMS
119 100+
A recent thread http://www.thescripts.com/forum/thread583573.html may provide a partial solution to you.
I looked over that link. There is a lot of information, but I understand very little of it. For example, the Doublet(Monome...) part. Doublet is part of what module? It isn't defined so I get an error message.

Again, I'm starting at the very beginning of this assignment, and not necessarily trying to write the functions complete yet (its due on Wednesday!!!). I want to understand this:
If my teacher runs my module and find_root is defined like this:

find_root(f, xLo, xHi, eps):

and the teacher enters this:

find_root(5x**3 + 2x**2-x+1, -3, 4, .0001)

how do I help Python understand that x is defined by xLo and then xHi. In order to solve for the root, if I understand bisection correctly, I use xLo to find y0, and xHi to find y1 by using it like this:

y0 = 5(xLo)**3 + 2(xLo)**2 - (xLo) + 1
y1 = 5(xHi)**3 + 2(xHi)**2 - (xHi) + 1

If it is still higher than eps, then I divide by 2... etc. But in order to begin I have to map xLo into x. And in order to do that, I have to get Python to know what to do with an x that is undefined.

Perhaps I am not reducing my question far enough? I do appreciate all the help, and I definitely appreciate the help on the functions. I just can't get there until I understand how to communicate with Python what to do with a variable that will be defined through the process.

Thank you, thank you... for your patience.
Jan 21 '07 #11
bartonc
6,596 Expert 4TB
Ok, but I'm still confused about how Python handles the 5x**8. The x is not defined, so there is an error. You defined f with a function, but later you added a variable to FindRoot(f, x, L, R, e). My function is assigned by the teacher and is as follows:
find_root(f, xLo, xHi, eps). I can only have 4 variables. Does your def f(arg) attempt to solve that problem?


Expand|Select|Wrap|Line Numbers
  1. def f(arg):
  2.     arg = arg * 1   # re-assignment is perfectly fine.
  3.     return arg   # do any math you want to here
  4.  
  5. def FindRoot(f, x, L, R, e):
  6.     xL = x * L
  7.     xR = x * R
  8.     while (xR  - xL) > e:
  9.         xM = (xR  + xL) / 2.0   # python is a bit funny aboud dividing ints vs floats
  10.         if (f(xL) * f(xM) > 0:
  11.             xL = xM
  12.         else:
  13.             xR = xM
  14.  
  15. print FindRoot(f, 5, -1, 1, .0001)
What I've done here is give an example of how this MIGHT be done. Hence "someting like this". Please make an attempt at writing the function according to your assignment and I'll help you debug it. I believe that you will learn much more this way rather than just being given the answer.
Jan 21 '07 #12
TMS
119 100+
What I've done here is give an example of how this MIGHT be done. Hence "someting like this". Please make an attempt at writing the function according to your assignment and I'll help you debug it. I believe that you will learn much more this way rather than just being given the answer.
I don't want you to write the function (arggggg) I've been working on this since Wednesday night. I can't find a way for python to interpret the x when I put 5x as the value of f. That is all I'm looking for. I can get through the pseudocode... its this one thing I'm hung up on. I've been reading, searching... I don't understand how to tell Python that I will eventually define x when it is in the format of 5x**8. Thats all I've been asking all along. If I sound exasperated its because I am. Believe me, I don't go to the boards unless I'm really stuck. I even emailed my teacher (before I tried the board) and he hasn't responded.

I must be asking the question wrong.
Jan 21 '07 #13
bartonc
6,596 Expert 4TB
I don't want you to write the function (arggggg) I've been working on this since Wednesday night. I can't find a way for python to interpret the x when I put 5x as the value of f. That is all I'm looking for. I can get through the pseudocode... its this one thing I'm hung up on. I've been reading, searching... I don't understand how to tell Python that I will eventually define x when it is in the format of 5x**8. Thats all I've been asking all along. If I sound exasperated its because I am. Believe me, I don't go to the boards unless I'm really stuck. I even emailed my teacher (before I tried the board) and he hasn't responded.

I must be asking the question wrong.
how about
Expand|Select|Wrap|Line Numbers
  1. fiveX = 5 * x
?
Jan 21 '07 #14
TMS
119 100+
how about
Expand|Select|Wrap|Line Numbers
  1. fiveX = 5 * x
?
no, because x is undefined.

My teacher will be testing my assignment. He will use something like this:

find_root(5x**3+2x**2+x+1, -3, 4, .0006)

I have to find a way to process that statement, where f is defined as 5x**3+2x**2+x+1, but will (through the function) define x with xLo and then later with xHi. I get an error saying that x is not defined. It won't let me pass this right now because I'm missing something, obviously :(

It is probably so obvious to python programmers who have been programming in python for a while, but I'm not getting it, which is why I'm being so persistent. I really want to understand what I'm missing.
Jan 21 '07 #15
TMS
119 100+
ok, I'm really not trying to be rude. I am hoping you understand that I can't change the way the function find_root(f, xLo, xHi,eps) is because its the assignment. It the way the teacher told us that we have to do it. Then, he said, try it with... 5x**8 + 3x**4 -2 or some such equation. The requirements specify that I have to define f with the above 'type' of equation. I am limited in this respect. All I want help with is how to tell python what I mean when an integer and a variable are entered together.

Is that more clear?
Jan 21 '07 #16
bartonc
6,596 Expert 4TB
no, because x is undefined.

My teacher will be testing my assignment. He will use something like this:

find_root(5x**3+2x**2+x+1, -3, 4, .0006)

I have to find a way to process that statement, where f is defined as 5x**3+2x**2+x+1, but will (through the function) define x with xLo and then later with xHi. I get an error saying that x is not defined. It won't let me pass this right now because I'm missing something, obviously :(

It is probably so obvious to python programmers who have been programming in python for a while, but I'm not getting it, which is why I'm being so persistent. I really want to understand what I'm missing.
There are two ways that I see for the call
find_root(5x**3+2x**2+x+1, -3, 4, .0006)
to be made (this will never be interpreted directly by python). The first is that have a long argument list with a variable for each factor, which violates the "4 arguments only" rule. The second would be to use text processing on a literal first argument as in
find_root("5x**3+2x**2+x+1", -3, 4, .0006)
but that seems like a very complex asignment for a beginning coarse.
How 'bout you put the text of the asignment here?
Jan 21 '07 #17
bvdet
2,851 Expert Mod 2GB
I hope these snippets will help
Expand|Select|Wrap|Line Numbers
  1. >>> s = '5*(x**3)-3*(x**2)+x-2'
  2. >>> x = 4
  3. >>> eval(s)
  4. 274
Expand|Select|Wrap|Line Numbers
  1. >>> def solve2(s, x, y=1.0):
  2. ...     return eval(s)
  3. ... 
  4. >>> s = '5*(x**3)-3*(x**2)+x-2'
  5. >>> solve2(s, 4.0)
  6. 274.0
  7. >>> 
Jan 21 '07 #18
bartonc
6,596 Expert 4TB
I hope these snippets will help
Expand|Select|Wrap|Line Numbers
  1. >>> s = '5*(x**3)-3*(x**2)+x-2'
  2. >>> x = 4
  3. >>> eval(s)
  4. 274
Expand|Select|Wrap|Line Numbers
  1. >>> def solve2(s, x, y=1.0):
  2. ...     return eval(s)
  3. ... 
  4. >>> s = '5*(x**3)-3*(x**2)+x-2'
  5. >>> solve2(s, 4.0)
  6. 274.0
  7. >>> 
Nice one BV! Although I'm not sure the paren. grouping is right for the problem.
Jan 21 '07 #19
TMS
119 100+
I hope these snippets will help
Expand|Select|Wrap|Line Numbers
  1. >>> s = '5*(x**3)-3*(x**2)+x-2'
  2. >>> x = 4
  3. >>> eval(s)
  4. 274
Expand|Select|Wrap|Line Numbers
  1. >>> def solve2(s, x, y=1.0):
  2. ...     return eval(s)
  3. ... 
  4. >>> s = '5*(x**3)-3*(x**2)+x-2'
  5. >>> solve2(s, 4.0)
  6. 274.0
  7. >>> 
awesome. by George (or whomever you are) I think you've got it! So, now if the teacher won't be formatting the input that way, with the quote, I could simply do this:

Expand|Select|Wrap|Line Numbers
  1. f = 'f'
  2.  
  3. def find_root(f, xLo, xHi, eps):
  4.     return eval(f)
  5.  
  6. s = find_root('5*(x**3)-3*(x**2)+x-2', -3, 1, .001)
  7. print s
  8.  
which gives me -11 when I run the module. And I don't think thats right. I think it should be -113 because 5*(-3**3) - 3*(-3**2) -3 -2 evaluates to -113. So, is having the 2 variables confusing it? How does eval work?

thank you very much. This is going to help so much.

tms
Jan 21 '07 #20
TMS
119 100+
Ok, nevermind. I think I figured it out.

If I do this:

Expand|Select|Wrap|Line Numbers
  1. f = 'f'
  2.  
  3. def find_root(f, xLo, xHi, eps):
  4.     x = xHi
  5.     return eval(f)
  6.  
  7. s = find_root('5*(x**3)', -3, 1, .001)
  8. print s
  9.  
  10.  
I can control which variable is being evaluated. I think I can take it from here. Thank you so much, everyone for your patience.

TMS
Jan 21 '07 #21
bartonc
6,596 Expert 4TB
How does eval work?

thank you very much. This is going to help so much.

tms
In section 2.3.4 of the (version 2.4.4) docs it says:
All numeric types (except complex) support the following operations, sorted by ascending priority (operations in the same box have the same priority; all numeric operations have a higher priority than comparison operations):


Operation Result Notes
________________________
x + y sum of x and y
________________________

x - y difference of x and y
________________________

x * y product of x and y
________________________

x / y quotient of x and y (1)
________________________

x // y (floored) quotient of x and y (5)
________________________

x % y remainder of x / y (4)
________________________

-x x negated
________________________

+x x unchanged
________________________

abs(x) absolute value or magnitude of x
________________________

int(x) x converted to integer (2)
________________________

long(x) x converted to long integer (2)
________________________

float(x) x converted to floating point
________________________

complex(re,im) a complex number with real part re, imaginary part im. im defaults to zero.
________________________

c.conjugate() conjugate of the complex number c
________________________

divmod(x, y) the pair (x // y, x % y) (3)(4)
________________________

pow(x, y) x to the power y
________________________

x ** y x to the power y

Notes:

(1)
For (plain or long) integer division, the result is an integer. The result is always rounded towards minus infinity: 1/2 is 0, (-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result is a long integer if either operand is a long integer, regardless of the numeric value.

(2)
Conversion from floating point to (long or plain) integer may round or truncate as in C; see functions floor() and ceil() in the math module for well-defined conversions.

(3)
See section 2.1, ``Built-in Functions,'' for a full description.

(4)
Complex floor division operator, modulo operator, and divmod().

Deprecated since release 2.3. Instead convert to float using abs() if appropriate.


(5)
Also referred to as integer division. The resultant value is a whole integer, though the result's type is not necessarily int.

You can change the order of evaluation with the parentheses as in:
(5*x)**3 multiplies 5 times x first then raises to the power of 3,
5*(x**3) raises x to the power of 3 then multiplies by 5
Jan 21 '07 #22
bvdet
2,851 Expert Mod 2GB
I looked over that link. There is a lot of information, but I understand very little of it. For example, the Doublet(Monome...) part. Doublet is part of what module? It isn't defined so I get an error message.

Again, I'm starting at the very beginning of this assignment, and not necessarily trying to write the functions complete yet (its due on Wednesday!!!). I want to understand this:
If my teacher runs my module and find_root is defined like this:

find_root(f, xLo, xHi, eps):

and the teacher enters this:

find_root(5x**3 + 2x**2-x+1, -3, 4, .0001)

how do I help Python understand that x is defined by xLo and then xHi. In order to solve for the root, if I understand bisection correctly, I use xLo to find y0, and xHi to find y1 by using it like this:

y0 = 5(xLo)**3 + 2(xLo)**2 - (xLo) + 1
y1 = 5(xHi)**3 + 2(xHi)**2 - (xHi) + 1

If it is still higher than eps, then I divide by 2... etc. But in order to begin I have to map xLo into x. And in order to do that, I have to get Python to know what to do with an x that is undefined.

Perhaps I am not reducing my question far enough? I do appreciate all the help, and I definitely appreciate the help on the functions. I just can't get there until I understand how to communicate with Python what to do with a variable that will be defined through the process.

Thank you, thank you... for your patience.
Yeah, there is a lot of stuff in that thread that is beyond the scope of your assignment. I was trying to give you an example of the calculation to determine the root. Monome, Doublet (the names are French!) and iterDoub are defined at various places in the thread. They are used to create an instance that stores all of the various factors and exponents in a multinomial equation and format the information for printing. It's actually kinda neat.

5x**3 + 2x**2-x+1

If the above statement is the required format for the argument, then you will need to modify the string so it can be 'eval'ed properly. I believe the argument must be entered as a string, i.e. enclosed in single or double quotes. Even if 'x' is a defined variable, the above equation cannot be evaluated as it is.

'5*x**3 + 2*x**2-x-1' can be evaluated if 'x' has been defined as in:
Expand|Select|Wrap|Line Numbers
  1. x = 2.5
If you can define the input format, this should work:
Expand|Select|Wrap|Line Numbers
  1. find_root('5*x**3+2*x**2-x+1', -3, 4, .0001)
  2.  
  3. def find_root(s, xL, xH, e):
  4.     ....do some stuff...
  5.     intermediate_results = eval(s)
  6.     ....do more stuff....
Jan 21 '07 #23
TMS
119 100+
5*x**3 + 2*x**2-x-1' can be evaluated if 'x' has been defined as in:
Expand|Select|Wrap|Line Numbers
  1. x = 2.5
If you can define the input format, this should work:
Expand|Select|Wrap|Line Numbers
  1. find_root('5*x**3+2*x**2-x+1', -3, 4, .0001)
  2.  
  3. def find_root(s, xL, xH, e):
  4.     ....do some stuff...
  5.     intermediate_results = eval(s)
  6.     ....do more stuff....
It does work. Thank you. By using eval and formatting the input and utilizing the pseudocode that wikipedia provided for the bisection method I was able to finish the first part of the homework assignment. It works quite well. I will check with my teacher to make sure its what he was looking for.
The second part requires me to "use the rootfinder module to implement a script findroot that concatenates all of its command line arguments (except, of course the program name) into an expression for f() so that you have a rootsolver that works fromt the command line."

????

Well, I'm calling it a day for the first part. I have some AI homework to work on.

thank you again for your help, and everone else too! Especially for being patient with me while I figure out how to ask the question, lol.

TMS
Jan 22 '07 #24
bvdet
2,851 Expert Mod 2GB
You are welcome. I am glad to help, and I'm sure Barton is too. Keep with it!
Jan 22 '07 #25

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: ron | last post by:
Hi, I'm still new at Python and have been away from programming for a number of years in general. I apologized in advance if this has been discussed extensively already. Is the input()...
2
by: Paul A. Hoadley | last post by:
Hello, I am trying to convert a RELAX NG schema to DTD using Trang. I am currently trying to add some inline elements to the schema, such as <emph> for marking emphasised text. Here is an...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
8
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR...
7
by: Laurent Pointal | last post by:
on win32] Given the following: 45 .... (<generator object at 0x00A79788>,) .... File "<stdin>", line 1 SyntaxError: invalid syntax
8
by: dmoore | last post by:
Hi folks, I've seen the following issue come up in multiple posts to this mailing list: I have a python program that spawns a child process with popen or popen2 or popen3 or popen2.popen2...
0
by: Terry Reedy | last post by:
rishi pathak wrote: Does not work on Windows, at least with 3.0, as tty fails trying to import termios. There, use msvcrt module "msvcrt.kbhit() Return true if a keypress is waiting to be...
7
by: Lie Ryan | last post by:
>>I want to write something that handle every char immediately after its Don't you think that getting a one-character from console is something that many people do very often? Do you think that...
4
by: =?GB2312?B?0rvK18qr?= | last post by:
Hi all, Today I was writing a simple test app for a video decoder library. I use python to parse video files and input data to the library. I got a problem here, I need a windows form, and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.