←to practical programming

Exercise "func"

Learn about closures and how to pass functions as parameters into a numerical integration routine.

In the folder matlib/integration[] there are several functions to perform numerical integration. In particular, the function quad.o8av attempts to deal with integrable singularities and infinite limits (in Csharp (positive) infinity is System.Double.PositiveInfinity). The general call to this routine is like this,

Func<double,double> f = (x) => Log(x)/Sqrt(x);
double a=0,b=1,result;
result=quad.o8av(f,a,b);
/* or */
result=quad.o8av(f,a,b,acc:1e-6,eps:1e-6);
where acc and eps are the absolute and relative accuracy requirements.

The function is not optimized in any way other than to be as simple as possible for the students to understand its workings.

  1. Calculate numerically (and check, of course) the following definite integrals,
    01 dx (ln(x)/√x) =-4,
    
    -∞ dx (exp(-x²)) = √(π),
    
    01 dx (ln(1/x))p = Γ(p+1),
    
    (calculate the last integral for several different values of the parameter p).
  2. Calculate numerically (and check) some of the definite integrals from [this table].
  3. In the variational method in quantum mechanics one calculates the expectation value E[ψ] of a Hamiltonian H,

    E[ψ] ≡ ⟨ψ|H|ψ⟩/⟨ψ|ψ⟩ ,
    
    (in Dirac notation) with some trial function ψ. The trial function is then optimized to provide the minimum of the expectation value.

    Consider the Hamiltonian operator for one-dimensional oscillator,

    Hos = - (1/2) (d²/dx²) + (1/2) x² ,
    
    and the trial function in the form of a Gaussian,
    ψα(x) = exp(-αx²/2) ,
    
    where α is the variational parameter. Calculate numerically the expectation value of the Hamiltonian,
    E(α) ≡ ⟨ψα|Hosα⟩/⟨ψαα⟩ .
    
    and plot E(α) in the interval, say, α∈[0.1,3]. You should see a minimum of E=0.5 at α=1 (why?).

    Hints:

    1. The norm-integral, ⟨ψαα⟩, has the form (check it)

      ⟨ψαα⟩ = -∞ exp(-αx²) dx .
      
      It is actually analytic, but you have to calculate it numerically.
    2. The Hamiltonian-integral, ⟨ψα|Hosα⟩, has the form (check it)

      ⟨ψα|Hosα⟩ = -∞ (-α²x²/2 + α/2 + x²/2)*exp(-αx²) dx .
      
      It is actually also analytic, but you again have to calculate it numerically.