Hello everyone, my latest project is this:
1) Ask user to input a function
2) Plot said function
Well, I figured out how to strip the function into two parts, input a number, and evaluate the function. That all works fine outside of a defined function I created. The trouble I have, is now when asking for user input, it screws it up. I can't really explain it, but it looks like the results are just repeats of a few numbers. Here's my code:

``````from numpy import *
from pylab import *

numpyfunctions = [
'abs',
'absolute',
'angle',
'sqrt',
'log',
'sin',
'sinh',
'cos',
'cosh',
'tan',
'tanh']

def frange(start, end=None, inc=None):
"A range function, that does accept float increments..."

if end == None:
end = start + 0.0
start = 0.0

if inc == None:
inc = 1.0

L = []
while 1:
next = start + len(L) * inc
if inc > 0 and next >= end:
break
elif inc < 0 and next <= end:
break
L.append(next)

return L

def funeval(function, numbers=range(-5, 5), splitat='x'):
'''Takes the function (string) and runs through a list of numbers,
then return a list of numbers that equals the function'''
# List of the function's results
result = []
for number in numbers:
# Usually not a good idea to divide anything by zero :D
if number != 0:
# Split the function input
# Usually at 'x'
# Then, we put together the number
# inside the function in replace
# of the 'x'
b = function.split(splitat)
c = ''
for y in b:
c += y
if y != b[-1]:
c+= '%d' %(number)
result.append(eval(c))
if number == 0:
result.append(0)
return result

print("Function Plotter v1.0")
print("~Hondros")
print("Type in a function, or type 'help' for function commands")
print("Type frange(x, y) for a specific x range)")
print("example: 2*sin(x)")
print("")
#ran = frange(-5, 5, 0.01)
while True:
try:
f = raw_input("y = ")
if f == 'help':
if f[0:6] == 'frange':
ran = eval(f)
else:
y = funeval(f, ran)
#plot(ran, y)
#show()
except:
print("The function %s is invalid" %(f))

Any thoughts? If needed, please look at attached .txt file. That's the output I have when I first tried creating this.

Edited by hondros: n/a

Attachments
``````Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32

****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface.  This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************

IDLE 2.6.4
>>> a = '5*34'
>>> b = eval(a)
>>> b
170
>>> from numpy import *
>>> sin
<ufunc 'sin'>
>>> sin()

Traceback (most recent call last):
File "<pyshell#44>", line 1, in <module>
sin()
ValueError: invalid number of arguments
>>> sin(x)

Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
sin(x)
NameError: name 'x' is not defined
>>> sin(b)
0.3466494554970303
>>> pi
3.1415926535897931
>>> a = 'sin(4)
SyntaxError: EOL while scanning string literal
>>> a = 'sin(4)'
>>> eval(a)
-0.7568024953079282
>>> a = 'sin(x)'
>>> a.split('x')
['sin(', ')']
>>> a
'sin(x)'
>>> x = 56
>>> for x in range(-5, 5):
if x != 0:
b = a.split('x')
c = ''
for y in b:
c += y
if y != b[-1]:
c+= x

Traceback (most recent call last):
File "<pyshell#64>", line 8, in <module>
c+= x
TypeError: cannot concatenate 'str' and 'int' objects
>>> for x in range(-5, 5):
if x != 0:
b = a.split('x')
c = ''
for y in b:
c += y
if y != b[-1]:
c+= '%d' %(x)

>>> c
'sin(4)'
>>> for x in range(-5, 5):
if x != 0:
b = a.split('x')
c = ''
for y in b:
c += y
if y != b[-1]:
c+= '%d' %(x)
print c

sin(-5)
sin(-4)
sin(-3)
sin(-2)
sin(-1)
sin(-1)
sin(1)
sin(2)
sin(3)
sin(4)
>>> eval(c)
-0.7568024953079282
>>> for x in range(-5, 5):
if x != 0:
b = a.split('x')
c = ''
for y in b:
c += y
if y != b[-1]:
c+= '%d' %(x)
print c, " = ", eval(c)

sin(-5)  =  0.958924274663
sin(-4)  =  0.756802495308
sin(-3)  =  -0.14112000806
sin(-2)  =  -0.909297426826
sin(-1)  =  -0.841470984808
sin(-1)  =  -0.841470984808
sin(1)  =  0.841470984808
sin(2)  =  0.909297426826
sin(3)  =  0.14112000806
sin(4)  =  -0.756802495308
>>> a = 'cos(x)'
>>> for x in range(-5, 5):
if x != 0:
b = a.split('x')
c = ''
for y in b:
c += y
if y != b[-1]:
c+= '%d' %(x)
print c, " = ", eval(c)

