matlab/matlab-agentic-toolkit / skills-catalog/math-and-optimization/matlab-use-symbolic-math
matlab-use-symbolic-math
VerifiedGenerate 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).
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 14.5 KB |
| manifest.yaml | config | 523 B |
| references/control-systems.md | markdown | 5.1 KB |
| references/extract-pde-coefficients.md | markdown | 3.2 KB |
| references/matlabFunction-patterns.md | markdown | 6.4 KB |
| references/ode-solving.md | markdown | 3.2 KB |
| references/plotting-and-display.md | markdown | 3.6 KB |
| references/rewrite-combine-expressions.md | markdown | 4.7 KB |
| references/simplification-and-polynomials.md | markdown | 3.2 KB |
| references/symbolic-to-c-code.md | markdown | 3.7 KB |
| references/symmatrix-workflow.md | markdown | 3.0 KB |
| references/symunit-workflow.md | markdown | 3.6 KB |
references/symbolic-to-c-code.md
View on GitHub# Symbolic to C Code Generation
Use `matlabFunction` to write an optimized MATLAB function file from a symbolic expression, then `codegen` to compile it to C/C++.
## Workflow
1. Create or simplify the symbolic expression
2. Use `matlabFunction(..., 'File', 'myFunc.m')` to generate an optimized `.m` file
3. Use `codegen` to compile the `.m` file to C/C++ code or a MEX file
4. Verify the generated code
## Key Functions
| Function | Purpose | Toolbox | Since |
|----------|---------|---------|-------|
| `matlabFunction(expr, 'File', path)` | Convert symbolic expression to optimized `.m` function file | Symbolic Math | R2008b |
| `codegen funcName -args {...}` | Generate C/C++ code from MATLAB function | MATLAB Coder | R2011a |
| `ccode(expr)` | Generate C code string (single expression only) | Symbolic Math | — |
## Why Not Just `ccode`?
`ccode` converts a single symbolic expression to a C code string. It does NOT:
- Handle multi-output functions
- Optimize common subexpressions across a full function
- Generate a complete, compilable C program
- Handle input/output argument marshalling
`matlabFunction` + `codegen` is the supported, scalable pipeline.
## Pattern: Full Pipeline
```matlab
% Step 1: Define symbolic expression
syms x y z real
expr = sin(x^2 + y^2) * exp(-z) + cos(x*y*z);
% Step 2: Generate optimized MATLAB function file
matlabFunction(expr, 'File', 'mySymExpr.m', ...
'Vars', [x y z], ...
'Optimize', true);
% Step 3: Generate C code (as static library)
cfg = coder.config('lib');
cfg.GenerateReport = true;
codegen -config cfg mySymExpr -args {0, 0, 0}
% Step 4: Or generate MEX for fast in-MATLAB execution
codegen -config:mex mySymExpr -args {0, 0, 0}
```
## Pattern: Vector/Matrix Output
```matlab
syms t real
syms A gamma omega phi real
% Damped oscillator: position and velocity
pos = A * exp(-gamma*t) * cos(omega*t + phi);
vel = diff(pos, t);
% Generate function with multiple outputs
matlabFunction(pos, vel, 'File', 'oscillator.m', ...
'Vars', [t A gamma omega phi], ...
'Outputs', {'position', 'velocity'}, ...
'Optimize', true);
% Generate C code
codegen oscillator -args {0, 0, 0, 0, 0} -config:lib
```
## Pattern: Large Expression with Optimization
```matlab
syms x1 x2 x3 x4 x5 real
% Large expression from symbolic computation
bigExpr = jacobian([x1^2*x2 + sin(x3*x4); exp(x1*x5) + x2*x3], [x1 x2 x3 x4 x5]);
% matlabFunction with Optimize=true handles common subexpression elimination
matlabFunction(bigExpr, 'File', 'myJacobian.m', ...
'Vars', [x1 x2 x3 x4 x5], ...
'Optimize', true);
% Compile to MEX for fast numerical evaluation
codegen -config:mex myJacobian -args {zeros(1,5)}
```
## When to Use `ccode` Instead
`ccode` is appropriate only when:
- You need a single C expression to embed in an existing C program
- You are generating code for a very specific, non-MATLAB target
- The expression is simple enough that no optimization is needed
```matlab
syms x real
expr = x^2 + 2*x + 1;
cStr = ccode(expr); % Returns: "t0 = (x+1.0)*(x+1.0);"
```
## Common Mistakes
| Mistake | Why It's Wrong | Correct Approach |
|---------|---------------|-----------------|
| Using `ccode` + manually writing a C file | Brittle, doesn't optimize, doesn't scale | `matlabFunction(...,'File',...)` then `codegen` |
| Using `matlabFunction` without `'File'` | Creates a function handle, not a file — can't use with `codegen` | Always pass `'File'` when targeting C generation |
| Forgetting `'Optimize', true` | Misses common subexpression elimination | Always pass `'Optimize', true` for C targets |
| Not specifying `-args` with `codegen` | `codegen` needs input types to generate C | Always provide `-args` with example inputs |
----
Copyright 2026 The MathWorks, Inc.
----
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |