Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Overview
This interactive SageMath worksheet will explain how to define variables and functions, evaluate and plot functions, as well as finding derivatives and integrals of symbolic functions. We will use the quantum mechanical wave function of a free particle as an example.
Goals of this activity:
Plot the real and imaginary elements of the free particle wave function (\psi(x,t) = A_0 e^{i(kx - \omega t)})
Create an animated plot of the wave function
Check that the wave function satisfies the Schrodinger's equation
Schrodinger's equation for a one-dimensional system (e.g. think of an electron confined to a nanowire) is [\frac{-\hbar^2}{2m}\frac{\partial^2}{\partial x^2} \psi(x,t) +V(x)\psi(x,t) = i\hbar \frac{\partial}{\partial t} \psi(x,t)].
A free particle is a particle that does not experience any forces along the direction of motion and can move freely without any walls. In truth there is no such thing as a free particle since all particle are confined in some ways but as long as the dimensions of confinement are much much larger than the wavelength of the particle we can treat the particle as free. An electron confined to a nanowire with a length on the order of millimeters would be a good example of this.
Defining variables and functions
All variables must be explicitly declared (except for the variable 'x' and variables used as arguments of functions) using the var() command
Variables cannot start with numbers or contain spaces but they can contain underscores
Functions can be evaluated by inserting values for the arguments
To evaluate an input cell in this worksheet, put the cursor in the cell and hit Shift+Enter. The output will be displayed in the cell below the input cell.
What is the free particle wave function solution to Schrodinger's equation?
The general solution to Schrodinger's equation is (\psi(x,t) = A_0 e^{i(kx-\omega t)} + B_0 e^{i(kx + \omega t)}) where the only difference between the two terms is the sign in front of (\omega t), which determines which direction the wave travels. For the moment we will only look at the first term by setting (B_0=0).
Plotting functions
To replace variables with numerical values you need to use the .substitute() command.
To replace all values of the variable with the value , append '.substitute(A_0=1)' to the function name.
The real and imaginary parts of a function can be obtained by using .real() and .imag().
Since SageMath is based on Python, everything is an object. This means that you can modify properties or act on things using the "dot" notation.
What does the free particle wave function look like?
The solution to Schrodinger's equation must be a complex function so the wave function must be split into it's real part and imaginary part to plot the function. The free particle wave function for a one-dimensional system is . What do you think the graph of the real part of the wave function will look like? What do you think the imaginary part of the wave function will look like? (Hint: What is Euler's identity?)
For you to try:
Use the fact that to plot the real part of the wave function at time . Type your work into the input cell below.
How does your graph for the real part of the wave function compare to the graph of the real part above? What function do you think represents the imaginary part of the free particle wave function?
Animating plots
A collection of still images can be combined to create an animation
Put all images in a list (square brackets denote a list)
Use the list of images as an argument to the
animage(list_of_images_here)
command
Which way is the free particle wave function moving?
To determine the direction that the wave is moving you can create an animated plot at different times using a short Python script. You will need to create a list of plot snapshots at different times and then combine them into an animation.
Note:
You can accomplish the same thing with more compact code using Python list comprehensions:
Working with complex functions
Assumptions about variables can be specified using the assume() command. In this case we assume all variables are real.
The complex conjugate can be found using .conjugate().
print() and pretty_print() can be used to include text in an output cell.
Calculate the probability density for the free particle wave function
The wave function (\psi(x,t)) is called the probability amplitude of the particle. The probility density (\rho(x,t)) is equal to the magnitude of the probability amplitude squared. Since (\psi(x,t)) is complex, the magnitude squared is not the same as the square of (\psi(x,t)). You must multiply (\psi(x,t)) by it's complex conjugate, which is written as (\psi^(x,t)). The probability density is then given by (\rho(x,t) = \psi^(x,t) \psi(x,t)).
How would you find the probability to find a particle between two points given the probability density? In other words, how is the probability density related to the probability to find the particle within some region of space?
For you to try:
SageMath has a help system that is easy to use. To figure out how to use a command, type the name of the command followed by the question mark '?'.
SageMath also has an autocomplete feature where you can hit the first few letters of a command followed by the Tab key to get a list of possible commands. These two tools used together can help you tremendously.
To see what other assumptions you can make, type 'assume?' into the input cell below.
To get help with the integral() command, first type 'int' and then hit Tab to use autocomplete. Select 'integral' from the pop-up list and add a question mark to the end before evaluating the cell by hitting Shift+Enter
Taking derivatives of functions
The command .derivative() will find the derivative of a symbolic function.
The arguments of the .derivative() command are the variable of differentiation and the order of the derivative
To find the third derivative of the function with respect to use:
The output from the .derivative() command is another function, which you can evaluate or set equal to another variable.
Note:
Rather than using the "dot" notation, you can treat the derivative() command as a function. You can also use diff() in place of derivative; the results are the same for both commands.
Here are a couple of examples that are all equivalent:
Defining functionals involved derivatives
Another way to define functions is using Python commands.
Definition must start with def followed by the name of the function and the arguments of the function.
First line of function definition must end with a colon :
All code included in the function must be indented four spaces (one tab)
Last line of function defintion must return resulting equation
Momentum Operator
In quantum mechanics, all observable quantities have an associated operator. These operators require a wave function to operate on. The momentum operator in one-dimension is defined as [\hat{p}=-i\hbar \frac{d}{dx}]. The value of the momentum (p) is found by applying the operator to the wave function. In other words, [p\psi(x,t) = \hat{p} \psi(x,t)] or, dividing both sides by (\psi(x,t)), [p=\frac{\hat{p}\psi(x,t)}{\psi(x,t)}.] Notice that (\psi(x,t)) doesn't cancel out the (\psi(x,t)) in the numerator because the numerator is (\hat{p}) operating on (\psi(x,t)) which is different than multiplying the two terms together.
The momentum of a free particle is given by (\hbar k), which is what the following calculation demonstrates.
For you to try:
Schrodinger's equation is given by [\frac{-\hbar^2}{2m}\frac{\partial^2}{\partial x^2} \psi(x,t) +V(x)\psi(x,t) = i\hbar \frac{\partial}{\partial t} \psi(x,t).] In order to demonstrate that (\psi(x,t) = A_0 e^{i(kx - \omega t)}) satisfies this equation, you need to plug this equation for (\psi(x,t)) into both sides of the equation and show both sides are equal to one another.
First, take the second derivative of the wave function with respect to in the cell below. Multiply this by . This will give you the left-hand side of Schrodinger's equation.
Next find the first derivative of with respect to . Multiply this result by to get the right-hand side of Schrodinger's equation. Compare this equation to the previous equation. The two equations should agree if you recognize that .
Use .substitute(omega = hbar k^2/(2*M)) on this second equation and both equations should match, showing that this wave function satisfies Schrodinger's equation.
Another exampe of defining functionals involving derivatives
We will use the Python def command again to define our two functions. To define the Hamiltonian operator we would use a Python definition that returns the desired function. We can define the energy operator in a similar fashion. These two functions can then be used to find and
Defining Schrodinger's equation in SageMath
The left-hand side of Schrodinger's equation is called the Hamiltonian which you may have heard of in a classical physics course. The Hamiltonian (\hat{H}) and energy (\hat{E}) are operators which must have a function to operate on. In this worksheet we use the hat symbol (\hat{}) above the letter to indicate an operator but your instructor may use different notation. The operator (\hat{H}) is called 'H-hat'.
In terms of operators, Schrodinger's equation can be written as (\hat{H}\psi(x,t) = \hat{E} \psi(x,t)) where we understand that (\hat{H}) and (\hat{E}) operator on the functions immediately to their right.
Solving algebraic equations
When solving equations you must remember the difference between and .
The 'double-equals' sign is a comparison and is what you should use with the solve() command.
The "single-equals' sign is an assignment operator and sets the left hand side to the value on the right.
The arguments of solve() are the equation to solve and the variable to solve for.
Type solve? into an input cell to see the other options available for the solve() command.
Find the values of (k) that satisfy Schrodinger's equation
We've already specified the equation that relates (k) and (\omega) but the following will show you how to use SageMath to find this relation using Schrodinger's equation. We'll use the previous definitions of (H) and (E) along with (\psi(x,t)) to find (k).
For you to try:
Solve the equation (H \psi(x,t) == E \psi(x,t)) for omega rather than k and show the result is (\omega = \frac{\hbar^2 k^2}{2m}).
Extract a solution from a list of answers
The solution from the solve() command is a Python list and we can extract elements of the equations from the list. Python starts indices at 0 rather than 1 so the first element in ans is ans[0] and the second element is ans[1].
You can extract the right hand side of an equation using .rhs().
To see if the equation holds true we need to compare the two sides using a double equals sign (==) and force SageMath to give us a Boolean answer by using bool().
Double-check that satisfies the wave equation
To help practice using SageMath, we will take the solution obtained for (k) and plug it back into Schrodinger's equation (again).
Integrating a function
The command to integrate is .integrate() or you can apply integrate() as a function to the equation.
For an indefinite integral you only need to specify the variable of integration
For a definite integral you must specify the variable of integration and the limits.
For example, to integrate the function (f(x)) from (x=0) to (x=L) you'd type: integrate(f(x),[x,0,L])
Normalize the wave function and find the probability to find the free particle between to points in space
To normalize the wave function, the total probability to find the particle somewhere must equal 1. Unfortunately with the free particle you run into an issue because the free particle can be anywhere between and . Using these limits results in a divergent integral. To get around the infinite integral we will consider the case where the particle is limited to some region between and where where is the wavelength of the free particle.
Other ways to do things
In addition to using the "dot" notation to operate on functions, you can also use various operations as functions. For instance, derivative(psi,x) gives the same result as psi.derivative(x)
Project Ideas and Problems to Solve
Have students find realistic values for masses and velocities for electrons, protons, or neutrons and use de Broglie's equation to find (k), (\omega), and total energy (E).
Plot wave functions for electrons and protons on the same plot and have students explain why electron microscopes are used for imaging small objects while 'proton microscopes' aren't as useful.
Have students complete homework problems from the end of the textbook chapter.