Python Forum

Full Version: Help coding graph
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello,
I'm a beginner and i must draw a graph

[Image: 4tsd.jpg]
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 ?
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)