Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help coding graph
#1
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 ?
Reply
#2
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)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020