# -*- coding: utf-8 -*- # Python simulation of an electron in a 1d infinite box potential # Integrate time independent SE using the Verlet method # Boundary conditions are found by shooting # MW 190402 from pylab import * import numpy as np import matplotlib.pyplot as plt from bisection import bisection # Constants a=1.0e-9 # well width a=1 nm hbar=1.0545718e-34 # Plancks constant m=9.10938356e-31 # electron mass e=1.60217662e-19 # electron charge=-e c=2.0*m/hbar**2 # constant in Schrödinger equation # EeV = 0.3 # input energy in eV: test 0.3 , 0.4 , 0.3760 , 1.5 #print('Exact solution for infinite box potential') # print('E1=',(hbar*np.pi/a)**2/(2.0*m)/e,'eV') # print('E2=',(hbar*5.0*np.pi/a)**2/(2.0*m)/e,'eV') # potential energy function # V0=10*e def V(x): y = 0.0 return y def verletmethod(EeV, N): #, N=1000): E = EeV*e # input energy in J # N=1000 # number of mesh points dx=a/N # step length dx2=dx**2 # step length squared # initial values and lists x = 0 # initial value of position x psi = 0.0 # wave function at initial position dpsi = 1.0 # derivative of wave function at initial position x_tab = [] # list to store positions for plot psi_tab = [] # list to store wave function for plot x_tab.append(x/a) psi_tab.append(psi) for i in range(N) : d2psi = c*(V(x)-E)*psi d2psinew = c*(V(x+dx)-E)*psi psi += dpsi*dx + 0.5*d2psi*dx2 dpsi += 0.5*(d2psi+d2psinew)*dx x += dx x_tab.append(x/a) psi_tab.append(psi) print('E=',EeV,'eV , psi(x=a)=',psi) # return x_tab, psi_tab return psi def plot_line(x_tab, psi_tab): plt.close() plt.figure(num=None, figsize=(8,8), dpi=80, facecolor='w', edgecolor='k') plt.plot(x_tab, psi_tab, linewidth=1, color='red') #plt.xlim(0, 1) #limit=1.e-9 #plt.ylim(0, limit) #plt.ylim(-limit, limit) #plt.autoscale(False) plt.xlabel('x/a') plt.ylabel('$\psi$') #plt.savefig('psi.pdf') plt.show() def error_plot(): dpsi_a = [] EeV1 = (hbar*np.pi/a)**2/(2.0*m)/e N = [1000,10000,100000] for n in N: psi = verletmethod(EeV1, n) dpsi_a.append(psi) print(dpsi_a) print(N) plt.close() plt.figure(num=None, figsize=(8,8), dpi=80, facecolor='w', edgecolor='k') plt.loglog(N, dpsi_a, 'r-x', linewidth=1, color='red') plt.xlabel('N') plt.ylabel('$\Delta \Psi(x=a)$') plt.grid(True, which="both") plt.show() def main(): # x_tab, psi_tab = # psi = verletmethod(1.5) # plot_line(x_tab, psi_tab) # print(bisection(verletmethod, 9.3, 9.5, 1e-9)) error_plot() # Correct pass if __name__ == '__main__': main()
Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.
OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.
import sys
name = sys.stdin.readline()
print("Hello "+ name)
Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.
When ever you want to perform a set of operations based on a condition IF-ELSE is used.
if conditional-expression
#code
elif conditional-expression
#code
else:
#code
Indentation is very important in Python, make sure the indentation is followed correctly
For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.
mylist=("Iphone","Pixel","Samsung")
for i in mylist:
print(i)
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition
#code
There are four types of collections in Python.
List is a collection which is ordered and can be changed. Lists are specified in square brackets.
mylist=["iPhone","Pixel","Samsung"]
print(mylist)
Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
Below throws an error if you assign another value to tuple again.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)
Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.
myset = {"iPhone","Pixel","Samsung"}
print(myset)
Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.
mydict = {
"brand" :"iPhone",
"model": "iPhone 11"
}
print(mydict)
Following are the libraries supported by OneCompiler's Python compiler
Name | Description |
---|---|
NumPy | NumPy python library helps users to work on arrays with ease |
SciPy | SciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation |
SKLearn/Scikit-learn | Scikit-learn or Scikit-learn is the most useful library for machine learning in Python |
Pandas | Pandas is the most efficient Python library for data manipulation and analysis |
DOcplex | DOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling |