SetDirectory[NotebookDirectory[]];
a=1;hbar=1;m=1.0;xini=-5a;xlast=5a;Deltat=0.1;
Psi[x_,t_]:=(1/(Pi*Sqrt[2a]))NIntegrate[Sin[k*a]/k*Exp[I*(k*x - 0.5*hbar*k^2*t/m)],{k,-Infinity,Infinity}];

Psisq[x_,t_]:=Conjugate[Psi[x,t]]*Psi[x,t];
tPsisq[t_]:=Table[{x,Re[Psisq[x,t]]},{x,xini,xlast,0.1a}];

(****** Plotting phi[k] ********)
phi[k_]:=Sqrt[0.5a/Pi]Sin[k*a]/(k*a);
Plot[phi[k]^2,{k,-2Pi/a,2Pi/a},PlotLabel->"|phi|² vs k "]


(****** Plotting initial profile Psi[x,t=0] ********)
t=0;
ListPlot[tPsisq[t],Joined->True,Mesh->All,PlotRange->{0,1/(2*a)},PlotLabel->{"|Psi|² vs x at t = ",t}]


(* Visualising evolution of Psi[x,t] as a function of time: *)
For[j=0,j≤20,j++,
t=j*Deltat;
dum[j]=tPsisq[t];
];(*end for j*)

Manipulate[
ListPlot[dum[i],Joined->True,Mesh->All,PlotRange->{0,1/(2*a)},PlotLabel->{"|Psi|² vs x at t = ",i*Deltat}]
,{i,0,20,1}]