In [14]:
from sympy import *
In [15]:
init_printing(use_unicode=True)
In [16]:
x, y, z = symbols('x y z', real=True)
a, c = symbols('a c', nonzero=True, real=True)
In [17]:
integrate?
There are two ways to use the integrate
function. In one line, like integrate(x,(x,0,1))
or by naming an expression and then integrating it over a range:
A = (c*cos((pi*x)/(2.0*a)))**2
A.integrate((x,-a,a),conds='none')
We'll use both, at different times. For longer expressions, the second form can be easier to read and write.
First, just try the following, then we'll re-create some examples in the book.
In [58]:
integrate(x,(x,0,1))
Out[58]:
In [59]:
integrate(x**2,(x,0,1))
Out[59]:
The cell below will return an odd set of conditions on the result. This is because the solver doesn't want to assume anything about a
and there is a special case where the answer would be different. If you look closely though, that special case isn't physically realistic so to igore these special conditions, we add conds='none'
. The next cell down does what you'd expect. From here on out, just add this to the integrate
function and we'll get what we expect.
In [64]:
A = (c*cos((pi*x)/(2.0*a)))**2
A.integrate((x,-a,a))
Out[64]:
In [65]:
A = (c*cos((pi*x)/(2.0*a)))**2
A.integrate((x,-a,a), conds='none')
Out[65]:
So this tells us the normalization constant should be $c=\frac{1}{\sqrt{a}}$. Check that it is normalized if we do that:
In [68]:
psi = 1/sqrt(a)*cos((pi*x)/(2.0*a)) # notice we can name the expression something useful.
B = psi**2
B.integrate( (x,-a,a), conds='none')
Out[68]:
Because psi
is a real function, we can calculate expectation values by integrating over $x$ or $x^2$ with psi**2
:
In [70]:
C = x*psi**2
C.integrate( (x,-a,a), conds='none')
Out[70]:
In [71]:
D = x**2 * psi**2
E = D.integrate( (x,-a,a), conds='none')
In [73]:
E.n() # the .n() method approximates the numerical part. You can look at the full expression below.
Out[73]:
In [74]:
E
Out[74]:
In [44]:
h = Symbol('hbar', real=True)
Use the diff
function to take a derivative of a symbolic expression. For example:
In [75]:
diff(x**2, x)
Out[75]:
In [45]:
# Solution goes here
In [28]:
# Solution goes here
In [29]:
p = Symbol('p', real=True)
In [42]:
# Solution goes here
In [43]:
# Solution goes here
In [ ]: