PoliSpectralTools – Usage Examples

Expanded descriptions for the five showcase cases (slides referenced from class_slides/).

1. Chebyshev Linear BVP

Slides: PE_Aula_05_N.pdf (pp. 2–5)

Solve -(1+x) y'' = sin(πx) with homogeneous Dirichlet data using Chebyshev Lobatto collocation. We scale the second derivative matrix and impose BCs via the boundary utilities.

a(x) = -(1 + x)
b(x) = 0; c(x) = 0; rhs(x) = sinpi(x)
sol = solve_linear_bvp(a, b, c, rhs; N = 48, basis = :chebyshev)
Chebyshev solution

Max norm difference versus an N=192 Chebyshev reference: 9.8e-14.

2. Diffusion Decay

Slides: PE_Aula_06_N.pdf (pp. 2–3)

Heat equation with analytic solution exp(-π² t / 4) sin(π(x+1)/2) solved via MOL + RK4 (N=48 Chebyshev nodes, dt=1e-5).

diff = solve_diffusion_1d(u_exact(_, 0.0), (0.0, 0.05);
    N = 48, dt = 1e-5, bc = (left = (:dirichlet, 0.0), right = (:dirichlet, 0.0)))
Diffusion decay Diffusion decay animation

Error at t = 0.05: 3.3e-15 (L∞ norm).

3. Legendre vs. Chebyshev

Slides: PE_Aula_07_N.pdf, PE_Aula_08_N.pdf

Repeat the variable-diffusivity BVP on Legendre Lobatto nodes and compare with Chebyshev references via barycentric interpolation.

leg = solve_linear_bvp(...; basis = :legendre, N = 40)
cheb = solve_linear_bvp(...; basis = :chebyshev, N = 96)
vals, _ = Generic.Bary_Interp(cheb.x, cheb.u, leg.x)
Legendre error plot
N (Legendre)max |u_leg − u_cheb|
126.09×10⁻¹⁰
201.29×10⁻¹⁴
281.41×10⁻¹⁴
361.92×10⁻¹⁴

4. Nonlinear BVP (y'' = sin y)

Slides: PE_Aula_06_N.pdf (Newton flow)

Newton iteration with analytic Jacobian on Chebyshev nodes (N=80). Starting from zero, the solver converges in a single iteration.

g(x,y,yp) = sin(y)
sol = solve_nonlinear_bvp(g; dg_dy = (x,y,yp)->cos(y), N = 80)
Nonlinear BVP solution

Residual norm < 1e-13, iterations: 1.

5. Wave Equation with Mixed BCs

Slides: PE_Aula_10_N.pdf

Leapfrog integration on Chebyshev nodes (N=40, dt=5e-4), Neumann flux on the left boundary and Dirichlet clamp on the right. Energy drift reflects work injected by the Neumann boundary.

bc = (left = (:neumann, (x,t)->cos(5t)), right = (:dirichlet, 0.0))
wave = solve_wave_1d(u0, v0, (0.0, 0.2); N = 40, dt = 5e-4, bc = bc)
Wave snapshot Wave animation

Energy drift over t∈[0,0.2]: 17.9%; boundary flux error < 5×10⁻³.

File Map

ExampleScriptAssets
1examples/bvp_linear.jlbvp_linear_solution.png
2examples/diffusion.jldiffusion_decay.png / .gif
3examples/bvp_legendre.jlbvp_legendre_error.png
4examples/bvp_nonlinear.jlbvp_nonlinear_solution.png
5examples/wave_mixed_bc.jlwave_mode.png / .gif