Help coding graph - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Help coding graph (/thread-24047.html) |
Help coding graph - breme57 - Jan-28-2020 hello, I'm a beginner and i must draw a graph I try this but... import matplotlib.pyplot as plt import numpy as np from math import * def HM(m): i = 0 while (i <= m): X = np.linspace(0,10,1) Y= [(1/(2*i+1)**2)*np.cos((2*i)+1)*x for x in X] plt.plot(X,Y) plt.show() print (HM(10))can you help me please ? RE: Help coding graph - jefsummers - Jan-29-2020 You never increment i so your loop will go forever. Your calls to plt are outside the function, so they have no access to X or Y and will plot nothing You don't need to print HM. I don't think you used linspace in the manner intended I did not check your algebra. THis is closer. import matplotlib.pyplot as plt import numpy as np from math import * def HM(m): i = 0 while (i <= m): X = np.linspace(0,10,100) Y= [(1/(2*i+1)**2)*np.cos((2*i)+1)*x for x in X] i=i+1 plt.plot(X,Y) HM(10) |