Evaluate Symbolic Expression in MATLAB Programming
Vectorized implementations, execution benchmarks, and step-by-step programming documentation.
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:
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.
syms x expr = x^2 + 2*x + 1;
% Substitute x = 3 result = subs(expr, x, 3) % Output: 162. Substituting Multiple Variables Simultaneously
Pass arrays of variables and replacement values to evaluate multi-variable expressions:
syms x y expr = x^2 + 3*y;
% Substitute x = 2 and y = 5 result = subs(expr, [x y], [2 5]) % Output: 193. 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:
syms x expr = sin(x) + cos(x);
val = subs(expr, x, pi/4);
numericResult = double(val) % Output: 1.41424. 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:
syms x expr = 1/x;
val = subs(expr, x, 3);
highPrecision = vpa(val, 20) % Output: 0.33333333333333333333Comparison: 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:
% 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)Need Expert Guidance with MATLAB or Simulink?
Our 500+ PhD engineers provide verified MATLAB code, custom Simulink models, and 1-on-1 tutoring with comprehensive code quality verification.