Symbolic math is a powerful tool when you need exact expressions instead of approximate numerics. It’s especially valuable for:
-
high-precision modeling (no rounding until you decide)
-
automatic algebraic simplification
-
differentiation/integration without manual derivations
-
generating clean formulas for documentation (LaTeX) or code generation
This example demonstrates a typical engineering workflow: deriving cubic beam shape functions and computing an exact stiffness-related integral symbolically, then evaluating the shape functions numerically over a grid.
What this example does
-
Loads the symbolic engine (
sym.load()) -
Defines symbolic variables (
le, x, E, Iz) -
Builds a polynomial basis and boundary-condition matrix
P -
Computes shape functions:
N = p * inv(P) -
Computes second derivatives:
d2N = diff(N, x, 2) -
Computes an internal stiffness term via an exact integral.
-
Substitutes
le = 1and evaluatesN(x)overxi = 0..1 -
Displays symbolic results as LaTeX
JavaScript Example
// Symbolic Math
var le, x, E, Iz;
var p, P, invP, N, d2N;
var k_int;
var xi = range(0, 1, 0.01);
await sym.load();
[le, x, E, Iz] = sym.syms(['le', 'x', 'E', 'Iz']);
P = sym.mat([
[1, 0, 0, 0],
[0, 1, 0, 0],
[1, le, sym.pow(le, 2), sym.pow(le, 3)],
[0, 1, sym.mul(2, le), sym.mul(3, sym.pow(le, 2))]
]);
p = sym.mat([[1, x, sym.pow(x, 2), sym.pow(x, 3)]]);
invP = sym.inv(P);
N = sym.mul(p, invP);
d2N = sym.diff(N, 'x', 2);
k_int = sym.mul(
E, Iz,
sym.intg(sym.mul(sym.transp(d2N), d2N), x, [0, le])
);
// Evaluate N(x) numerically for le=1 on xi grid
Ni = sym.subs(sym.subs(N, le, 1), x, xi).toNumeric();
var N_flat = Ni.flat();
// Show results as LaTeX
sym.showLatex(N);
sym.showLatex(k_int);
Practical uses
-
Finite element derivations: shape functions, stiffness/mass matrices, exact integrals
-
Control and estimation: symbolic Jacobians/Hessians for nonlinear models
-
Education & documentation: show derivations as LaTeX directly from code
-
Verification: compare numeric implementations against exact symbolic ground truth
