This is my program to graph functions and it works great. There is only one problem.
while 1==1:
import numpy as np
import matplotlib.pyplot as plt
print("FUNCTION GRAPHER")
def graph(formula,domain):
x = np.array(domain)
y = eval(formula)
plt.plot(x, y)
plt.show()
def sin(x):
return np.sin(x)
def cos(x):
return np.cos(x)
def tan(x):
return np.tan(x)
def csc(x):
return 1/(np.sin(x))
def sec(x):
return 1/(np.cos(x))
def cot(x):
return 1/(np.tan(x))
formula=input("Function: y=")
domainmin=int(input("Min X Value: "))
domainmax=int(input("Max X Value: "))
graph(formula, range(domainmin,domainmax))
print("DONE!")
When I try a non-linear function, it is not "curvy":
FUNCTION GRAPHER
Function: y=sin(x**2)
Min X Value: 0
Max X Value: 32
I cannot post a pic because I don't have enough reputation yet... but its just really spiky, and the points are only plotted every 1 unit.
Rather than range(domainmin,domainmax), use numpy.linspace(domainmin,domainmax, n_pts) where n_pts is the number of points you want, e.g. 200 or some other large number since you want the plot to be less jagged. Documentation for numpy.linspace can be found here.
The function range can only handle integers; see documentation here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With