Ihsan Ahmed K
Ihsan Ahmed K

Reputation: 159

Plotting a graph but having range issues

Here is my code to draw a graph, but i am not understanding the error please help

import matplotlib.pyplot as plt
import math

l=np.linspace(0,2001,100)
c=3*(10**8)
h=6.626*(10**-34)
K=1.38*(10**-23)
E=[]
for i in range(100,2001):
    y=2*(math.pi)*h*(c**2)/((l[i]**5)*math.exp((h*c/l[i]*K*T)-1))
    E.append(y)

plt.plot(l,E)
plt.show

Here is the error:

y=2*(math.pi)*h*(c**2)/((l[i]**5)*math.exp((h*c/l[i]*K*T)-1))
IndexError: index 100 is out of bounds for axis 0 with size 100

Please help me understand

Upvotes: 1

Views: 135

Answers (1)

RandomGuy
RandomGuy

Reputation: 1207

That's because np.linspace(0,2001,100) produces an array ranging from 0 to 2001 but with only 100 points, thus l[100] is out of bounds.

On a completely different tone : you should definitely try vectorization, a powerful tool supported by numpy arrays that makes code easier to implement :

import numpy as np
import matplotlib.pyplot as plt

l = np.linspace(0,2001,100)
c = 3*(10**8)
h = 6.626*(10**-34)
K = 1.38*(10**-23)
y = 2*np.pi*h*(c**2) / ( (l**5)*np.exp((h*c/l*K*T)-1) )

plt.plot(l,y)
plt.show()

Upvotes: 4

Related Questions