matlab-symbolic-math
This skill generates correct MATLAB Symbolic Math Toolbox code for creating and manipulating symbolic variables, performing symbolic differentiation and integration, simplifying expressions, solving equations analytically, computing transforms, deriving transfer functions from differential equations, and generating MATLAB functions or C code from symbolic results. Use it when users request symbolic computations, analytical mathematical solutions, or conversion of symbolic expressions to numeric or code formats.
git clone --depth 1 https://github.com/matlab/agent-skills-playground /tmp/matlab-symbolic-math && cp -r /tmp/matlab-symbolic-math/skills/matlab-symbolic-math ~/.claude/skills/matlab-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
## 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 — THREE ways (know the differences):
syms x % Recreate with syms: clears assumptions
assume(x, 'clear') % Explicitly clear assumptions on x
reset(symengine) % Nuclear option: clears EVERYTHING
% DANGER: clear x does NOT clear assumptions!
clear x % Removes variable from workspace
x = sym('x'); % x INHERITS old assumptions from engine!
```
**Best Practice:** Use `syms` to create variables at the start of a script. This clears stale assumptions. Use `assume(x, 'clear')` when you need to reset a specific variable mid-script.
### 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
```
### 6. Variable Naming: Symbolic-to-Numeric Conversions
When substituting numeric values or converting symbolic expressions to numeric form, keep the base variable name and append a suffix indicating the conversion type:
- **`Val`** — after `subs()` or `double()` (numeric value)
- **`Vpa`** — after `vpa()` (variable-precision arithmetic)
```matlab
syms m g L
% Substituting numeric values
mVal = double(subs(m, 5)); % or: mVal = 5;
gVal = 9.81;
LVal = 0.5;
% Evaluating a symbolic expression numerically
omega = sqrt(g/L);
omegaVal = double(subs(omega, [g L], [gVal LVal]));
% Variable-precision arithmetic
piVpa = vpa(sym(pi), 50);
omegaVpa = vpa(subs(omega, [g L], [gVal LVal]), 32);
```
**Rationale:** This convention keeps symbolic and numeric variables visually distinct in the workspace, avoids accidentally overwriting a symbolic expression with a numeric value, and makes it clear at a glance which variables are exact symbolic vs. evaluated numeric.
## 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)
% SymbolicUse this skill when the user wants to author, design, scope, or refine an Agent Skill (a SKILL.md file). Trigger phrases include "build a new skill", "design an agent skill", "scope a SKILL.md", "how should I structure this skill", "write a skill for X", "my skill isn't working well", or any request to improve an existing SKILL.md. Walks the user through an empirical, test-first process — probe the agent for real failures, design only for genuine knowledge gaps, iterate against runnable examples, and verify across models.
>
Use when a learner asks for help with MATLAB homework, labs, projects, graded assignments, take-home exams, quizzes, or any programming task where academic integrity, course policy, or instructor constraints may limit direct solutions. Use to provide policy-aware hints, conceptual coaching, partial feedback, and assignment-safe MATLAB tutoring.
Use when tutoring a learner through MATLAB debugging, error interpretation, failed tests, incorrect outputs, array-shape problems, indexing mistakes, function argument issues, or code repair practice. Use for guided debugging sessions, debugging drills, teach-the-agent critique, and evidence-based MATLAB troubleshooting.
Use when an AI tutor session concerns MATLAB programming concepts, MATLAB syntax, MATLAB errors, MATLAB code style, MATLAB projects, or MATLAB toolbox workflows.
Use when an instructor wants to create, interview for, configure, install, update, or review a course AI-use policy for MATLAB AI tutoring. Produces an AI-POLICY.md file for LMS sharing and local tutoring-session enforcement by assignment guardrails.
Create MATLAB Course Designer MATLAB Exercise learning activities by wrapping the existing matlab-generate-grader-assessments skill, then validating generated solution.m, template.m, and tests.m files with MATLAB MCP Server tool calls. Use when the user asks to create a MATLAB Exercise, MATLAB course activity, Course Designer MATLAB activity, validated MATLAB solution file, or MATLAB Exercise component folder.
Use when prompting a learner to complete hands-on MATLAB coding exercises, guided practice, debugging drills, code tracing, small MATLAB projects, or MATLAB-script assessment during tutoring. Use when the tutor should create a complete runnable MATLAB script, execute it through MATLAB tools, compare the produced outputs with expected outputs, and evaluate MATLAB programming style.