I have a really simple issue with my Python program -- which isn't finished at all. Right now it's doing everything I want, but not how I want.
There are three things I've been trying to change:
- all functions are being plotted using the same color, and I'd like the program to automatically switch to a new color when a new function is added to the plot (it will be more than 2, all on the same plot).
- f(x)'s range is 140. How can I decrease that? Maybe to 20/40.
- (most important) My code isn't very efficient.
f1andderivativeare not associated at all. I declare the function's model inf1, but I have to set up everything again inderivative. Every time I try to do otherwise I end up having some problem with themainfunction. I'll eventually add more features like integration and whatnot, and if I'm declaring everything from scratch everytime I want to do something withf1the program will kinda lose its purpose.
Should I use x = Symbol('x') inside f1?
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
x = Symbol('x')
def f1(a, b, c, d):
y = a*x**3 + b*x**2 + x*c + d
return y
###yprime = y.diff(x)
###return yprime
def derivative(a, b, c, d):
y = a*x**3 + b*x**2 + x*c + d
yprime = y.diff(x)
return yprime
def factorail(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
###colors = iter(cm.rainbow(np.linspace(0, 1, len(ys))))
###for y in ys:
###plt.scatter(x, y, color=next(colors))
def main():
###colors = itertools.cycle(["r", "b", "g"])
y = f1(0,1,2,1)
yp = derivative(0,1,2,1)
print(y)
plot(y, yp)
plot(yp)
plt.show()
if __name__ == '__main__':
main()


