Symbolic Computations
MATLAB Symbolic Computation
MATLAB Symbolic Computation lets you manipulate formulas before choosing numerical values. You can retain exact fractions, rearrange expressions, solve equations, differentiate, and integrate using Symbolic Math Toolbox. This is useful when you need a formula that applies to several parameter values or want to check a calculation algebraically. A symbolic result still needs interpretation: its assumptions, domain, and physical units determine whether it answers your problem.
Create exact symbolic expressions
A symbolic expression represents a mathematical quantity using symbolic numbers, variables, and operations. Declare variables before constructing an expression:
syms x
f = x^2 + 4*x + 3;
c = sym(1)/7;Here, f retains its dependence on x, and c stores the exact fraction 1/7. MATLAB requires explicit multiplication, so write 4*x rather than 4x.
Representation | Example | What you obtain |
|---|---|---|
Ordinary numeric arithmetic | 1/7 | A double-precision approximation |
Exact symbolic arithmetic | sym(1)/7 | The exact rational number 1/7 |
Symbolic decimal approximation | vpa(sym(1)/7,8) | An approximation requested to eight significant digits |
Create exact symbolic inputs before carrying out operations that you want to keep exact. Adding decimal digits to an already rounded measurement does not supply additional measurement accuracy.
Transform expressions and solve equations
An expression is different from an equation. The command f = x^2 + 4*x + 3 assigns an expression to a workspace name. A double equals sign constructs a symbolic equation:
eqn = f == 0;
r = solve(eqn,x);
check = simplify(subs(f,x,r));The solutions are x=−3 and x=−1. Substituting either into the original polynomial gives zero, so check contains two zeros. Naming the unknown explicitly makes your intention clear when other symbols appear.
Use expand to multiply out products and simplify to seek a simpler equivalent form. A shorter expression is not automatically valid at every input where its printed formula can be evaluated.
Differentiate, integrate, and evaluate
Symbolic calculus operates on the expression itself. For the polynomial above, the governing operations are
Specify the variable in each command:
df = diff(f,x);
A = int(f,x,0,1);
slope = subs(df,x,2);
slopeNumeric = double(slope);The derivative at x=2 is exactly 8; double converts that symbolic number into an ordinary MATLAB numeric value. For an indefinite integral, int(f,x) returns an antiderivative without an arbitrary constant. Add C when writing the general mathematical family.
Use subs(expression,variable,value) to evaluate a symbolic expression. Assigning x = 2 afterward does not substitute into an expression already stored in f. An expression with unresolved variables generally cannot be converted into an ordinary numeric value using double.
State assumptions and check results
An assumption restricts the values a symbolic variable may represent. Symbols are complex by default. Use syms x real for a real variable or syms x positive for a positive variable when that restriction is justified.
For real x, x2=∣x∣; for positive x, it equals x. Assumptions also affect which roots solve returns. Always compare returned roots with the original equation and any physical restrictions. Recreating a variable with syms x clears its previous assumptions.
In engineering calculations, state the units represented by each variable and coefficient; ordinary symbolic variables do not automatically carry units.
What to remember
- Create exact symbolic inputs before manipulating a formula.
- Use == for equations and specify the variable in solve, diff, and int.
- Substitute values deliberately, convert at the end, and check domains and units.
Example: Energy delivered during a power ramp
A laboratory heater draws electrical power according to
for 0≤t≤8 s. The cumulative electrical energy supplied is zero at t=0. Use MATLAB Symbolic Computation with numerical values expressed in SI units.
a) Obtain an exact symbolic expression for the energy E(T) supplied from t=0 to an elapsed time T within the model's interval.
Energy supplied over an interval is the definite integral of power. Declare both the integration variable and the upper limit symbolically:
syms t T real
P = 40 + 8*t;
E = int(P,t,0,T);The command integrates with respect to t and retains T as the adjustable upper limit. Mathematically,
Both terms have units of watt-seconds, or joules. MATLAB therefore stores 40*T + 4*T^2, representing the energy in joules when T is expressed in seconds. This result applies for 0≤T≤8 s.
b) Find when the cumulative supplied energy reaches 224 J. Report the physically admissible time.
Set the symbolic energy expression equal to the target and solve explicitly for T:
candidates = solve(E == 224,T);
double(candidates)Because T was declared real rather than positive, both real algebraic roots are available for inspection. Using numerical values in SI units,
The candidates are T=−14 s and T=4 s. Only 4 s lies in the physical interval 0≤T≤8 s.
Check the accepted time in the energy expression:
The heater reaches the target energy at T=4 s.
c) Evaluate the supplied energy during the first 2.5 s and the average power over that interval. Preserve the time exactly during substitution.
Represent 2.5 exactly as the symbolic fraction 5/2. Substitute it into the stored energy expression, then divide that energy by the elapsed time:
Tvalue = sym(5)/2;
Evalue = subs(E,T,Tvalue);
Paverage = Evalue/Tvalue;
double(Evalue)
double(Paverage)The energy calculation is
Average power is energy supplied divided by the duration:
As a check, the power rises linearly from 40 W to 40+8(2.5)=60 W. Its average is (40+60)/2=50 W, agreeing with the integral-based result. The supplied energy is 125 J and the average power is 50 W.
An engineer has run syms x in MATLAB and is manipulating polynomial expressions symbolically. For each task below, type only the name of the Symbolic Math Toolbox function, without parentheses or arguments.
Multiplying out (x+2)3 into the sum x3+6x2+12x+8 uses . Rewriting x2−9 as a product of its lower-degree factors uses . Finding an antiderivative or a definite integral of an expression with respect to x uses . Replacing x with the number 2 in an expression uses .
A motion model represents displacement by
where displacement is in metres, time is in seconds, k has units of m/s3, and the coefficient of t is 2 m/s. The following MATLAB code uses numerical values in those units:
syms t k real
s = k*t^3 + 2*t;
result = subs(diff(s,t),[t k],[2 3]);What velocity does result represent?