MATLAB is a powerful tool for both symbolic and numeric computations. While symbolic expressions let you represent mathematical formulas in their exact algebraic form, there are times when you need numerical values for further analysis, simulation, or visualization.
This is where evaluating symbolic expressions numerically becomes essential. MATLAB provides built-in functions like subs() and vpa() that make it easy to substitute values and compute numeric results with precision.
A symbolic expression in MATLAB is an algebraic formula that represents variables, constants, and mathematical operations symbolically, without assigning specific numeric values.
You can define symbolic variables using the syms command.
Example:
syms x y expr = x^2 + 3*y; Here, expr is a symbolic expression representing the formula x2+3yx^2 + 3yx2+3y.
Evaluating symbolic expressions numerically helps you:
Compute actual numeric results from formulas.
Test expressions with specific input values.
Perform engineering or simulation calculations that require numerical output.
Combine symbolic modeling with numeric analysis in hybrid workflows.
subs() FunctionThe subs() function substitutes specific numeric values into a symbolic expression.
Syntax:
subs(expression, variable, value) Example:
syms x expr = x^2 + 2*x + 1; % Substitute x = 3 result = subs(expr, x, 3) Output:
result = 16 Here, MATLAB replaces x with 3 and calculates 32+2∗3+1=163^2 + 2*3 + 1 = 1632+2∗3+1=16.
subs() for Multiple VariablesYou can substitute multiple values simultaneously.
Example:
syms x y expr = x^2 + 3*y; % Substitute x = 2, y = 5 result = subs(expr, [x y], [2 5]) Output:
result = 19 double() FunctionOnce you have a symbolic result, you can convert it into a numeric (double-precision) value using double().
Example:
syms x expr = sin(x) + cos(x); val = subs(expr, x, pi/4); numericResult = double(val) Output:
numericResult = 1.4142 Here, MATLAB substitutes x = π/4 and converts the symbolic result to a numeric approximation.
vpa() for High-Precision EvaluationThe vpa() (Variable Precision Arithmetic) function evaluates expressions numerically with arbitrary precision.
Syntax:
vpa(expression, digits) Example:
syms x expr = 1/x; % Evaluate with high precision for x = 3 val = subs(expr, x, 3); highPrecision = vpa(val, 20) Output:
highPrecision = 0.33333333333333333333 vpa() is particularly useful when you need more decimal accuracy than MATLAB’s default double() precision.
double() vs vpa()| Function | Purpose | Precision |
|---|---|---|
double() | Converts symbolic result to double precision numeric value | 15–16 digits |
vpa() | Evaluates with arbitrary precision | User-defined digits |
eval() FunctionIn some cases, you can convert symbolic expressions into MATLAB commands and use eval() to compute results.
Example:
syms x expr = sin(x)^2 + cos(x)^2; % Evaluate for x = pi/6 numericValue = eval(subs(expr, x, pi/6)) Output:
numericValue = 1 Let’s compute the deflection of a beam using a symbolic formula and evaluate it numerically.
Example:
syms F L E I deflection = (F*L^3) / (3*E*I); % Substitute known values F = 500; L = 2; E = 200e9; I = 1.6e-5; delta = subs(deflection) delta = double(delta) Output (approx):
delta = 4.1667e-05 % meters This shows how symbolic math integrates seamlessly with engineering data in MATLAB.
Evaluating symbolic expressions numerically is a vital step in MATLAB programming.
By using functions like subs(), double(), and vpa(), you can easily move between symbolic representation and numerical computation—combining the power of algebraic manipulation with real-world numerical accuracy.
This hybrid approach makes MATLAB a top choice for engineers, scientists, and researchers working on analytical and simulation-based projects.
“I got full marks on my MATLAB assignment! The solution was perfect and delivered well before the deadline. Highly recommended!”
“Quick delivery and excellent communication. The team really understood the problem and provided a great solution. Will use again.”
Explore how MATLAB Solutions has helped clients achieve their academic and research goals through practical, tailored assistance.
In today\\\'s rapidly advancing era of automation, robotics control systems are evolving to meet the demand for smarter, faster, and more reliable performance. Among the many innovations driving this transformation is the use of MCP (Model-based Control Paradigms)
The financial sector is witnessing a technological revolution with the rise of Large Language Models (LLMs). Traditionally used for text analysis, LLMs are now being integrated with powerful platforms like MATLAB to develop financial forecasting models