cos(-5)  =  0.283662185463
cos(-4)  =  -0.653643620864
cos(-3)  =  -0.9899924966
cos(-2)  =  -0.416146836547
cos(-1)  =  0.540302305868
cos(-1)  =  0.540302305868
cos(1)  =  0.540302305868
cos(2)  =  -0.416146836547
cos(3)  =  -0.9899924966
cos(4)  =  -0.653643620864
>>> a = 'sin(cos(x))*x**2'
>>> for x in range(-5, 5):
if x != 0:
b = a.split('x')
c = ''
for y in b:
c += y
if y != b[-1]:
c+= '%d' %(x)
print c, " = ", eval(c)

sin(cos(-5))*-5**2  =  -6.99683376921
sin(cos(-4))*-4**2  =  9.72932815425
sin(cos(-3))*-3**2  =  7.52419675384
sin(cos(-2))*-2**2  =  1.61695661541
sin(cos(-1))*-1**2  =  -0.514395258524
sin(cos(-1))*-1**2  =  -0.514395258524
sin(cos(1))*1**2  =  0.514395258524
sin(cos(2))*2**2  =  -1.61695661541
sin(cos(3))*3**2  =  -7.52419675384
sin(cos(4))*4**2  =  -9.72932815425
>>> def funeval(a, ran=range(-5, 5):

SyntaxError: invalid syntax
>>> def funeval(a, ran=range(-5, 5)):
for x in ran:
if x != 0:
b = a.split('x')
c = ''
for y in b:
c += y
if y != b[-1]:
c+= '%d' %(x)
print c, " = ", eval(c)

>>> funeval('sin(x)*cos(x)')
sin(-5)*cos(-5)  =  0.272010555445
sin(-4)*cos(-4)  =  -0.494679123312
sin(-3)*cos(-3)  =  0.139707749099
sin(-2)*cos(-2)  =  0.378401247654
sin(-1)*cos(-1)  =  -0.454648713413
sin(-1)*cos(-1)  =  -0.454648713413
sin(1)*cos(1)  =  0.454648713413
sin(2)*cos(2)  =  -0.378401247654
sin(3)*cos(3)  =  -0.139707749099
sin(4)*cos(4)  =  0.494679123312
>>> def funeval(a, ran=range(-5, 5)):
z = []
for x in ran:
if x != 0:
b = a.split('x')
c = ''
for y in b:
c += y
if y != b[-1]:
c+= '%d' %(x)
z += eval(c)
return z

>>> funeval('sin(x)'
)
array([], dtype=float64)
>>> a = funeval('sin(x)')
>>> a
array([], dtype=float64)
>>> def funeval(a, ran=range(-5, 5)):
z = []
for x in ran:
if x != 0:
b = a.split('x')
c = ''
for y in b:
c += y
if y != b[-1]:
c+= '%d' %(x)
d = eval(c)
z += '%d' %(d)
return z

>>> funeval('sin(x)')
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
>>> def funeval(a, ran=range(-5, 5)):
z = []
for x in ran:
if x != 0:
b = a.split('x')
c = ''
for y in b:
c += y
if y != b[-1]:
c+= '%d' %(x)
d = eval(c)
print d, c
z += '%d' %(d)
return z

>>> funeval('sin(x)')
0.958924274663 sin(-5)
0.756802495308 sin(-4)
-0.14112000806 sin(-3)
-0.909297426826 sin(-2)
-0.841470984808 sin(-1)
-0.841470984808 sin(-1)
0.841470984808 sin(1)
0.909297426826 sin(2)
0.14112000806 sin(3)
-0.756802495308 sin(4)
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
>>> def funeval(a, ran=range(-5, 5)):
z = []
for x in ran:
if x != 0:
b = a.split('x')
c = ''
for y in b:
c += y
if y != b[-1]:
c+= '%d' %(x)
d = eval(c)
z += '%f' %(d)
return z

>>> funeval('sin(x)')
['0', '.', '9', '5', '8', '9', '2', '4', '0', '.', '7', '5', '6', '8', '0', '2', '-', '0', '.', '1', '4', '1', '1', '2', '0', '-', '0', '.', '9', '0', '9', '2', '9', '7', '-', '0', '.', '8', '4', '1', '4', '7', '1', '-', '0', '.', '8', '4', '1', '4', '7', '1', '0', '.', '8', '4', '1', '4', '7', '1', '0', '.', '9', '0', '9', '2', '9', '7', '0', '.', '1', '4', '1', '1', '2', '0', '-', '0', '.', '7', '5', '6', '8', '0', '2']
>>> def funeval(a, ran=range(-5, 5)):
z = []
for x in ran:
if x != 0:
b = a.split('x')
c = ''
for y in b:
c += y
if y != b[-1]:
c+= '%d' %(x)
d = eval(c)
z += d
return z

>>> funeval('sin(x)')
array([], dtype=float64)
>>> def funeval(a, ran=range(-5, 5)):
z = []
for x in ran:
if x != 0:
b = a.split('x')
c = ''
for y in b:
c += y
if y != b[-1]:
c+= '%d' %(x)
d = eval(c)
z += '%f' %(d)
print c, d, z
return z

>>> funeval('sin(x)')
sin(-5) 0.958924274663 ['0', '.', '9', '5', '8', '9', '2', '4']
sin(-4) 0.756802495308 ['0', '.', '9', '5', '8', '9', '2', '4', '0', '.', '7', '5', '6', '8', '0', '2']
sin(-3) -0.14112000806 ['0', '.', '9', '5', '8', '9', '2', '4', '0', '.', '7', '5', '6', '8', '0', '2', '-', '0', '.', '1', '4', '1', '1', '2', '0']
sin(-2) -0.909297426826 ['0', '.', '9', '5', '8', '9', '2', '4', '0', '.', '7', '5', '6', '8', '0', '2', '-', '0', '.', '1', '4', '1', '1', '2', '0', '-', '0', '.', '9', '0', '9', '2', '9', '7']
sin(-1) -0.841470984808 ['0', '.', '9', '5', '8', '9', '2', '4', '0', '.', '7', '5', '6', '8', '0', '2', '-', '0', '.', '1', '4', '1', '1', '2', '0', '-', '0', '.', '9', '0', '9', '2', '9', '7', '-', '0', '.', '8', '4', '1', '4', '7', '1']
sin(-1) -0.841470984808 ['0', '.', '9', '5', '8', '9', '2', '4', '0', '.', '7', '5', '6', '8', '0', '2', '-', '0', '.', '1', '4', '1', '1', '2', '0', '-', '0', '.', '9', '0', '9', '2', '9', '7', '-', '0', '.', '8', '4', '1', '4', '7', '1', '-', '0', '.', '8', '4', '1', '4', '7', '1']
sin(1) 0.841470984808 ['0', '.', '9', '5', '8', '9', '2', '4', '0', '.', '7', '5', '6', '8', '0', '2', '-', '0', '.', '1', '4', '1', '1', '2', '0', '-', '0', '.', '9', '0', '9', '2', '9', '7', '-', '0', '.', '8', '4', '1', '4', '7', '1', '-', '0', '.', '8', '4', '1', '4', '7', '1', '0', '.', '8', '4', '1', '4', '7', '1']
sin(2) 0.909297426826 ['0', '.', '9', '5', '8', '9', '2', '4', '0', '.', '7', '5', '6', '8', '0', '2', '-', '0', '.', '1', '4', '1', '1', '2', '0', '-', '0', '.', '9', '0', '9', '2', '9', '7', '-', '0', '.', '8', '4', '1', '4', '7', '1', '-', '0', '.', '8', '4', '1', '4', '7', '1', '0', '.', '8', '4', '1', '4', '7', '1', '0', '.', '9', '0', '9', '2', '9', '7']
sin(3) 0.14112000806 ['0', '.', '9', '5', '8', '9', '2', '4', '0', '.', '7', '5', '6', '8', '0', '2', '-', '0', '.', '1', '4', '1', '1', '2', '0', '-', '0', '.', '9', '0', '9', '2', '9', '7', '-', '0', '.', '8', '4', '1', '4', '7', '1', '-', '0', '.', '8', '4', '1', '4', '7', '1', '0', '.', '8', '4', '1', '4', '7', '1', '0', '.', '9', '0', '9', '2', '9', '7', '0', '.', '1', '4', '1', '1', '2', '0']
sin(4) -0.756802495308 ['0', '.', '9', '5', '8', '9', '2', '4', '0', '.', '7', '5', '6', '8', '0', '2', '-', '0', '.', '1', '4', '1', '1', '2', '0', '-', '0', '.', '9', '0', '9', '2', '9', '7', '-', '0', '.', '8', '4', '1', '4', '7', '1', '-', '0', '.', '8', '4', '1', '4', '7', '1', '0', '.', '8', '4', '1', '4', '7', '1', '0', '.', '9', '0', '9', '2', '9', '7', '0', '.', '1', '4', '1', '1', '2', '0', '-', '0', '.', '7', '5', '6', '8', '0', '2']
['0', '.', '9', '5', '8', '9', '2', '4', '0', '.', '7', '5', '6', '8', '0', '2', '-', '0', '.', '1', '4', '1', '1', '2', '0', '-', '0', '.', '9', '0', '9', '2', '9', '7', '-', '0', '.', '8', '4', '1', '4', '7', '1', '-', '0', '.', '8', '4', '1', '4', '7', '1', '0', '.', '8', '4', '1', '4', '7', '1', '0', '.', '9', '0', '9', '2', '9', '7', '0', '.', '1', '4', '1', '1', '2', '0', '-', '0', '.', '7', '5', '6', '8', '0', '2']
>>> def funeval(a, ran=range(-5, 5)):
z = []
for x in ran:
if x != 0:
b = a.split('x')
c = ''
for y in b:
c += y
if y != b[-1]:
c+= '%d' %(x)
d = eval(c)
z += float(d)
return z

>>> funeval('sin(x)')

Traceback (most recent call last):
File "<pyshell#109>", line 1, in <module>
funeval('sin(x)')
File "<pyshell#108>", line 12, in funeval
z += float(d)
TypeError: 'float' object is not iterable
>>> list(float(40))

Traceback (most recent call last):
File "<pyshell#110>", line 1, in <module>
list(float(40))
TypeError: 'float' object is not iterable
>>> a
array([], dtype=float64)
>>> c
'sin(cos(4))*4**2'
>>> b
['sin(cos(', '))*', '**2']
>>> c
'sin(cos(4))*4**2'
>>> eval(c)
-9.7293281``````
2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by hondros

Okay, I redid the code, and it's all working now. Don't know what the issue was, but this version works a LOT better. However, I have a new issue. This time, it won't do anything except the "else" clause. Any ideas?

``````from numpy import *
from pylab import *

def frange(start, end=None, inc=None):
"A range function, that does accept float increments..."

if end == None:
end = start + 0.0
start = 0.0

if inc == None:
inc = 1.0

L = []
while 1:
next = start + len(L) * inc
if inc > 0 and next >= end:
break
elif inc < 0 and next <= end:
break
L.append(next)

return L

def funceval(function, numlist=frange(-100, 100, 0.01), splitat='x'):
y = []
for x in numlist:
try:
c = '%s%s%s'%(function.split(splitat)[0], str(x), function.split(splitat)[1])
y.append(eval(c))
except:
numlist.pop(numlist.index(x))
return y

a = []
for x in ran:
return a

print("Function Plotter v2.0")
print("~Hondros")
print("Type in a function, or type 'help' for function commands")
print("Type frange(x, y) for a specific x range)")
print("Type show to show the plot window")
print("example: 2*sin(x)")
print("")
while True:
try:
ran = frange(-100, 100, 0.01)
f = raw_input("y = ")
if f == 'help':
if f[0:6] == 'frange':
ran = eval(f)
if f == 'show':
show()
else:
y = funceval(f, ran)
plot(ran, y)
except:
print("The function %s is invalid" %(f))

I don't have numpy, but try replacing all of the other 'ifs' with 'elif' and see if that works.

Okay, fixed it. Again, don't know what the issue is. Anyways, if anyone wants the finished code, here it is:
(note that numpy and pylab are required)

``````from numpy import *
from pylab import *

def frange(start, end=None, inc=None):
"A range function, that does accept float increments..."

if end == None:
end = start + 0.0
start = 0.0

if inc == None:
inc = 1.0

L = []
while 1:
next = start + len(L) * inc
if inc > 0 and next >= end:
break
elif inc < 0 and next <= end:
break
L.append(next)

return L

def funceval(function, numlist=frange(-100, 100, 0.01), splitat='x'):
y = []
for x in numlist:
try:
numfunction = ''
for c in function.split(splitat)[:-1]:
numfunction += c
numfunction += '(%f)' %(x)
numfunction += function.split(splitat)[-1]
y.append(eval(numfunction))
except:
numlist.pop(numlist.index(x))
return y

a = []
for x in ran:
return a

print("Function Plotter v2.0")
print("Help")
print("")
print("Functions can be in any form, so long as there is an 'x'")
print("Standard form for parabola's typing in the function is:")
print("A*(x**2)+B*x+C")
print("Carry this form to any function")
print("Make sure to type in every symbol, none are implied")
print("Pi is: pi")
print("Using sine or similar functions: sin(x)")
print("""Functions usable are:
'abs'
'absolute'
'angle'
'sqrt'
'log'
'sin'
'sinh'
'cos'
'cosh'
'tan'
'tanh'
""")
print("For further help, please look at the source code to figure it out,")
print("Or, e-mail me at: alexpolosky@gmail.com")

if once != True:
print("Function Plotter v2.0")
print("~Hondros")
print("Type in a function, or type 'help' for function commands")
print("Type frange(x, y) for a specific x range)")
print("Type show() to show the plot window")
print("example: 2*sin(x)")
print("")
while True:
try:
f = raw_input("y = ")
if f == 'help':
if f[0:6] == 'frange':