/
bonusWavefunctions.py
430 lines (365 loc) · 17.1 KB
/
bonusWavefunctions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.7.1
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# Wavefunction Practice Questions
# Here are some plots and equtions related to the Bonus Wavefunction Questions.
#
#
# +
#These are just the initial imports to get the plots later
import numpy as np #import the numpy library as np
import matplotlib.pyplot as plt #import the pyplot library as plt
import matplotlib.style #Some style nonsense
import matplotlib as mpl #Some more style nonsense
import math
#Set default figure size
#mpl.rcParams['figure.figsize'] = [12.0, 8.0] #Inches... of course it is inches
mpl.rcParams["legend.frameon"] = False
mpl.rcParams['figure.dpi']=150 # dots per inch
# -
# # Question 1
#
# The question gives us this piecewise wavefunction
# $$\psi(x)=\begin{cases}
# A \cos( \frac{2}{\pi x L}) & \text{if } \frac{-L}{4} \leq x \leq \frac{L}{4} \\
# 0 & \text{otherwise}
# \end{cases}$$
#
# We will use the python numpy library to code up this function using the piecewise method. Note that the $x$ below will refer to an array of numbers rather than just a single number, which means we have to be careful how we define the conditions and the function.
#Define a function which will operate on an array of x values all at once
def cos2pixoverL(A,x,L):
conds = [x < -L/4, (x >= -L/4) & (x <= L/4), x > L/4] #The three regions of x
funcs = [lambda x: 0, lambda x: A*np.cos(2*math.pi*x/L), # sin(pi x) in the middle and 0 outsides
lambda x: 0] #the lambda keyword is allowing us to define a quick function
return np.piecewise(x, conds, funcs) #Now do the piecewise calculation and return it
#Now we will use the linspace function to get 100 numbers
# linearly spaced between -L and L and lets set L=1
x = np.linspace(-0.5, 0.5, 100)
#print("x=",x) #Just print the numbers, since -2 and 4 are in the 100 numbers the step size is not 0.06
#print("cos2pixoveL(x)=",cos2pixoverL(1,x,1)) #Print the wavefunction numbers
#Now let's create out first plot
fig, ax = plt.subplots() #I like to make plots using this silly fig,ax method but plot how you like
ax.plot(x,cos2pixoverL(1,x,1),linewidth=3) #Plot x vs \psi(x)
ax.set_title(r"$\psi(x)=A\cos(\frac{2\pi x}{L})$ for $A=1, L=1, -0.5\leq x \leq 0.5$") #Set the title
ax.set_xlabel("$x$") # Set the x-axis label
ax.set_ylabel("$\psi(x)$") # Set the y-axis label
ax.grid() # Draw a grid
# ## Probability density
# We saw in the lectures that the probability density associated with the waveform is:
# $$ \rho(x) = \left| \psi(x) \right|^2$$
# so in our case we have
# $$\rho(x)=\left| \psi(x) \right|^2 =\begin{cases}
# A^2 \cos^2(\frac{2 \pi x}{L}) & \text{if } -L/4 \leq x \leq L/4 \\
# 0 & \text{otherwise}
# \end{cases}$$
#
# So now we can plot this function
#Now let's create our next plot, and zoom in to the non-zero region
x = np.linspace(-0.5, 0.5, 100) # Return an array of 100 numbers linearly spaced between -0.5 and 0.5
fig, ax = plt.subplots() #I like to make plots using this silly fig,ax method but plot how you like
ax.plot(x,cos2pixoverL(1,x,1)**2,linewidth=3) # Plot x vs psi^2
ax.set_title(r"$\rho(x)=A^2 \cos^2(\frac{2\pi x}{L})$ for $A=1, L=1, -0.5\leq x \leq 0.5$") # Set the title
ax.set_xlabel("$x$") # Set the x-axis label
ax.set_ylabel(r"$\rho(x)$") # Set the y-axis label
ax.grid() # Draw a grid
# ## Question 1a) Normalisation
#
# We can integrate the probability density in order to determine what the normalisation constant $A$ should be.
# $$\int_{-\infty}^{\infty} A^2 \cos^2 \left(\frac{2 \pi x}{L}\right) dx = \int_{-\frac{L}{4}}^{\frac{L}{4}} A^2 \cos^2 \left(\frac{2 \pi x}{L}\right) dx = 1 $$
# we can use the double angle formula $cos^2 \theta = \frac{1}{2}\left(1 + \cos \left(2 \theta \right) \right) $ to get
# $$ 1 = \int_{-\frac{L}{4}}^{\frac{L}{4}} \frac{A^2}{2} dx + \int_{-\frac{L}{4}}^{\frac{L}{4}} \frac{A^2}{2} \cos \left(\frac{4 \pi x}{L}\right) dx $$
# $$ 1 = \left[ \frac{xA^2}{2}\right]_{-\frac{L}{4}}^{\frac{L}{4}} + \left[ \frac{A^2}{2} \frac{\sin \left(\frac{4 \pi x}{L}\right)}{4\pi/L}\right]_{-\frac{L}{4}}^{\frac{L}{4}} $$
# The second term is zero since $\sin \pi =0$, giving
# $$ 1 = \left( \frac{LA^2}{8} + \frac{LA^2}{8} \right) =\frac{LA^2}{4} $$
# and therefore
# $$ A = \frac{2}{\sqrt{L}}$$
#Now let's create our next plot, and zoom in to the non-zero region
L=1
A=2/np.sqrt(L)
x = np.linspace(-0.5, 0.5, 100) # Return an array of 100 numbers linearly spaced between -0.5 and 2.5
fig, ax = plt.subplots() #I like to make plots using this silly fig,ax method but plot how you like
ax.plot(x,cos2pixoverL(A,x,L)**2,linewidth=3) # Plot x vs psi^2
ax.set_title(r"$\rho(x)=A^2 \cos^2(\frac{2\pi x}{L})$ for $A=2/\sqrt{L}, L=1, -0.5\leq x \leq 0.5$") # Set the title
ax.set_xlabel("$x$") # Set the x-axis label
ax.set_ylabel(r"$\rho(x)$") # Set the y-axis label
ax.grid() # Draw a grid
# ### Is this normalised?
# We can also use the computer to check if we got the normalistion correct.
# The integral is just the area under the curve.
# numpy.trapz uses the trapezium rule to calculate the area under the curve
norm=np.trapz(cos2pixoverL(A,x,L)**2,dx=x[1]-x[0])
print("Did we normalise the wavefunction?\nN=",norm)
# ## Question 1b) Probability
#
# We can integrate the probability density in order to determine what the normalisation constant $A$ should be.
# $$P(0 \leq x \leq \frac{L}{8}) = \int_{0}^{\frac{L}{8}} \frac{2}{L} \cos^2 \left(\frac{2 \pi x}{L}\right) dx $$
# we can use the double angle formula $cos^2 \theta = \frac{1}{2}\left(1 + \cos \left(2 \theta \right) \right) $ to get
# $$ P(0 \leq x \leq \frac{L}{8}) = \int_{0}^{\frac{L}{8}} \frac{2}{L} dx + \int_{0}^{\frac{L}{8}} \frac{2}{L} \cos \left(\frac{4 \pi x}{L}\right) dx $$
# $$ P(0 \leq x \leq \frac{L}{8}) = \left[ \frac{x2}{L}\right]_{0}^{\frac{L}{8}} + \left[ \frac{2}{L} \frac{\sin \left(\frac{4 \pi x}{L}\right)}{4\pi/L}\right]_{0}^{\frac{L}{8}} $$
# which gives
# $$ P(0 \leq x \leq \frac{L}{8}) = \frac{1}{4}+ \frac{1}{2 \pi }\left( \sin \left(\frac{\pi }{2}\right) - \sin 0 \right) $$
# $$ P(0 \leq x \leq \frac{L}{8}) = \frac{1}{4}+ \frac{1}{2 \pi }$$
#
#Which again we can check
x = np.linspace(0, 1./8., 100) # 0 to 1/8 in 100 steps
prob=np.trapz(cos2pixoverL(A,x,L)**2,dx=x[1]-x[0])
print("What is the probability of finding the system in 0<x<L/8?\nP=",prob)
print("1/4 + 1/(2pi)=",0.25 + 1./(2*math.pi))
# # Question 2
# Now we have another wavefunction question. This time our wavefunction is
# $$ \psi(x) = A \exp^{-\frac{(x-\mu)^2}{4 \sigma^2}} $$
#
# Once again first we will plot the wavefunction
# +
#Define a function which will operate on an array of x values all at once
def psiq2(A,x,mu,sig):
return A *np.exp(-((x-mu)**2)/(4 *sig**2))
#Set our initial mu, sigma and A for plotting
mu=0
sig=1
A=1
#Now let's create plot psiq2
fig, ax = plt.subplots() #I like to make plots using this silly fig,ax method but plot how you like
x = np.linspace(-5*sig,5*sig,100)
ax.plot(x,psiq2(A,x,mu,sig),linewidth=3) #Plot x vs \psi(x)
ax.set_title(r"$\psi(x)=A \exp^{-\frac{(x-\mu)^2}{4 \sigma^2}}$ for $A="+str(A)+", \mu="+str(mu)+", \sigma="+str(sig)+"$") #Set the title
ax.set_xlabel("$x/\sigma$") # Set the x-axis label
ax.set_ylabel("$\psi(x)$") # Set the y-axis label
ax.grid() # Draw a grid
# -
# ## Now do the normalisation
# The wavefunction is $$ \psi(x) = A \exp^{-\frac{(x-\mu)^2}{4 \sigma^2}}. $$
#
# The normalisation condition is
# $$ 1= \int_{-\infty}^{\infty} \left| \psi(x) \right|^2 dx = \int_{-\infty}^{\infty} A^2 \exp^{-\frac{(x-\mu)^2}{2 \sigma^2}} dx. $$
#
# Let $$y=\frac{x-\mu}{\sqrt{2}\sigma}$$
# so $$dy = \frac{dx}{\sqrt{2}\sigma} $$
#
# The normalisation condition is now
# $$ 1= \int_{-\infty}^{\infty} A^2 \exp^{-y^2} \sqrt{2}\sigma dy $$
# using
# $$ \int_{-\infty}^{\infty} e^{-y^2} dy = \sqrt{\pi} $$
# we get
# $$ 1=A^2 \sqrt{\pi} \sqrt{2}\sigma $$
# so
# $$ A = \frac{1}{\sqrt{\sqrt{2\pi} \sigma}} $$
# +
#Set our initial mu, sigma and A for plotting
mu=0
sig=1
A=1/np.sqrt(sig*np.sqrt(2*math.pi))
#Now let's create plot psiq2
fig, ax = plt.subplots() #I like to make plots using this silly fig,ax method but plot how you like
x = np.linspace(-5*sig,5*sig,100)
ax.plot(x,psiq2(A,x,mu,sig),linewidth=3) #Plot x vs \psi(x)
ax.set_title(r"$\psi(x)=A \exp^{-\frac{(x-\mu)^2}{4 \sigma^2}}$ for $A="+str(A)+", \mu="+str(mu)+", \sigma="+str(sig)+"$") #Set the title
ax.set_xlabel("$x/\sigma$") # Set the x-axis label
ax.set_ylabel("$\psi(x)$") # Set the y-axis label
ax.grid() # Draw a grid
# -
#Once more we can check by numerical integration using numpy.trapz
norm=np.trapz(psiq2(A,x,mu,sig)**2,dx=x[1]-x[0])
print("Did we normalise the wavefunction?\nN=",norm)
# # Question 3
# Now we have a string of wavefunctions to test.
#
# a) $ \psi(x) = N e^{a x^2}$
#
# b) $ \psi(x) = N e^{-a x^2}$
#
# c) $ \psi(x) = N \frac{e^{-a x^2}}{\left(3-x\right)}$
#
# d) $ \psi(x) = N e^{a x}$
#
# e) $ \psi(x) = N e^{-a x}$
# +
#Define a function which will operate on an array of x values all at once
def psiq3a(N,x,a):
return N *np.exp(a*x**2)
def psiq3b(N,x,a):
return N *np.exp(-a*x**2)
def psiq3c(N,x,a):
return N *np.exp(-a*x**2)/(3-x)
def psiq3d(N,x,a):
return N *np.exp(a*x)
def psiq3e(N,x,a):
return N *np.exp(-a*x)
# -
#Now let's plot probabilty density
N=1
a=1
fig, ax = plt.subplots() #I like to make plots using this silly fig,ax method but plot how you like
x = np.linspace(-5*sig,5*sig,1000)
ax.plot(x,psiq3a(N,x,a)**2,linewidth=3,label="a") #Plot x vs \psi(x)
ax.set_title(r"$\rho_a(x)=\psi^2_a(x)$") #Set the title
ax.set_xlabel("$x$") # Set the x-axis label
ax.set_ylabel("$\psi(x)$") # Set the y-axis label
ax.grid() # Draw a grid
ax.set_yscale("log")
# ## a)
# Clearly $ \psi(x) = N e^{a x^2}$ is smooth an continuous, but also this is not normalisable as the above graph diverges for both positive and negative $x$
#Now let's plot probability density
N=1
a=1
fig, ax = plt.subplots() #I like to make plots using this silly fig,ax method but plot how you like
x = np.linspace(-5*sig,5*sig,1000)
ax.plot(x,psiq3b(N,x,a)**2,linewidth=3,label="a") #Plot x vs \psi(x)
ax.set_title(r"$\rho_b(x)=\psi^2_b(x)$") #Set the title
ax.set_xlabel("$x$") # Set the x-axis label
ax.set_ylabel("$\psi(x)$") # Set the y-axis label
ax.grid() # Draw a grid
# ## b)
# Clearly $ \psi(x) = N e^{-a x^2}$ is smooth and continuous, and since the area under the above curve is finite it will be normalisable
#
#Now let's plot probability density
N=1
a=1
fig, ax = plt.subplots() #I like to make plots using this silly fig,ax method but plot how you like
x = np.linspace(-5*sig,5*sig,1000)
ax.plot(x,psiq3c(N,x,a)**2,linewidth=3,label="a") #Plot x vs \psi(x)
ax.set_title(r"$\rho_c(x)=\psi^2_c(x)$") #Set the title
ax.set_xlabel("$x$") # Set the x-axis label
ax.set_ylabel("$\psi(x)$") # Set the y-axis label
ax.grid() # Draw a grid
#Now let's plot the wavefunction
N=1
a=1
fig, ax = plt.subplots() #I like to make plots using this silly fig,ax method but plot how you like
x = np.linspace(-5*sig,5*sig,10000)
ax.plot(x,psiq3c(N,x,a),linewidth=3,label="a") #Plot x vs \psi(x)
ax.set_title(r"$\psi_c(x)$") #Set the title
ax.set_xlabel("$x$") # Set the x-axis label
ax.set_ylabel("$\psi(x)$") # Set the y-axis label
ax.grid() # Draw a grid
# ## c)
# $$ \psi(x) = N \frac{e^{-a x^2}}{\left(3-x\right)}$$
# Whilst the area under the probability density is finite this function is not smooth and continuous (flipping from +ve to -ve infinity at 3.
#
#
#Now let's plot probability density
N=1
a=1
fig, ax = plt.subplots() #I like to make plots using this silly fig,ax method but plot how you like
x = np.linspace(-5*sig,5*sig,1000)
ax.plot(x,psiq3d(N,x,a)**2,linewidth=3,label="a") #Plot x vs \psi(x)
ax.set_title(r"$\rho_d(x)=\psi^2_d(x)$") #Set the title
ax.set_xlabel("$x$") # Set the x-axis label
ax.set_ylabel("$\psi(x)$") # Set the y-axis label
ax.grid() # Draw a grid
# ## d)
# Clearly $ \psi(x) = N e^{a x}$ is smooth an continuous, but also this is not normalisable as the above graph diverges for positive $x$
#Now let's plot probability density
N=1
a=1
fig, ax = plt.subplots() #I like to make plots using this silly fig,ax method but plot how you like
x = np.linspace(-5*sig,5*sig,1000)
ax.plot(x,psiq3e(N,x,a)**2,linewidth=3,label="a") #Plot x vs \psi(x)
ax.set_title(r"$\rho_e(x)=\psi^2_e(x)$") #Set the title
ax.set_xlabel("$x$") # Set the x-axis label
ax.set_ylabel("$\psi(x)$") # Set the y-axis label
ax.grid() # Draw a grid
# ## e)
# Clearly $ \psi(x) = N e^{-a x}$ is smooth an continuous, but also this is not normalisable as the above graph diverges for negative $x$
# # Question 4
#
#
# The question gives us this piecewise wavefunction
# $$\psi(x)=\begin{cases}
# \frac{Ax}{a} & \text{if } 0 \leq x \lt a \\
# \frac{A(b-x)}{(b-a)} & \text{if } a \leq x \lt b \\
# 0 & \text{otherwise}
# \end{cases}$$
#Define a function which will operate on an array of x values all at once
def psiq4(A,x,a,b):
conds = [x < 0, (x>= 0) & (x<a), (x >= a) & (x <= b), x > b] #The four regions of x
funcs = [lambda x:0, lambda x: A*x/a, lambda x: A*(b-x)/(b-a),
lambda x: 0] #the lambda keyword is allowing us to define a quick function
return np.piecewise(x, conds, funcs) #Now do the piecewise calculation and return it
#Now let's plot the wavefunction
A=1
a=1.5
b=3
fig, ax = plt.subplots() #I like to make plots using this silly fig,ax method but plot how you like
x = np.linspace(-1,4,10000)
ax.plot(x,psiq4(A,x,a,b),linewidth=3,label="a") #Plot x vs \psi(x)
ax.set_title(r"$\psi_4(x), A="+str(A)+", a="+str(a)+", b="+str(b)+"$") #Set the title
ax.set_xlabel("$x$") # Set the x-axis label
ax.set_ylabel("$\psi(x)$") # Set the y-axis label
ax.grid() # Draw a grid
# ## a) Normalisation
#
# We can integrate the probability density in order to determine what the normalisation constant $A$ should be.
# $$\int_{-\infty}^{\infty} | \psi(x) |^2 dx = \int_{0}^{a} \frac{A^2 x^2}{a^2} dx + \int_{a}^{b} \frac{A^2 (b-x)^2}{(b-a)^2} dx = 1 $$
# Taking the first term first
# $$ \int_{0}^{a} \frac{A^2 x^2}{a^2} dx = \left[ \frac{A^2 x^3}{3a^2} \right]_{0}^{a} = \frac{A^2 a}{3} $$
# Now the second term
# $$ \int_{a}^{b} \frac{A^2 (b-x)^2}{(b-a)^2} dx= \left[ -\frac{A^2 (b-x)^3}{3(b-a)^2} \right]_a^b = \frac{A^2 (b-a)}{3} $$
# Adding them together we get
# $$ 1 = \frac{A^2 b}{3} $$
# so
# $$ A=\sqrt{\frac{3}{b}}$$
# Interestingly the normalisation turns out to be independent of $a$, which makes since the length from 0 to $b$ always defines the bottom of the triangle.
# ## b) Plot wavefunction and probability density
#Now let's plot the wavefunction
a=1
b=3
A=np.sqrt(3/b)
fig, ax = plt.subplots() #I like to make plots using this silly fig,ax method but plot how you like
x = np.linspace(-1,4,10000)
ax.plot(x,psiq4(A,x,a,b),linewidth=3,label="a") #Plot x vs \psi(x)
ax.set_title(r"$\psi_4(x), A="+str(A)+", a="+str(a)+", b="+str(b)+"$") #Set the title
ax.set_xlabel("$x$") # Set the x-axis label
ax.set_ylabel("$\psi(x)$") # Set the y-axis label
ax.grid() # Draw a grid
#Now let's plot the probability density
a=1
b=3
A=np.sqrt(3/b)
fig, ax = plt.subplots() #I like to make plots using this silly fig,ax method but plot how you like
x = np.linspace(-1,4,10000)
ax.plot(x,psiq4(A,x,a,b)**2,linewidth=3,label="a") #Plot x vs \psi(x)
ax.set_title(r"$\rho_4(x)=\psi^2_4(x), A="+str(A)+", a="+str(a)+", b="+str(b)+"$") #Set the title
ax.set_xlabel("$x$") # Set the x-axis label
ax.set_ylabel("$\psi(x)$") # Set the y-axis label
ax.grid() # Draw a grid
# ## c) Probability
#
# We can integrate the probability density in order to determine what the normalisation constant $A$ should be.
# $$P(0 \leq x \leq a) = \int_{0}^{a} \frac{3 x^2}{b a^2} dx $$
# Doing the integral (again)
# $$ \int_{0}^{a} \frac{3 x^2}{b a^2} dx = \left[ \frac{x^3}{ b a^2} \right]_{0}^{a} = \frac{a}{b} $$
#
# ## d) Expectation Value
#
# Now we get to do a (slightly) different integral
# $$<x>=\int_{-\infty}^{\infty} x | \psi(x) |^2 dx = \int_{0}^{a} \frac{3 x^3}{b a^2} dx + \int_{a}^{b} \frac{3 x (b-x)^2}{b (b-a)^2} dx = 1 $$
# Again doing the easy one first
# $$ \int_{0}^{a} \frac{3 x^3}{b a^2} dx = \left[ \frac{3 x^4}{4b a^2} \right]_{0}^{a} = \frac{3 a^2}{4b}$$
# Now doing the second term
# $$ \int_{a}^{b} \frac{3 x (b-x)^2}{b (b-a)^2} dx = \int_{a}^{b} \frac{3(x b^2 -2bx^2 +x^3)}{b (b-a)^2} dx = \frac{3}{b (b-a)^2} \left[ \frac{x^2 b^2}{2} - \frac{2 b x^3}{3} + \frac{x^4}{4} \right]_{a}^{b} $$
# $$ \frac{3}{b (b-a)^2} \left[ (\frac{b^2 b^2}{2} - \frac{2 b b^3}{3} + \frac{b^4}{4}) - (\frac{a^2 b^2}{2} - \frac{2 b a^3}{3} + \frac{a^4}{4}) \right] $$
# Therefore
# $$ <x>= \frac{3a^2}{4b} + \frac{3}{b (b-a)^2} \left[ \frac{b^4}{12} - (\frac{a^2 b^2}{2} - \frac{2 b a^3}{3} + \frac{a^4}{4}) \right] $$
# Which is a nasty mess but if we plug in $a=1$ and $b=3$ we get
# $$<x>=\frac{5}{4}$$
# +
#Sadly this doesn't simplify any more but it is calcuable
def nastymess(a,b):
return (3*a**2/(4*b)) + ((b**4)/12 - ( (a**2 * b**2) /2 - 2 * b * a**3/3 + a**4/4))*3/(b*(b-a)**2)
# We can also evaulate this numerically
#Once more we can check by numerical integration using numpy.trapz
mu=np.trapz(x*psiq4(A,x,a,b)**2,dx=x[1]-x[0])
print("What is the numerically integrated expectation value:\n<x>=",mu)
print("What is the nasty mess value:\n<x>=",nastymess(a,b))
# -