harmonic

Harmonic numbers H_n = 1 + 1/2 + ... + 1/n.

Category: Mathematics > Number Theory

Syntax

H = harmonic(n)

Returns the n-th harmonic number.

Description

The harmonic number is the partial sum of the harmonic series. It grows without bound but very slowly, like log(n) + 0.5772..., where the constant is the Euler-Mascheroni constant. harmonic(0) is 0.

With a symbolic integer, for example sym(10), the result is the exact fraction 7381/2520.

Input arguments

n
Integer scalar or array, double or symbolic.

Output arguments

H
Harmonic numbers, with the size of n. Exact fractions for symbolic input.

Example

Numeric and exact

harmonic(1:5)
harmonic(sym(10))
harmonic(100)
Output
ans = 1×5

    1.0000    1.5000    1.8333    2.0833    2.2833
ans = 7381/2520
ans = 5.1874

Notes

Only integer arguments are accepted. MATLAB also defines harmonic for non-integer and symbolic non-integer arguments, which MatLivre rejects with an error.

See also

zeta bernoulli cumsum sum