Quick guide to Mathematica

To get help, place the cursor on the function and press F1
To run all commands in one cell, place the cursor there and press Shift + Enter or Enter on the numerical keyboard.
Output of many commands is suppressed by ; at the end of the command. If you want to see the output, delete ;
Argument of functions must be in square brackets [...], braces {...} are for arrays, ranges etc., use double brackets [[...]] to access elements of arrays
expr /. {x -> a} means substitute a for x in expr, expr // function means apply function to expr, i.e. it is equivalent to function[expr].
D[f[x],x] = derivative of f[x] with respect to x.
To define a function of x one can use f[x_] := 1+(x^2), notice the underscore and the colon, or # nad & like in f := 1+((#)^2) &
N[expr] evaluates expr with machine precision, if you use the decimal point in the expression it will be also evaluated with machine precision
Special characters can be inserted by pressing Esc ... Esc, e.g. Esc p Esc gives π
Clear[...] is used to unset any variables which could have been assigned previously.
Some other useful commands: Simplify, Expand, Factor; Integrate, Series, Sum

Clear all symbols from previous evaluations to avoid problems

Clear["Global`*"]

Jacobi diagonalization of real symmetric matrix

Run Basic.Functions.for.TMF057.m first !

Exact energies and eigenfunctions of 1D harmonic oscillator

n=6;
a=4;
μ=1;ω=1; 
V[x_]=(1/2)μ (ω^2)(x^2);
En[i_]=ω(i+(1/2));
ψ[i_,x_]=(1/(Sqrt[(2^i)i!]))(((μ ω)/π)^(1/4))Exp[-((μ ω (x^2))/2)]HermiteH[i,x];
Plot[{V[x],Table[En[i],{i,0,n-1}],Table[ψ[i,x]+En[i],{i,0,n-1}]},{x,-a,a},PlotRange->{0,6}]

Construction of the Hamiltonian matrix in the sine basis

n=10;
Clear[H];
ϕ[i_,x_]=Sin[2π(x+a)i/(4a)]/Sqrt[a];
KE[i_,j_]=If[i==j,(1/(2μ))(((((i Pi)/(2a))))^2),0];
PE[i_,j_]=If[i==j,-(a^2)((1/((i^2) (π^2)))-(1/6)),If[Mod[i+j,2]==1,0,((4(a^2))/(π^2)) ((1/(((i-j))^2))-(1/(((i+j))^2)))]];
H=ConstantArray[0, {n,n}];
Do[
Do[
H[[i,j]]=KE[i,j]+PE[i,j](* Integrate[-ϕ[i,x]D[ϕ[j,x],{x,2}]/(2m)(* +ϕ[i,x]V[x]ϕ[j,x]*),{x,-a,a}]*),
{j,1,n}
],
{i,1,n}
];
(* H//MatrixForm *)

Diagonalization using the Jacobi method:

{Energies,Coefficients,Sums}=MyJacobiDiagonalization[N[H],4];
Print["Energies:"]
Energies
Print["Coefficients:"]
Coefficients//MatrixForm
Print["Sums of off-diagonal elements after each sweep:"]
Sums

index=Ordering[Energies];
m=8;
Plot[{V[x],Table[En[i],{i,0,m-1}],Table[Abs[ψ[i,x]]^2+En[i],{i,0,m-1}],Table[Energies[[index[[i]]]],{i,1,m}],Table[Abs[Sum[Coefficients[[j,index[[i]]]]ϕ[j,x],{j,1,n}]]^2+Energies[[index[[i]]]],{i,1,m}]},{x,-a,a},PlotRange->{0,7}]