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
def menu():
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':
menuhelp()
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))
menu()
Any thoughts? If needed, please look at attached .txt file. That's the output I have when I first tried creating this.