Equana

Back to Tutorials

Polynomials

Fit, evaluate, and manipulate polynomials

Run AllReset

Polynomials appear everywhere in scientific computing — from curve fitting and interpolation to control systems and signal processing. Equana provides a complete set of polynomial operations that work on coefficient arrays.

Representing Polynomials

A polynomial is represented as an array of coefficients in low-to-high degree order. The first element is the constant term, the second is the linear coefficient, and so on:

Code [1]Run
# p(x) = 1 + 2x + 3x²
p = [1, 2, 3]
println(p)

# q(x) = 5 - x
q = [5, -1]
println(q)

Evaluating Polynomials

Use polyval to evaluate a polynomial at one or more points:

Code [2]Run
# p(x) = 1 + 2x + 3x²
p = [1, 2, 3]

# Evaluate at a single point
println(polyval(p, 2.0))   # 1 + 4 + 12 = 17.0

# Evaluate at multiple points
x = [0.0, 1.0, 2.0, 3.0]
println(polyval(p, x))     # [1, 6, 17, 34]

Polynomial Arithmetic

Add, subtract, multiply, and divide polynomials:

Code [3]Run
a = [1, 2, 3]   # 1 + 2x + 3x²
b = [1, 1]       # 1 + x

# Addition
println(polyadd(a, b))

# Subtraction
println(polysub(a, b))
Code [4]Run
a = [1, 2, 3]
b = [1, 1]

# Multiplication
println(polymul(a, b))   # (1+2x+3x²)(1+x) = 1+3x+5x²+3x³

# Division returns (quotient, remainder)
(q, r) = polydiv(a, b)
println("quotient: ", q)
println("remainder: ", r)

Derivatives and Integration

Compute derivatives with polyder and antiderivatives with polyint:

Code [5]Run
p = [1, 2, 3]   # 1 + 2x + 3x²

# First derivative: 2 + 6x
println(polyder(p))

# Second derivative: 6
println(polyder(p, 2))
Code [6]Run
p = [1, 2, 3]

# Antiderivative (constant of integration = 0)
println(polyint(p))    # [0, 1, 1, 1] → x + x² + x³

# Double integration
println(polyint(p, 2))

Curve Fitting

Use polyfit to find the best-fit polynomial through data points:

Code [7]Run
# Data points
x = [0.0, 1.0, 2.0, 3.0, 4.0]
y = [1.0, 3.0, 9.0, 19.0, 33.0]

# Fit a degree-2 polynomial: p(x) ≈ a₀ + a₁x + a₂x²
coeffs = polyfit(x, y, 2)
println(coeffs)

# Verify the fit
println(polyval(coeffs, x))

Polynomials from Roots

Construct a polynomial whose roots are a given set of values using poly:

Code [8]Run
# Polynomial with roots at x = 1, 2, 3
# (x-1)(x-2)(x-3) = -6 + 11x - 6x² + x³
p = poly([1.0, 2.0, 3.0])
println(p)

# Verify: should be ≈ 0 at each root
println(polyval(p, [1.0, 2.0, 3.0]))

Summary

FunctionDescriptionExample
polyval(p, x)Evaluate polynomialpolyval([1,2,3], 2.0)
polyadd(a, b)Add polynomialspolyadd([1,2], [3,4])
polysub(a, b)Subtract polynomialspolysub([1,2], [3,4])
polymul(a, b)Multiply polynomialspolymul([1,1], [1,1])
polydiv(a, b)Divide polynomialspolydiv([1,3,2], [1,1])
polyder(p)Derivativepolyder([1,2,3])
polyder(p, m)m-th derivativepolyder([1,2,3], 2)
polyint(p)Antiderivativepolyint([2,6])
polyint(p, m)m-th antiderivativepolyint([6], 2)
polyfit(x, y, deg)Least-squares fitpolyfit(x, y, 2)
poly(roots)From rootspoly([1.0, 2.0])

Workbench

Clear
No variables in workbench

Next Steps