Initialisierung

ShowCodeButton:=DynamicModule[{mode=1(*show*)},SelectionMove[EvaluationNotebook[],All,EvaluationCell];
SetOptions[NotebookSelection[EvaluationNotebook[]],CellOpen->False];
Button[Dynamic[If[mode==1,"Show code","Hide code"]],If[mode==1,SelectionMove[EvaluationNotebook[],All,EvaluationCell];
SelectionMove[EvaluationNotebook[],Previous,Cell];SetOptions[NotebookSelection[EvaluationNotebook[]],CellOpen->True];
mode=2,
(*else*)
SelectionMove[EvaluationNotebook[],All,EvaluationCell];
SelectionMove[EvaluationNotebook[],Previous,Cell];SetOptions[NotebookSelection[EvaluationNotebook[]],CellOpen->False];
mode=1
]
]
]

Plot

ShowCodeButton
Manipulate[
ℏ=1;
a=1;
m=1;
ndivs=300;
boundary=2a;
dx=2boundary/ndivs;
Vfunc1[x_]=(Piecewise[{{0, x<-(a/2)||(a/2)<x}, {-V, -a/2≤x≤a/2}}]);
Varray1=Table[Vfunc1[x],{x,-boundary,boundary,dx}];
MProblem1=N[DiagonalMatrix[-((ℏ^2)/(2 m))(((-2))/(dx^2))+Varray1]+DiagonalMatrix[Table[-((ℏ^2)/(2 m))(1/(dx^2)),{ndivs}],1]+DiagonalMatrix[Table[-((ℏ^2)/(2 m))(1/(dx^2)),{ndivs}],-1]];
answers1=Eigensystem[MProblem1];
{evals,evects}=Sort[Transpose[answers1],#1[[1]]<#2[[1]]&]//Transpose;
nboundstates=Select[evals,#<0&]//Length;
boundstateenergies=Take[evals,nboundstates];
potplot=ListPlot[Varray1,Filling->1.1Min[Varray1],Joined->True,Frame->True,PlotRange->All,GridLines->{None,boundstateenergies},GridLinesStyle->Directive[Thickness[0.002],Red,Dashed]];
ScaleFactor=0.3V;
stateplot=ListPlot[Table[ScaleFactor evects[[i]]+evals[[i]],{i,1,nboundstates}],Joined->True,Filling->Table[n->evals[[n]],{n,1,nboundstates}],PlotStyle->Red,FillingStyle->Directive[Red,Opacity[0.3]],PlotRange->All,ImageSize->600,Frame->True,Axes->False];
Show[stateplot,potplot],{{V,100},1,100,ControlType->Slider,Appearance->"Labeled"},LabelStyle->Large, SaveDefinitions->True]