This example demonstrates how Symbolic Math Toolbox™ enables physics-aware controller design for automotive applications. A DC motor drives a load through a flexible shaft, creating a 4-state drivetrain model whose torsional resonance depends on the reflected load inertia
The load inertia varies with operating conditions: full traction with a heavy vehicle gives
By keeping
Symbolic declaration of a four-state motor-shaft-load drivetrain: motor speed
The flexible shaft coupling uses stiffness
syms- declare physical parameters symbolically- Symbolic state-space matrices - encode motor, electrical, shaft, and load dynamics
Known motor and shaft constants are substituted while
subs- selectively substitute fixed motor and shaft parameterseig- compute pole migration across the inertia rangess,bode- visualize the load-speed frequency response
The control law is
Because the system matrix contains symbolic
det- form the controllability determinant and characteristic equationsolve- compute symbolic pole placement gainssimplify,subs- reduce gains after substituting fixed physical constants
The closed-loop matrix
eig- verify closed-loop poles after substituting each inertiastep- compare load-speed tracking responses
A fixed-gain controller designed for SUV full traction (
The live script also notes that real actuator voltage limits, such as a 48 V armature supply, would make resonance-driven voltage swings more damaging in production unless voltage limiting and anti-windup are included.
Convert the symbolic gain and reference-scaling expressions into optimized MATLAB functions. These functions take
matlabFunction- generatescontrollerGains.mandreferenceScaling.mccode- emits C code for the symbolic gain expressions
The generated functions can be called directly from MATLAB® Function blocks in Simulink®. The estimated load inertia can come from a lookup table, online estimator, or traction control module.
J_L_est (from estimator, lookup, or traction module)
| | |
v v v
r -----> [Nbar(JL)] -->(+)----> u ---> [Plant(JL)] ----> y
^(-) |
| v
+---- [K(JL)] <--- x (state feedback)
buildSimulinkModel.m creates a working model for a traction-loss scenario where
Running the example produces deployable MATLAB functions:
| Generated file | Signature | Use case |
|---|---|---|
controllerGains.m |
K = controllerGains(J_L) |
1x4 state feedback gain vector for the current load inertia |
referenceScaling.m |
Nbar = referenceScaling(J_L) |
Reference precompensator for zero steady-state load-speed error |
C code equivalents for the gain expressions are available via ccode().
- MATLAB R2024b or later
- Symbolic Math Toolbox
- Control System Toolbox™
- Simulink (for the generated integration model)
- Open MATLAB and navigate to this directory
- Open
DCMotorSpeedAdaptive.mas a Live Script - Run section by section, or run all - the example is self-contained
- Run
buildSimulinkModel.mto create the Simulink traction-loss model
The generated functions (controllerGains.m, referenceScaling.m) are written to the working directory at runtime. The Simulink model (DCMotorSpeedAdaptiveModel.slx) is created by buildSimulinkModel.m.
| File | Description |
|---|---|
DCMotorSpeedAdaptive.m |
Main example (Live Script). Full workflow from symbolic drivetrain modeling through adaptive gain generation and verification. |
buildSimulinkModel.m |
Programmatically creates the Simulink model that uses the generated functions in a traction-loss scenario. |
controllerGains.m |
Generated at runtime. State feedback gains as a function of load inertia. |
referenceScaling.m |
Generated at runtime. Reference scaling as a function of load inertia. |
DCMotorSpeedAdaptiveModel.slx |
Generated by buildSimulinkModel.m. Simulink model demonstrating runtime-adaptive control. |