Epsilon:0.1.0 − Numerical root-finding

epsilon is a package which allows numerical root finding directly in Typst. It currently supports the following methods:

  • Newton
  • bisection
  • secant

The following example shows how to use epsilon to solve a non-linear equation and plot the result using lilaq:

#import "@preview/lilaq:0.3.0" as lq
#import "@preview/epsilon:0.1.0": find-root

#{
  let prandtl-meyer-angle = M => (
    calc.sqrt((1.4 + 1) / (1.4 - 1))
      * calc.atan(calc.sqrt((1.4 - 1) / (1.4 + 1) * (calc.pow(M, 2) - 1)))
      - calc.atan(calc.sqrt(calc.pow(M, 2) - 1))
  )

  let angle = 0.736810609185532rad
  let prandtl-meyer-angle-root = M => prandtl-meyer-angle(M).rad() - angle.rad()
  let root = find-root(prandtl-meyer-angle-root, 1, x1: 4)

  let M = lq.linspace(1, 4)
  lq.diagram(
    width: 8cm,
    height: 5cm,
    title: [Calculating the Mach number for a given Prandtl-Meyer angle],
    xlabel: [Mach number $M$],
    ylabel: [Prandtl-Meyer angle $nu$ [°]],
    lq.plot(
      M, M.map(M => prandtl-meyer-angle(M).deg()), mark: none
    ),
    lq.plot(
      (root,),
      (angle.deg(),),
      mark-size: 6pt,
      color: red
    ),
    lq.hlines(angle.deg(), max: root, stroke: (dash: "dashed")),
    lq.vlines(root, max: angle.deg(), stroke: (dash: "dashed")),
  )
}

7 Likes

Awesome package! And congratulations on the creative name :)

1 Like