Variational Approach to 1D Square Well

Remove["Global`*"]
$Assumptions={ℏ>0,m>0,a>0,α>1/2}

First do the simple parabolic wave function, and do the normalization integral

ψ=N(a^2/4-x^2);

int=Integrate[ψ^2,{x,-a/2,a/2}]

sol=Solve[int==1,N]

ψ=ψ/.sol[[2]]

Integrate[ψ^2,{x,-a/2,a/2}]==1

Now calculate the ground state energy

estE=Integrate[ψ(-ℏ^2/(2m))D[ψ,{x,2}],{x,-a/2,a/2}]

Compare to the true ground state energy

truE=ℏ^2Pi^2/(2m a^2);
Simplify[estE/truE]

N[%]

Calculate the overlap integral

truψ=Sqrt[2/a]Cos[Pi x/a];
Integrate[ψ truψ,{x,-a/2,a/2}]

N[%]

Plot the two wave functions

ψPlot=ψ/.a->1;
truψPlot=truψ/.a->1;
Plot[{ψPlot,truψPlot},{x,-1/2,1/2}]

Now do something that lets us have a variable parameter α

ψ=N(a^2/4-x^2)^α;
int=Integrate[ψ^2,{x,-a/2,a/2}]

sol=Solve[int==1,N];
ψ=ψ/.sol[[2]];

estE=Integrate[ψ(-ℏ^2/(2m))D[ψ,{x,2}],{x,-a/2,a/2}]

Minimize the ground state energy with respect to α

sol=Solve[D[estE,α]==0,α]

N[α/.sol[[2]]]

estEmin=estE/.sol[[2]]

Do the comparisons

Simplify[estEmin/truE]

N[%]

ψmin=ψ/.sol[[2]];
Integrate[ψmin truψ,{x,-a/2,a/2}]

N[%]

ψPlot=ψmin/.a->1;
truψPlot=truψ/.a->1;
Plot[{ψPlot,truψPlot},{x,-1/2,1/2}]