Skip to content

polykin.math¤

roots_xcotx cached ¤

roots_xcotx(
    a: float, N: int, xtol: float = 1e-06
) -> FloatVector

Determine the first N roots of the transcendental equation 1 - x_n*cot(x_n) = a.

PARAMETER DESCRIPTION
a

Parameter.

TYPE: float

N

Number of roots.

TYPE: int

xtol

Tolerance.

TYPE: float DEFAULT: 1e-06

RETURNS DESCRIPTION
FloatVector

Roots of the equation.

Examples:

Determine the first 4 roots for a=1.

>>> from polykin.math import roots_xcotx
>>> roots_xcotx(2.0, 4)
array([ 2.0287575 ,  4.91318033,  7.97866578, 11.08553772])
Source code in src/polykin/math/special.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
@functools.cache
def roots_xcotx(a: float, N: int, xtol: float = 1e-6) -> FloatVector:
    r"""Determine the first `N` roots of the transcendental equation 
    `1 - x_n*cot(x_n) = a`.

    Parameters
    ----------
    a : float
        Parameter.
    N : int
        Number of roots.
    xtol : float
        Tolerance.

    Returns
    -------
    FloatVector
        Roots of the equation.

    Examples
    --------
    Determine the first 4 roots for a=1.
    >>> from polykin.math import roots_xcotx
    >>> roots_xcotx(2.0, 4)
    array([ 2.0287575 ,  4.91318033,  7.97866578, 11.08553772])
    """
    result = np.zeros(N)

    if a == 1:
        for i in range(N):
            result[i] = (i + 1/2)*pi
    elif a == inf:
        for i in range(N):
            result[i] = (i + 1)*pi
    elif a < 2:
        for i in range(N):
            sol = fzero_newton(f=lambda x: x/tan(x) + a - 1,
                               x0=(i + 1/2)*pi,
                               xtol=xtol,
                               ftol=1e-10)
            result[i] = sol.x
    else:
        for i in range(N):
            x0 = (i+1)*pi
            e = 0.0
            for _ in range(0, 30):
                e_new = np.arctan((x0 + e)/(1 - a))
                if abs(e_new - e) < xtol:
                    break
                e = e_new
            result[i] = x0 + e

    return result