Generate correct MATLAB code using the Symbolic Math Toolbox. Use when the user asks for symbolic computations, analytical solutions, symbolic differentiation/integration, equation solving, or converting symbolic results to numeric MATLAB functions. Also use when converting differential equations to transfer functions or state-space form, extracting PDE coefficients (pdeCoefficients), symbolic-to-C code generation (matlabFunction + codegen), symbolic matrix variables (symmatrix), physical units and constants (symunit, newUnit), or rewriting/combining algebraic expressions (rewrite, combine).
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):
solve('x^2 + 2*x - 3 = 0')
dsolve('Dy = -a*y')
CORRECT:
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 namedpi, NOT the mathematical constant π. This is a common source of confusion.
WRONG:
% 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:
% 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.
% 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:
syms x
f = x^2 + 3*x;
subs(f, x, 2); % Result is discarded!
disp(f) % Still x^2 + 3*x
CORRECT:
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.
% 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
% 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; ...]
% Symbolic vector
syms a [1 3] % Creates row vector a = [a1 a2 a3]
% Symbolic numbers (exact)
a = sym(1/3); % Exact 1/3
piSym = sym(pi); % Exact π
Solving Algebraic Equations
syms x y
% Single equation
sol = solve(x^2 - 5*x + 6 == 0, x); % Returns [2; 3]
% System of equations
[solx, soly] = solve(x + y == 10, x - y == 2, x, y);
% Return all solutions along with the parameters in the solution and the conditions on the solution
[sol, params, conds] = solve(sin(x) == 0, x, 'ReturnConditions', true);
% Numerical solutions when analytic not possible
solN = vpasolve(x^5 - 3*x^4 + x - 1 == 0, x);
Calculus
syms x t n
% Differentiation
diff(sin(x), x) % cos(x)
diff(x^3, x, 2) % 6*x (second derivative)
% Integration
int(x^2, x) % x^3/3 (indefinite)
int(x^2, x, 0, 1) % 1/3 (definite, from 0 to 1)
% Limits
limit(sin(x)/x, x, 0) % 1
limit(1/x, x, 0, 'right') % Inf
limit(1/x, x, 0, 'left') % -Inf
% Summation
symsum(1/n^2, n, 1, Inf) % pi^2/6
% Taylor series
taylor(exp(x), x, 0, 'Order', 6) % x^5/120 + x^4/24 + x^3/6 + x^2/2 + x + 1
% Gradient, Hessian, Jacobian, Laplacian, Divergence, Curl — use dedicated functions, not manual diff
syms x y z
f = x^2*y + y^3;
gradient(f, [x, y]) % [2*x*y; x^2 + 3*y^2]
hessian(f, [x, y]) % [2*y, 2*x; 2*x, 6*y]
laplacian(f, [x, y]) % 2*y + 6*y = 8*y (trace of Hessian)
g = [x^2*y; 5*x + sin(y)];
jacobian(g, [x, y]) % [2*x*y, x^2; 5, cos(y)]
V = [x^2*y; y^2*z; z^2*x];
divergence(V, [x, y, z]) % 2*x*y + 2*y*z + 2*z*x
curl(V, [x, y, z]) % [-y^2; -z^2; -x^2]
Matrix Operations
syms a b c d
A = [a b; c d];
% Determinant
det(A) % a*d - b*c
% Inverse
inv(A) % Symbolic inverse
% Eigenvalues and eigenvectors
[V, D] = eig(A)
% Characteristic polynomial
charpoly = det(A - sym('lambda')*eye(2))
% Jacobian of a coordinate change
syms r(t) phi(t) theta(t); % polar coordinates that are a function of time
R = [r*sin(phi)*cos(theta), r*sin(phi)*sin(theta), r*cos(phi)] % coordinate transform from spherical to Cartesian
jacobian(R,[r,phi,theta])
Application Patterns
For detailed workflows, see the reference files below. Read the relevant file when the user's task matches:
references/simplification-and-polynomials.md — simplify/expand/factor/collect/partfrac/rewrite, sym2poly vs coeffs, variable-precision arithmetic (VPA)
references/control-systems.md — Deriving transfer functions from ODEs, tf/ss derivation from first principles, Laplace/Fourier/Z-transform, Bode plots from symbolic models
references/plotting-and-display.md — fplot/fsurf/fmesh/fcontour/fimplicit/fanimator family, disp() vs pretty(), symbolic plotting best practices
references/matlabFunction-patterns.md — Converting symbolic expressions to function handles/files, 'Vars'/'Optimize'/'File' options, piecewise handling, critical error-prevention rules
references/extract-pde-coefficients.md — pdeCoefficients workflow for extracting PDE Toolbox coefficients, pdeCoefficientsToDouble, systems of PDEs
references/symbolic-to-c-code.md — matlabFunction(..., 'File', ...) + codegen pipeline for C/C++ generation, when to use ccode vs full pipeline
references/symmatrix-workflow.md — Atomic symbolic matrices with symmatrix, symmatrix2sym conversion, symfunmatrix, supported vs unsupported operations
references/symunit-workflow.md — Physical units/constants via symunit, newUnit/removeUnit, correct constant names (u.hc, u.c_0), unit conversion
references/rewrite-combine-expressions.md — Valid targets for rewrite and combine, IgnoreAnalyticConstraints for log combination, when to use which function
Common Mistakes and Fixes
Mistake
Fix
solve('x^2=1')
syms x; solve(x^2 == 1, x)
dsolve('Dy = y')
syms y(t); dsolve(diff(y,t) == y)
subs(f,x,2) without assigning
f = subs(f,x,2)
Attempting to clear assumptions with the clear command
syms x or assume(x,'clear') — only these reset the symbolic engine
Using syms inside a function
Use x = sym('x') inside functions
Manually extracting PDE coefficients by inspection
Computing gradient/Hessian/Jacobian/Laplacian/divergence/curl manually with diff
Use gradient, hessian, jacobian, laplacian, divergence, curl
Manually building numeric arrays to plot a symbolic expression
Use fplot(f, [a b]), fsurf, fmesh, fcontour, or fanimator — they accept symbolic directly
Adding 'Optimize' to matlabFunction for a function handle
For handles: matlabFunction(expr, 'Vars', {...}) — nothing else needed. 'Optimize' is only for 'File' output
Keeping everything symbolic for a parametric sweep
Convert to numeric: use matlabFunction then loop, or subs+double in a loop
See also: application-specific mistakes in each reference file.
Conventions for Modern Features
Use gradient, hessian, jacobian, laplacian, divergence, and curl when computing these standard vector calculus objects — do not manually assemble them from diff calls
Prefer pdeCoefficients() for extracting PDE Toolbox coefficients when the PDE is in a supported form
Prefer physical constants from symunit (e.g., u.hc, u.c_0) when available; hard-code only if the constant is not in the symunit catalog
Use symmatrix when the user asks for atomic/textbook-style matrices
Never pass an invalid target to combine or rewrite — consult the reference for valid targets
Checklist Before Generating Symbolic Code
Using syms (not string-based sym('...')) for variable creation in scripts
Using == for equations, not =
Using diff(y, t, n) for derivatives, not D notation
Using gradient, hessian, jacobian, laplacian, divergence, curl instead of manual diff when computing these objects
Specifying the independent variable explicitly in diff, int, laplace
Assigning subs(...) output to a variable
NOT wrapping numeric literals in sym() when a symbolic variable is already in the expression
Setting assumptions with assume/assumeAlso, clearing with syms x or assume(x,'clear')
Using fplot/fsurf/fanimator for symbolic plots (they accept symbolic expressions directly)
For parametric sweeps: converting to numeric (matlabFunction or subs+double in a loop)
Troubleshooting
Issue: solve returns empty or unexpected results
Check: Are there assumptions restricting the domain? Use assumptions to check.
Try: solve(eqn, x, 'ReturnConditions', true) to see conditions on solutions.
Try: vpasolve for numeric solutions when no closed form exists.
Issue: Stale assumptions causing wrong results
Fix: Add syms <varname> at the top of your script to clear assumptions.
See also: application-specific troubleshooting in each reference file.
Copyright 2026 The MathWorks, Inc.
1---2name: matlab-use-symbolic-math3description: Generate correct MATLAB code using the Symbolic Math Toolbox. Use when the user asks for symbolic computations, analytical solutions, symbolic differentiation/integration, equation solving, or converting symbolic results to numeric MATLAB functions. Also use when converting differential equations to transfer functions or state-space form, extracting PDE coefficients (pdeCoefficients), symbolic-to-C code generation (matlabFunction + codegen), symbolic matrix variables (symmatrix), physical units and constants (symunit, newUnit), or rewriting/combining algebraic expressions (rewrite, combine).4license: https://www.mathworks.com/content/dam/mathworks/license/pmrl/lic5---67# MATLAB Symbolic Math Toolbox89This skill provides guidelines, correct syntax, and common patterns for generating MATLAB® code that uses Symbolic Math Toolbox.1011## When to Use This Skill1213- Creating or manipulating symbolic variables, expressions, and functions14- Performing symbolic differentiation, integration, limits, or summation15- Simplifying, factoring, expanding, or collecting symbolic expressions16- Computing Laplace, Fourier, or Z-transforms and their inverses17- Deriving transfer functions or state-space equations from differential equations18- Displaying or plotting symbolic expressions19- Using variable precision arithmetic (VPA)20- Generating MATLAB functions, Simulink function blocks, Simscape equations, and C code from symbolic expressions21- Extracting PDE coefficients for use with PDE Toolbox22- Converting symbolic expressions to C code or standalone executables23- Matrix-level (atomic, textbook-style) symbolic linear algebra with `symmatrix`24- Using physical units or constants in symbolic computations with `symunit`25- Rewriting or combining algebraic expressions into specific forms2627## When NOT to Use This Skill2829- Purely numeric computation with no symbolic variables (use standard MATLAB numeric functions)30- Statistics, machine learning, or data analysis on numeric datasets31- Image processing, signal processing, or other toolbox-specific workflows that don't involve symbolic math32- String manipulation or file I/O operations33- When the user explicitly asks for numeric approximations only (use `double` or numeric solvers directly)34- PDE Toolbox mesh generation, boundary conditions, or solving (downstream of coefficient extraction)35- Numeric linear algebra (use standard MATLAB matrix operations)3637## Critical Rules3839### 1. NEVER Pass Strings or Character Vectors to Symbolic Functions4041**WRONG (deprecated — warns today, errors in a future release; the single `=` in `solve` errors now):**42```matlab43solve('x^2 + 2*x - 3 = 0')44dsolve('Dy = -a*y')45```4647**CORRECT:**48```matlab49syms x50solve(x^2 + 2*x - 3 == 0, x)5152syms y(t) a53dsolve(diff(y,t) == -a*y)54```5556### 2. Use `syms` for Interactive Work, `sym` for Functions and Constants5758- **`syms x y z`** — Creates fresh symbolic variables and clears any prior assumptions. Use for interactive scripts and Live Scripts.59- **`x = sym('x')`** — Refers to a symbolic variable. Inherits existing assumptions. Required inside MATLAB functions (not scripts) because `syms` dynamically creates workspace variables.60- **`sym(pi)`** — Converts numeric to exact symbolic. Use for symbolic constants.61- **`sym('pi')`** — Creates a symbolic *variable named* `pi`, NOT the mathematical constant π. This is a common source of confusion.6263**WRONG:**64```matlab65% Inside a function:66function result = myFunc()67 syms x % Error or unreliable in compiled/nested functions68 result = x^2;69end7071% Creating symbolic constant pi:72p = sym('pi'); % Creates variable named "pi", NOT the constant73```7475**CORRECT:**76```matlab77% Inside a function:78function result = myFunc()79 x = sym('x'); % Use sym inside functions80 result = x^2;81end8283% Creating symbolic constant pi:84p = sym(pi); % Converts numeric pi to exact symbolic π85```8687### 3. Assumption Management8889Assumptions persist in the symbolic engine even after `clear`. This is a frequent source of subtle bugs.9091```matlab92% Setting assumptions93syms x real % x is real (clears prior assumptions)94syms n positive integer % n is a positive integer95assume(x > 0) % x is positive (REPLACES all prior assumptions on x)96assumeAlso(x < 10) % ADDS assumption: 0 < x < 109798% Checking assumptions99assumptions(x) % Shows assumptions on x100assumptions % Shows ALL assumptions in workspace101102% Clearing assumptions — two correct ways:103syms x % Recreate with syms: clears assumptions104assume(x, 'clear') % Explicitly clear assumptions on x105reset(symengine) % Nuclear option: clears EVERYTHING106```107108**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.109110### 4. `subs` Does Not Modify In-Place111112The `subs` function returns a new expression. It does NOT modify the original.113114**WRONG:**115```matlab116syms x117f = x^2 + 3*x;118subs(f, x, 2); % Result is discarded!119disp(f) % Still x^2 + 3*x120```121122**CORRECT:**123```matlab124syms x125f = x^2 + 3*x;126f_val = subs(f, x, 2); % Assign the result127% or: f = subs(f, x, 2); % Overwrite f128```129130### 5. Do Not Wrap Numeric Literals in `sym()` Inside Symbolic Expressions131132AI tools frequently over-wrap every numeric literal in `sym()`.133When 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.134**When you DO need `sym()`:** Only when creating a standalone symbolic number with NO symbolic variables present in the expression.135136```matlab137% No symbolic variable involved — sym() IS needed:138half = sym(1/2); % Exact 1/2, not 0.5 double139half = sym(1)/2; % Exact 1/2, declaring sym(1) promotes all numeric literals to symbolic140piExact = sym(pi); % Exact π, not 3.14159...141142% Symbolic variable already present — sym() is NOT needed:143syms x144f = x/2 + 1/3; % Automatically exact: x/2 + 1/3145g = exp(-x^2/2) / sqrt(2*pi); % All literals promoted by x146```147148## Core Workflow Patterns149150### Creating Variables and Expressions151152```matlab153% Multiple variables at once154syms a b c155156% Variables with assumptions157syms a b c real158syms n positive integer159syms x160assume(x > 2)161162163% Symbolic matrices with auto-generated elements164syms A [3 3] % Creates A = [A1_1 A1_2 A1_3; ...]165166% Symbolic vector167syms a [1 3] % Creates row vector a = [a1 a2 a3]168169% Symbolic numbers (exact)170a = sym(1/3); % Exact 1/3171piSym = sym(pi); % Exact π172```173174### Solving Algebraic Equations175176```matlab177syms x y178179% Single equation180sol = solve(x^2 - 5*x + 6 == 0, x); % Returns [2; 3]181182% System of equations183[solx, soly] = solve(x + y == 10, x - y == 2, x, y);184185% Return all solutions along with the parameters in the solution and the conditions on the solution186[sol, params, conds] = solve(sin(x) == 0, x, 'ReturnConditions', true);187188% Numerical solutions when analytic not possible189solN = vpasolve(x^5 - 3*x^4 + x - 1 == 0, x);190```191192### Calculus193194```matlab195syms x t n196197% Differentiation198diff(sin(x), x) % cos(x)199diff(x^3, x, 2) % 6*x (second derivative)200201% Integration202int(x^2, x) % x^3/3 (indefinite)203int(x^2, x, 0, 1) % 1/3 (definite, from 0 to 1)204205% Limits206limit(sin(x)/x, x, 0) % 1207limit(1/x, x, 0, 'right') % Inf208limit(1/x, x, 0, 'left') % -Inf209210% Summation211symsum(1/n^2, n, 1, Inf) % pi^2/6212213% Taylor series214taylor(exp(x), x, 0, 'Order', 6) % x^5/120 + x^4/24 + x^3/6 + x^2/2 + x + 1 215216% Gradient, Hessian, Jacobian, Laplacian, Divergence, Curl — use dedicated functions, not manual diff217syms x y z218f = x^2*y + y^3;219gradient(f, [x, y]) % [2*x*y; x^2 + 3*y^2]220hessian(f, [x, y]) % [2*y, 2*x; 2*x, 6*y]221laplacian(f, [x, y]) % 2*y + 6*y = 8*y (trace of Hessian)222g = [x^2*y; 5*x + sin(y)];223jacobian(g, [x, y]) % [2*x*y, x^2; 5, cos(y)]224V = [x^2*y; y^2*z; z^2*x];225divergence(V, [x, y, z]) % 2*x*y + 2*y*z + 2*z*x226curl(V, [x, y, z]) % [-y^2; -z^2; -x^2]227```228229### Matrix Operations230231```matlab232syms a b c d233A = [a b; c d];234235% Determinant236det(A) % a*d - b*c237238% Inverse239inv(A) % Symbolic inverse240241% Eigenvalues and eigenvectors242[V, D] = eig(A)243244% Characteristic polynomial245charpoly = det(A - sym('lambda')*eye(2))246247% Jacobian of a coordinate change248syms r(t) phi(t) theta(t); % polar coordinates that are a function of time249R = [r*sin(phi)*cos(theta), r*sin(phi)*sin(theta), r*cos(phi)] % coordinate transform from spherical to Cartesian250jacobian(R,[r,phi,theta])251```252253## Application Patterns254255For detailed workflows, see the reference files below. Read the relevant file when the user's task matches:256257- **`references/simplification-and-polynomials.md`** — `simplify`/`expand`/`factor`/`collect`/`partfrac`/`rewrite`, `sym2poly` vs `coeffs`, variable-precision arithmetic (VPA)258- **`references/control-systems.md`** — Deriving transfer functions from ODEs, `tf`/`ss` derivation from first principles, Laplace/Fourier/Z-transform, Bode plots from symbolic models259- **`references/ode-solving.md`** — `dsolve` syntax, `odeToVectorField` + `matlabFunction` + `ode45` pipeline, parameterized ODE solving260- **`references/plotting-and-display.md`** — `fplot`/`fsurf`/`fmesh`/`fcontour`/`fimplicit`/`fanimator` family, `disp()` vs `pretty()`, symbolic plotting best practices261- **`references/matlabFunction-patterns.md`** — Converting symbolic expressions to function handles/files, `'Vars'`/`'Optimize'`/`'File'` options, piecewise handling, critical error-prevention rules262- **`references/extract-pde-coefficients.md`** — `pdeCoefficients` workflow for extracting PDE Toolbox coefficients, `pdeCoefficientsToDouble`, systems of PDEs263- **`references/symbolic-to-c-code.md`** — `matlabFunction(..., 'File', ...)` + `codegen` pipeline for C/C++ generation, when to use `ccode` vs full pipeline264- **`references/symmatrix-workflow.md`** — Atomic symbolic matrices with `symmatrix`, `symmatrix2sym` conversion, `symfunmatrix`, supported vs unsupported operations265- **`references/symunit-workflow.md`** — Physical units/constants via `symunit`, `newUnit`/`removeUnit`, correct constant names (`u.hc`, `u.c_0`), unit conversion266- **`references/rewrite-combine-expressions.md`** — Valid targets for `rewrite` and `combine`, `IgnoreAnalyticConstraints` for log combination, when to use which function267268## Common Mistakes and Fixes269270| Mistake | Fix |271|---------|-----|272| `solve('x^2=1')` | `syms x; solve(x^2 == 1, x)` |273| `dsolve('Dy = y')` | `syms y(t); dsolve(diff(y,t) == y)` |274| `subs(f,x,2)` without assigning | `f = subs(f,x,2)` |275| Attempting to clear assumptions with the `clear` command | `syms x` or `assume(x,'clear')` — only these reset the symbolic engine |276| Using `syms` inside a function | Use `x = sym('x')` inside functions |277| Manually extracting PDE coefficients by inspection | Use `pdeCoefficients(pdeeq, u)` |278| Computing Laplacian as sum of second derivatives | Use `laplacian(f, vars)` |279| Using `ccode()` and manually building C files | Use `matlabFunction(..., 'File', ...)` then `codegen` |280| Using `sym('A', [n n])` for matrix-level algebra | Use `symmatrix('A', [n n])` |281| Hard-coding physical constants as numbers | Use `u = symunit; u.hc; u.c_0; u.m_e` |282| Using `combine(expr, 'power')` | Invalid target — valid: `'atan'`, `'exp'`, `'gamma'`, `'int'`, `'log'`, `'sincos'`, `'sinhcosh'` |283| Computing gradient/Hessian/Jacobian/Laplacian/divergence/curl manually with `diff` | Use `gradient`, `hessian`, `jacobian`, `laplacian`, `divergence`, `curl` |284| Manually building numeric arrays to plot a symbolic expression | Use `fplot(f, [a b])`, `fsurf`, `fmesh`, `fcontour`, or `fanimator` — they accept symbolic directly |285| Adding `'Optimize'` to `matlabFunction` for a function handle | For handles: `matlabFunction(expr, 'Vars', {...})` — nothing else needed. `'Optimize'` is only for `'File'` output |286| Keeping everything symbolic for a parametric sweep | Convert to numeric: use `matlabFunction` then loop, or `subs`+`double` in a loop |287288See also: application-specific mistakes in each reference file.289290## Conventions for Modern Features291292- Use `gradient`, `hessian`, `jacobian`, `laplacian`, `divergence`, and `curl` when computing these standard vector calculus objects — do not manually assemble them from `diff` calls293- Prefer `pdeCoefficients()` for extracting PDE Toolbox coefficients when the PDE is in a supported form294- Prefer physical constants from `symunit` (e.g., `u.hc`, `u.c_0`) when available; hard-code only if the constant is not in the symunit catalog295- Use `symmatrix` when the user asks for atomic/textbook-style matrices296- Never pass an invalid target to `combine` or `rewrite` — consult the reference for valid targets297298## Checklist Before Generating Symbolic Code299300- [ ] Using `syms` (not string-based `sym('...')`) for variable creation in scripts301- [ ] Using `==` for equations, not `=`302- [ ] Using `diff(y, t, n)` for derivatives, not `D` notation303- [ ] Using `gradient`, `hessian`, `jacobian`, `laplacian`, `divergence`, `curl` instead of manual `diff` when computing these objects304- [ ] Specifying the independent variable explicitly in `diff`, `int`, `laplace`305- [ ] Assigning `subs(...)` output to a variable306- [ ] NOT wrapping numeric literals in `sym()` when a symbolic variable is already in the expression307- [ ] Setting assumptions with `assume`/`assumeAlso`, clearing with `syms x` or `assume(x,'clear')`308- [ ] Using `fplot`/`fsurf`/`fanimator` for symbolic plots (they accept symbolic expressions directly)309- [ ] For parametric sweeps: converting to numeric (`matlabFunction` or `subs`+`double` in a loop)310311## Troubleshooting312313**Issue**: `solve` returns empty or unexpected results314- **Check**: Are there assumptions restricting the domain? Use `assumptions` to check.315- **Try**: `solve(eqn, x, 'ReturnConditions', true)` to see conditions on solutions.316- **Try**: `vpasolve` for numeric solutions when no closed form exists.317318**Issue**: Stale assumptions causing wrong results319- **Fix**: Add `syms <varname>` at the top of your script to clear assumptions.320- **Nuclear option**: `reset(symengine)` clears everything.321322See also: application-specific troubleshooting in each reference file.323324----325326Copyright 2026 The MathWorks, Inc.
Run npx skillmds@latest add gabrielmoreira/matlab-use-symbolic-math in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Generate correct MATLAB code using the Symbolic Math Toolbox. Use when the user asks for symbolic computations, analytical solutions, symbolic differentiation/integration, equation solving, or converting symbolic results to numeric MATLAB functions. Also use when converting differential equations to transfer functions or state-space form, extracting PDE coefficients (pdeCoefficients), symbolic-to-C code generation (matlabFunction + codegen), symbolic matrix variables (symmatrix), physical units and constants (symunit, newUnit), or rewriting/combining algebraic expressions (rewrite, combine). It is listed under Integrations & APIs on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. Capability flags: makes network calls. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
gabrielmoreira (@gabrielmoreira) published this skill. Their other Agent Skills are listed on their SkillMD profile.