Evaluate Symbolic Expression in MATLAB Programming

MATLAB Illustration

MATLAB excels at both exact algebraic (symbolic) manipulation and high-speed numerical calculation. While symbolic math lets you keep formulas in pure analytical form without rounding errors, real-world simulations, plotting, and engineering tasks require converting those symbolic equations into concrete numbers.

MATLAB provides simple built-in functions—such as subs(), double(), and vpa()—to evaluate symbolic equations with standard or arbitrary precision.

What Is a Symbolic Expression?

A symbolic expression represents a mathematical formula using symbolic variables created via the syms command, without binding them to static numbers:

MATLAB
syms x y expr = x^2 + 3*y;

Here, expr represents the analytical equation (x^2 + 3y).

Methods for Evaluating Symbolic Expressions

1. Substituting Values with subs()

The subs() function replaces symbolic variables with specific numerical inputs or other expressions.

MATLAB
syms x expr = x^2 + 2*x + 1;
% Substitute x = 3 result = subs(expr, x, 3) % Output: 16

2. Substituting Multiple Variables Simultaneously

Pass arrays of variables and replacement values to evaluate multi-variable expressions:

MATLAB
syms x y expr = x^2 + 3*y;
% Substitute x = 2 and y = 5 result = subs(expr, [x y], [2 5]) % Output: 19

3. Converting Symbolic Results to Double Precision with double()

Even after substitution, MATLAB often keeps results in exact symbolic fraction format (e.g. sqrt(2)/2). Use double() to convert the output to a standard IEEE 64-bit floating-point number:

MATLAB
syms x expr = sin(x) + cos(x);
val = subs(expr, x, pi/4);
numericResult = double(val) % Output: 1.4142

4. High-Precision Evaluation with vpa()

When default 64-bit floating-point precision (15–17 decimal digits) is insufficient, Variable Precision Arithmetic (vpa()) computes results to any arbitrary number of digits:

MATLAB
syms x expr = 1/x;
val = subs(expr, x, 3);
highPrecision = vpa(val, 20) % Output: 0.33333333333333333333

Comparison: double() vs. vpa()

Function Primary Purpose Numeric Precision
double() Converts symbolic output to standard floating-point representation 15–17 decimal digits
vpa() Evaluates symbolic output using Variable Precision Arithmetic User-specified number of digits

Practical Example: Engineering Cantilever Beam Deflection

Solve an engineering formula analytically and evaluate it with physical parameters:

MATLAB
% Define symbolic variables for Force, Length, Modulus, and Moment of Inertia syms F L E I deflection = (F * L^3) / (3 * E * I);
% Substitute physical test parameters F_val = 500;
% Force in Newtons L_val = 2;
% Length in meters E_val = 200e9;
% Young's Modulus in Pascals I_val = 1.6e-5;
% Area Moment of Inertia in m^4  % Calculate numerical beam deflection in meters delta_sym = subs(deflection, [F L E I], [F_val L_val E_val I_val]);
delta = double(delta_sym) % Output: 4.1667e-05 (meters)
Verified Feedback

What Engineering Students Say

Real feedback from students across top engineering universities worldwide.

Verified Student

“I got full marks on my MATLAB DSP assignment! The filter design code was completely vectorized, the frequency response plots were exact, and the delivery was 8 hours before my deadline. Highly recommended!”

AS

Aditi Sharma

IIT Bombay • Signal Processing Coursework
Verified Student

“Our Simulink EV powertrain model had severe algebraic loop and solver errors. The MATLABSolutions team fixed the solver configuration in 4 hours and provided an annotated scope diagram. Lifesaver for my final year!”

JM

John M.

Monash University, Australia • Simulink Dynamic Model
Technical Knowledge Base

Latest MATLAB Guides & Tutorials

Explore deep-dive technical articles written by our engineering team to master complex MATLAB & Simulink topics.

MATLAB Guide 5 Min Read

Reinforcement Learning for Microgrid Energy Management in MATLAB & Simulink

Operating a modern microgrid is an ongoing balancing act. Between changing solar output, shifting wind speeds, volatile electricity pricing, and unpredictable consumer demand, a...

MATLAB Guide 5 Min Read

Physics-Informed Neural Networks (PINNs) for Microgrid Dynamics and Power Flow in MATLAB

Modern microgrids operate with low physical inertia, rapid inverter switching dynamics, and intermittent renewable power generation. Simulating these systems requir...