Qu

chebdiff

Builtin index · Signal processing

result = chebdiff(n)

Input: n · Output: result

What it is

Chebyshev spectral differentiation (the cheb of Trefethen's *Spectral Methods in MATLAB*). n: the number of points, a whole number from 2 to 3000.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = chebdiff(n)
n numbernot documented yet
1/n —about 1e-13 for exp at n = 16
Returns a record {D, x}: x is the vector of the n Chebyshev-Gauss-Lobatto nodes cos(j*pi/(n-1)), j = 0..n-1, in descending order from 1 to -1, and D the dense n-by-n matrix such that D * f(x) is the derivative of the degree-n-1 interpolant at the nodes (D * (D * u) is the second derivative). The error for a smooth function falls faster than any power of 1/n (about 1e-13 for exp at n = 16); for a rescaled interval [a, b] use xs = (b - a)/2 * (x + 1) + a and multiply D by 2/(b - a). The diagonal is the negative row sum, so a constant differentiates to exactly zero. Errors: n < 2, or n not a whole number or above 3000.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

# y'' + y = 0, y(0) = 0, y(pi/2) = 1 (y = sin x), as the system y' = [y1, -y0]

function osc(x, y)
    return [y[1], -y[0]]
end function
function ends(ya, yb)
    return [ya[0], yb[0] - 1]
end function
b = bvp(osc, ends, linspace(0, pi/2, 11), zeros(2, 11))
print("{b.status} on {b.nodes} nodes")                  # converged on 41 nodes

print(bvp_eval(b, pi/4)[0] - sin(pi/4))                 # -1.83e-09


c = chebdiff(16)
print(max(abs(c.D * exp(c.x) - exp(c.x))))              # 5.8e-14, spectral accuracy

print(integral2((x, y) := 1, -1, 1, (x) := -sqrt(1 - x^2), (x) := sqrt(1 - x^2)))   # 3.141593, the unit disc

r = fminbnd((x) := cos(x), 3, 4)
print("{r.params} {r.converged}")                       # 3.141593 true

output
converged on 41 nodes
-1.834181e-9
5.817569e-14
3.141593
3.141593 true

from signal-processing