matlab-use-symbolic-math
>
git clone --depth 1 https://github.com/matlab/matlab-agentic-toolkit /tmp/matlab-use-symbolic-math && cp -r /tmp/matlab-use-symbolic-math/skills-catalog/math-and-optimization/matlab-use-symbolic-math ~/.claude/skills/matlab-use-symbolic-mathSKILL.md
# MATLAB Symbolic Math Toolbox
This skill provides guidelines, correct syntax, and common patterns for generating MATLAB® code that uses Symbolic Math Toolbox.
## When to Use This Skill
- Creating or manipulating symbolic variables, expressions, and functions
- Performing symbolic differentiation, integration, limits, or summation
- Simplifying, factoring, expanding, or collecting symbolic expressions
- Computing Laplace, Fourier, or Z-transforms and their inverses
- Deriving transfer functions or state-space equations from differential equations
- Displaying or plotting symbolic expressions
- Using variable precision arithmetic (VPA)
- Generating MATLAB functions, Simulink function blocks, Simscape equations, and C code from symbolic expressions
- Extracting PDE coefficients for use with PDE Toolbox
- Converting symbolic expressions to C code or standalone executables
- Matrix-level (atomic, textbook-style) symbolic linear algebra with `symmatrix`
- Using physical units or constants in symbolic computations with `symunit`
- Rewriting or combining algebraic expressions into specific forms
## When NOT to Use This Skill
- Purely numeric computation with no symbolic variables (use standard MATLAB numeric functions)
- Statistics, machine learning, or data analysis on numeric datasets
- Image processing, signal processing, or other toolbox-specific workflows that don't involve symbolic math
- String manipulation or file I/O operations
- When the user explicitly asks for numeric approximations only (use `double` or numeric solvers directly)
- PDE Toolbox mesh generation, boundary conditions, or solving (downstream of coefficient extraction)
- Numeric linear algebra (use standard MATLAB matrix operations)
## Critical Rules
### 1. NEVER Pass Strings or Character Vectors to Symbolic Functions
**WRONG (deprecated — warns today, errors in a future release; the single `=` in `solve` errors now):**
```matlab
solve('x^2 + 2*x - 3 = 0')
dsolve('Dy = -a*y')
```
**CORRECT:**
```matlab
syms x
solve(x^2 + 2*x - 3 == 0, x)
syms y(t) a
dsolve(diff(y,t) == -a*y)
```
### 2. Use `syms` for Interactive Work, `sym` for Functions and Constants
- **`syms x y z`** — Creates fresh symbolic variables and clears any prior assumptions. Use for interactive scripts and Live Scripts.
- **`x = sym('x')`** — Refers to a symbolic variable. Inherits existing assumptions. Required inside MATLAB functions (not scripts) because `syms` dynamically creates workspace variables.
- **`sym(pi)`** — Converts numeric to exact symbolic. Use for symbolic constants.
- **`sym('pi')`** — Creates a symbolic *variable named* `pi`, NOT the mathematical constant π. This is a common source of confusion.
**WRONG:**
```matlab
% Inside a function:
function result = myFunc()
syms x % Error or unreliable in compiled/nested functions
result = x^2;
end
% Creating symbolic constant pi:
p = sym('pi'); % Creates variable named "pi", NOT the constant
```
**CORRECT:**
```matlab
% Inside a function:
function result = myFunc()
x = sym('x'); % Use sym inside functions
result = x^2;
end
% Creating symbolic constant pi:
p = sym(pi); % Converts numeric pi to exact symbolic π
```
### 3. Assumption Management
Assumptions persist in the symbolic engine even after `clear`. This is a frequent source of subtle bugs.
```matlab
% Setting assumptions
syms x real % x is real (clears prior assumptions)
syms n positive integer % n is a positive integer
assume(x > 0) % x is positive (REPLACES all prior assumptions on x)
assumeAlso(x < 10) % ADDS assumption: 0 < x < 10
% Checking assumptions
assumptions(x) % Shows assumptions on x
assumptions % Shows ALL assumptions in workspace
% Clearing assumptions — two correct ways:
syms x % Recreate with syms: clears assumptions
assume(x, 'clear') % Explicitly clear assumptions on x
reset(symengine) % Nuclear option: clears EVERYTHING
```
**Best Practice:** Use `syms x` to clear assumptions (it resets the variable fresh). Use `assume(x, 'clear')` when you need to reset a specific variable mid-script. The MATLAB `clear` command only removes workspace variables — it has no effect on the symbolic engine's assumption store.
### 4. `subs` Does Not Modify In-Place
The `subs` function returns a new expression. It does NOT modify the original.
**WRONG:**
```matlab
syms x
f = x^2 + 3*x;
subs(f, x, 2); % Result is discarded!
disp(f) % Still x^2 + 3*x
```
**CORRECT:**
```matlab
syms x
f = x^2 + 3*x;
f_val = subs(f, x, 2); % Assign the result
% or: f = subs(f, x, 2); % Overwrite f
```
### 5. Do Not Wrap Numeric Literals in `sym()` Inside Symbolic Expressions
AI tools frequently over-wrap every numeric literal in `sym()`.
When any operand in an arithmetic expression is symbolic, MATLAB automatically promotes all numeric literals in that expression to symbolic. Wrapping literals in `sym()` adds clutter and can cause errors.
**When you DO need `sym()`:** Only when creating a standalone symbolic number with NO symbolic variables present in the expression.
```matlab
% No symbolic variable involved — sym() IS needed:
half = sym(1/2); % Exact 1/2, not 0.5 double
half = sym(1)/2; % Exact 1/2, declaring sym(1) promotes all numeric literals to symbolic
piExact = sym(pi); % Exact π, not 3.14159...
% Symbolic variable already present — sym() is NOT needed:
syms x
f = x/2 + 1/3; % Automatically exact: x/2 + 1/3
g = exp(-x^2/2) / sqrt(2*pi); % All literals promoted by x
```
## Core Workflow Patterns
### Creating Variables and Expressions
```matlab
% Multiple variables at once
syms a b c
% Variables with assumptions
syms a b c real
syms n positive integer
syms x
assume(x > 2)
% Symbolic matrices with auto-generated elements
syms A [3 3] % Creates A = [A1_1 A1_2 A1_3; ...>
Import recorded driving sensor data (GPS, camera, lidar, actor tracks, lanes) into scenariobuilder.* objects (GPSData, CameraData, LidarData, ActorTrackData, Trajectory, laneData) and run preprocessing — synchronize, offset correction, crop, normalizeTimestamps, convertTimestamps. Also: compute actor tracks from lidar when no annotations exist, attach camera/lidar mounting + intrinsics, export to MAT/workspace/timetable/script. Use for raw driving dataset files (KITTI, nuScenes, Waymo, Pandaset, ROS/ROS2 bags, .mat, .csv, .mp4) or driving/vehicle/sensor logs that need wrapping. drivingLogAnalyzer (DLA) is OPT-IN ONLY — invoke only on explicit user request ('DLA', 'open in DLA', 'inspect/explore/analyze the recording') or reported sensor problem (sync drift, timestamp mismatch, overlay misalignment). NEVER auto-launch DLA after wrapping (Rule 0). For 'build scenario / export to RoadRunner / drivingScenario / OpenSCENARIO / Unreal / simulate', hand off to matlab-scenario-builder.
Generate driving scenes, scenarios, road surfaces, and 3D content from already-wrapped scenariobuilder.* sensor data (GPS, camera, lidar, actor tracks) using Scenario Builder for Automated Driving Toolbox. Use to BUILD, EXPORT, or AUGMENT a virtual scenario/scene/map: ego or actor trajectories, trajectory smoothing, OpenCRG road-surface extraction, 3D asset generation, static-object placement, point-cloud georeferencing + elevation, lane-based ego localization, sensor-fusion tracking, scenario-event extraction (cut-ins, hard brakes, near-misses, ADAS disengagements), or export to RoadRunner, drivingScenario, OpenDRIVE, OpenCRG, OpenSCENARIO, or Unreal Engine. Also: log-to-scenario, scenario harvesting, accident/near-miss reconstruction, SOTIF (ISO 21448) and ISO 26262 scenario coverage, USGS-aerial-lidar scene augmentation, traffic-sign placement from camera+lidar logs. NOT for raw-data import or multi-sensor sync/crop/offset/timestamp normalization — route those to matlab-driving-data-importer.
>
>
>
>
Build, modify, and diagram SimBiology models — API reference, helper functions, and layout patterns. Use when constructing or editing models programmatically or visually.