Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Computational Mathematics in Symbolic Math Toolbox

This example provides an overview of the Symbolic Math Toolbox which offers a complete set of tools for computational and analytical mathematics.

This example includes

  • Variables, Expressions, Functions and Equations

  • Substitution and Solving

  • Simplification and Manipulation

  • Calculus (Differentiation, Integration, Limits, Series)

  • Differential Equations

  • Linear Algebra

  • Graphics

For more details see Getting Started with Symbolic Math Toolbox. For more details on documenting and sharing your mathematics see Live Scripts.

Variables, Expressions, Functions and Equations

Variables in MATLAB are by default double-precision. The Symbolic Math Toolbox extends this by allowing you to express numbers in exact symbolic form using sym and with variable-precision using vpa.

pi/6 + pi/4
ans = 1.3090
sym(pi/6) + sym(pi/4)
ans = 

vpa(pi/6) + vpa(pi/4)
ans = 

Symbolic variables can be used in mathematical expressions, functions and equations including trigonometric, logarithmic, exponential, and special functions. You can create symbolic expressions and perform mathematical calculations on them.

syms x y 
log(x) + exp(y)
ans = 

You can also create piecewise functions.

y(x) = piecewise(x<0, -1, x>0, 1)
y(x) = 

Create and evaluate functions. Find the value of f at .

syms f(x)
f(x) = x^4-2*x^3+6*x^2-2*x+10
f(x) = 

f(-5)
ans = 

Find the intersection between lines and using solve. Equate the lines using the == operator.

syms y1 y2
y1 = x+3; y2 = 3*x;
solve(y1 == y2)
ans = 

Make assumptions on symbolic variables. There are 4 solutions to , two real and two complex. Assuming that x is real and , there is only one solution.

syms x
solve(x^4 == 1)
ans = 

assume(x,'real')
assumeAlso( x > 0)
assumptions(x)
ans = 

solve(x^4 == 1)
ans = 

assume(x,'clear')

Substitution and Solving

The Symbolic Math Toolbox supports evaluation of mathematical functions by substituting for any part of an expression using subs. You can substitute numeric values, other symbolic variables or expressions, vectors, or matrices. The Symbolic Math Toolbox supports the solving of equations and systems of equations using solve. It supports solving multivariate equations, solving inequalities and solving with assumptions. Solutions can be found symbolically or numerically with high precision by using variable-precision arithmetic.

Make substitutions with your symbolic variables. Substitute into

syms x xo
subs(x^2+1,x,xo-1)
ans = 

Substitute multiple values. For example, evaluate by substituting .

syms a b c
subs(cos(a) + sin(b) - exp(2*c), [a b c], [pi/2 pi/4 -1])
ans = 

Create and solve equations. Find the zeros of .

solve(9*x^2 - 1 == 0)
ans = 

Solve the general quadratic equation and use subs to evaluate that solution for .

eqn = a*x^2 + b*x + c == 0;
sol = solve(eqn) 
sol = 

subs(sol,[a b c],[9 0 -1])
ans = 

Solve equations symbolically or with variable-precision arithmetic when exact results or high precision is needed. The graph of is very flat near its root.

syms x f(x)
assume(x>0)
f(x) = 6*x^7-2*x^6+3*x^3-8;
fplot(f)
xlim([-10 10])
ylim([-1e3 1e3])

doubleSol = roots([-8 3 -2 6]) %  double-precision
doubleSol = 3×1 complex

   0.9471 + 0.0000i
  -0.2861 + 0.8426i
  -0.2861 - 0.8426i

symsSol = solve(f) % exact. The roots object stores the zeros for symbolic computations
symsSol = 

vpaSol = vpasolve(f) % variable-precision 
vpaSol = 

Simplification and Manipulation

The Symbolic Math Toolbox supports the simplification and manipulation of mathematical functions. Most mathematical expressions can be represented in different, but mathematically equivalent forms and the Symbolic Math Toolbox supports a number of operations, including factoring or expanding expressions, combining terms, rewriting or rearranging expressions, and simplification based on assumptions.

Perform polynomial multiplication and simplify the results, show that simplifies to .

simplify((x - 1)*(x + 1)*(x^2 + x + 1)*(x^2 + 1)*(x^2 - x + 1)*(x^4 - x^2 + 1))
ans = 

Apply trigonometric identities to simplifications, for example .

combine(2*sin(x)*cos(x) + (1- cos(2*x))/2 + cos(x)^2,'sincos')
ans = 

Factor or expand multivariate polynomials.

syms x y
factor(y^6-x^6)
ans = 

f(x) = (x^3 + 7);
expand(f(y-1))
ans = 

Find the functional composition .

f(x) = sqrt(log(x));
g(x) = sqrt(1-x);
h = compose(g,f,x)
h(x) = 

Calculus (Differentiation, Integration, Limits, Series, etc)

The Symbolic Math Toolbox has a full set of calculus tools for applied mathematics. It can perform multivariate symbolic integration and differentiation. It can generate, manipulate, and perform calculations with series.

Find the derivative of .

diff(sin(x))
ans = 

Find the derivative of using the chain rule.

diff(x^2+sin(2*x^4)+1,x)
ans = 

Find the indefinite integral for .

int(exp(-x^2/2),x)
ans = 

Find the definite integral for from 0 to 1.

int(x*log(1+x),0,1)
ans = 

Show that at by computing the Taylor series expansion for around the point .

syms x 
T = taylor(sin(x)/x)
T = 

subs(T,x,0)
ans = 

Show that is discontinuous at by showing that the left and right limits are not equal. .

limit(tan(x),x,pi/2,'left')
ans = 

limit(tan(x),x,pi/2,'right')
ans = 

limit(tan(x),x,pi/2)
ans = 

Differential Equations

The Symbolic Math Toolbox can analytically solve systems of ordinary differential equations using dsolve.

Solve the first order ODE .

syms a b y(x)
dsolve(diff(y) == -a*y)
ans = 

Solve the same ODE with the initial condition .

dsolve(diff(y)== -a*y,y(0)==b)
ans = 

Solve the system of coupled first order ODEs and .

syms x(t) y(t)
z = dsolve(diff(x) == y, diff(y) == -x);
disp([z.x;z.y])

Linear Algebra

The Symbolic Math Toolbox can work with symbolic vectors and matrices. It can compute eigenvalues and eigenvectors of symbolic matrices.

Perform matrix multiplication where and

syms a b c d
syms x1 x2
x = [x1; x2];
A = [a b ; c d];
b = A*x
b = 

Find the determinant of A.

det(A)
ans = 

Find the eigenvalues of A.

lambda = eig(A)
lambda = 

Graphics

The Symbolic Math Toolbox supports analytical plotting in 2D and 3D.

fplot(tan(x))

Plot the parametric curve and .

syms t
x = t*sin(5*t); 
y = t*cos(5*t);
fplot(x, y)
grid on

Plot the 3D parametric curve , and from [-10,10] with a dashed red line.

syms t
xt = exp(abs(t)/10).*sin(5*abs(t));
yt = exp(abs(t)/10).*cos(5*abs(t));
zt = t;
h = fplot3(xt,yt,zt, [-10,10],'--r');

Plot the 3D surface .

syms x y
fsurf(sin(x) + cos(y))

Plot the 2D contours of the same surface.

fcontour(sin(x) + cos(y))

Was this topic helpful?