Reputation: 2346
I am trying to plot this data:
h = 1
m = 1
E1 = (((h**2)/(2*m)) * ((((1*np.pi)/2)+((1*np.pi)/2))**2))
E2 = (((h**2)/(2*m)) * ((((2*np.pi)/2)+((2*np.pi)/2))**2))
E3 = (((h**2)/(2*m)) * ((((3*np.pi)/2)+((3*np.pi)/2))**2))
E4 = (((h**2)/(2*m)) * ((((4*np.pi)/2)+((4*np.pi)/2))**2))
k1 = ((((1*np.pi)/2)+((1*np.pi)/2))**2)
k2 = ((((2*np.pi)/2)+((2*np.pi)/2))**2)
k3 = ((((3*np.pi)/2)+((3*np.pi)/2))**2)
k4 = ((((4*np.pi)/2)+((4*np.pi)/2))**2)
E = list[E1, E2, E3, E4]
k = list[k1, k2, k3, k4]
plt.scatter(k,E)
plt.show()
The list
function doesn't seem to work for this. I don't think it can get the pre-defined values. Using np.array
also doesn't seem to work.
Upvotes: 0
Views: 72
Reputation: 46530
Not an answer, just a suggestion but I wanted to write out enough code that wouldn't fit in a comment.
Instead of writing out four nearly identical lines, you can define a function:
def E(n, h=1.0, m=1.0):
return (((h**2)/(2*m)) * ((((n*np.pi)/2)+((n*np.pi)/2))**2))
Note that it accepts values for h
and m
as arguments, but if none are provided, it will use 1.0
as default for both. This can even be simplified further, just by cleaning up the notation a bit (e.g., you have something/2 + something/2
, which is equivalent to something
, see my comment on your question)
def E(n, h=1.0, m=1.0):
return (.5*h**2/m) * (n*np.pi)**2
And similarly for k
:
def k(n):
return (n*np.pi)**2
The best thing about this, is that you can now do this all at once without manually building that list:
>>> ns = np.arange(1,5) # this is np.array([1,2,3,4])
>>> E(ns)
array([ 4.9348022 , 19.7392088 , 44.4132198 , 78.95683521])
>>> k(ns)
array([ 9.8696044 , 39.4784176 , 88.82643961, 157.91367042])
Obviously this precise code won't work for you given the parenthesis typo, but I hope it helps with using numpy! This is the entire code at once:
import numpy as np
import matplotlib.pyplot as plt
def E(n, h=1.0, m=1.0):
return (.5*h**2/m) * (n*np.pi)**2
def k(n):
return (n*np.pi)**2
ns = np.arange(1, 5)
plt.scatter(k(ns), E(ns))
plt.show()
Upvotes: 0
Reputation: 572
The way you define your lists is the issue.
Try:
E = [E1, E2, E3, E4]
k = [k1, k2, k3, k4]
Or if you want to use numpy:
E = np.array([E1, E2, E3, E4])
k = np.array([k1, k2, k3, k4])
Upvotes: 4