#! /usr/bin/env Python ##################################################### # Version 0.3 By Gabe Fierro # # # # 7 September 2008 # ##################################################### #------Make sure you have Gnuplot and Gnuplot.py installed for this to work--------# from numpy import * import Gnuplot, Gnuplot.funcutils #get Dictionary to make into a string for Gnuplot def getFuncDict(): funcDict = {} global power power=[] global coef coef=[] a=raw_input("Highest power: ") global terms a=int(a)+1 terms=int(a)-1 terms='%i'% terms for q in range(a): print "Coefficient?" print "x^", + q x = raw_input(": ") power.append(q) coef.append(int(x)) return coef #makes a dictionary of string formats and combines userinput with those to create a graphable function def makeFuncString(coef,terms): global newfunc funcDict={'0':'%s*x**0', '1':'%s*x**0+%s*x**1', '2':'%s*x**0+%s*x**1+%s*x**2', '3':'%s*x**0+%s*x**1+%s*x**2+%s*x**3', '4':'%s*x**0+%s*x**1+%s*x**2+%s*x**3+%s*x**4', '5':'%s*x**0+%s*x**1+%s*x**2+%s*x**3+%s*x**4+%sx**5', '6':'%s*x**0+%s*x**1+%s*x**2+%s*x**3+%s*x**4+%sx**5+%s*x**6', '7':'%s*x**0+%s*x**1+%s*x**2+%s*x**3+%s*x**4+%sx**5+%sx**6+%sx**7'} coef=tuple(coef) newfunc = funcDict[terms] % coef newfunc=newfunc.replace("*x**0","") newfunc=newfunc.replace("*x**1","*x") #plots function def plotFunction(newfunc): g = Gnuplot.Gnuplot(debug=1) g.plot(Gnuplot.Func(newfunc)) #now, find the derivative of that function def findDeriv(coef,power,terms): ncoef=[] npower=[] del coef[0] del power[0] for i in coef: ncoef.append(int(i)) for i in power: npower.append(int(i)) coef2=[] for i in ncoef: coef2.append(i*npower[ncoef.index(i)]) print coef2 funcDict={'0':'%s*x**0', '1':'%s*x**0+%s*x**1', '2':'%s*x**0+%s*x**1+%s*x**2', '3':'%s*x**0+%s*x**1+%s*x**2+%s*x**3', '4':'%s*x**0+%s*x**1+%s*x**2+%s*x**3+%s*x**4', '5':'%s*x**0+%s*x**1+%s*x**2+%s*x**3+%s*x**4+%sx**5', '6':'%s*x**0+%s*x**1+%s*x**2+%s*x**3+%s*x**4+%sx**5+%sx**6', '7':'%s*x**0+%s*x**1+%s*x**2+%s*x**3+%s*x**4+%sx**5+%sx**6+%sx**7'} terms=int(terms)-1 terms='%i'%terms coef2=tuple(coef2) global deriv deriv=funcDict[terms] % coef2 deriv=deriv.replace("*x**0","") deriv=deriv.replace("*x**1","*x") #graph d=Gnuplot.Gnuplot(debug=1) d.plot(Gnuplot.Func(deriv)) deriv=deriv.replace('**','^') deriv=deriv.replace('*x','x') print "DERIVATIVE: " print deriv #function calls getFuncDict() makeFuncString(coef,terms) plotFunction(newfunc) findDeriv(coef,power,terms)