Defining Symbolic Expressions

  • Matlab has a symbolic math ability. Rather than making calculations on known numbers, we can make calculations on symbolic expressions.
  • The key function in Matlab to create a symbolic representation of data is: syms
In [1]:
syms x;

In [2]:
syms a b c % Create multiple symbolic variables in one command

In [3]:
A = sym('a',[1,20]) % Create the variables a1, ..., a20
 
A =
 
[ a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20]
 

In [4]:
syms a b c x
f = a*x^2+b*x+c
 
f =
 
a*x^2 + b*x + c
 

In [5]:
subs(f,x,5) % Assign a value to x
 
ans =
 
25*a + 5*b + c
 

In [6]:
subs(f,[x a b c],[5 1 2 3]) % Assign a list of values to a, b, c and x
 
ans =
 
38
 

Symbolic Algebra

  • expand(S): Multiples out all the portions of the expression or equation
In [7]:
syms x
expand((x-3)*(x+6))
 
ans =
 
x^2 + 3*x - 18
 

factor(S): Factors the expression or equation

In [8]:
syms x
factor(x^3-1)
 
ans =
 
[ x - 1, x^2 + x + 1]
 

  • collect(S): Collects like terms
In [9]:
syms x
S = 2*(x+3)^2 + x^2 + 6*x + 9;
collect(S)
 
ans =
 
3*x^2 + 18*x + 27
 

  • simplify(S): Simplifies in accordance with MuPad's simplification rules
In [10]:
syms x
simplify(exp(log(x)))
 
ans =
 
x
 

  • numden(S): Finds the numerator of an expression; this function is not valid for equations
In [11]:
syms x
numden((x-5)/(x+5))
 
ans =
 
x - 5
 

  • [num,den] = numden(S): Finds bot the numerator and the denominator of an expression
In [12]:
syms x
[num,den] = numden((x-5)/(x+5))
 
num =
 
x - 5
 
 
den =
 
x + 5
 

Solving Expressions and Equations (solve(S))

  • solve(S): Solves an expression with a single variable
In [13]:
syms x
solve(x-5)
 
ans =
 
5
 

  • solve(S): Solves an equation with a single variable
In [14]:
syms x
solve(x^2-2==5)
 
ans =
 
  7^(1/2)
 -7^(1/2)
 

  • solve(S): Solves an equation whose solutions are complex numbers
In [15]:
syms x
solve(x^2==-5)
 
ans =
 
 -5^(1/2)*1i
  5^(1/2)*1i
 

  • solve(S): Solves an equation with more than one variable for x or the closest variable to x
In [16]:
syms x y
solve(y==x^2+2)
 
ans =
 
  (y - 2)^(1/2)
 -(y - 2)^(1/2)
 

  • solve(S,y): Solves an equation with more than one variable for a specified variable
In [17]:
syms x y
solve(y+6*x,x)
 
ans =
 
-y/6
 

  • solve(S1,S2,S3): Solves a system of equations and presents the solutions as a structure array
In [18]:
syms x y z
one = 3*x+2*y-z==10;
two = -x+3*y+2*z==5;
three = x-y-z==-1;
solve(one,two,three)
ans = 

  struct with fields:

    x: [1×1 sym]
    y: [1×1 sym]
    z: [1×1 sym]


  • [A,B,C] = solve(S1,S2,S3): Solves a system of equations and assigns the solutions to user-defined variable names; displays the results alphabetically
In [19]:
syms x y z
one = 3*x+2*y-z==10;
two = -x+3*y+2*z==5;
three = x-y-z==-1;
[A,B,C] = solve(one,two,three)
 
A =
 
-2
 
 
B =
 
5
 
 
C =
 
-6
 

Symbolic Plotting

  • ezplot(f): Plots a symbolic expression, equation, or function $f$. By default, ezplot plots a univariate expression or function over the range $[-2\pi,2\pi]$ or over a subinterval of this range. If $f$ is an equation or function of two variables, the default range for both variables, the default range for both variables is $[-2\pi,2\pi]$ or over a subinterval of this range.
In [20]:
syms x y
f(x,y)=sin(x+y)*sin(x*y);
ezplot(f)

  • ezplot(f,[min,max]): plots $f$ over the specified range. If $f$ is a univariate expression or function, then $[min,max]$ specifies the range for that variable along the abscissa. If $f$ is an equation or function of two variables, then $[min,max]$ specifies the range for both variables along both the abscissa and the ordinate.
In [21]:
syms x
y=x^2-2;
ezplot(y,[-10,10])

In [22]:
ezplot('x^2+y^2=1',[-1.5,1.5])

  • ezplot(f,[xmin,xmax,ymin,ymax]): Plots $f$ over the specified ranges along the abscissa and the ordinate.
In [23]:
syms x
a=1/x;
ezplot(a,[-2,2,-3,3])

Symbolic Differentiation

  • diff(f): Returns the derivative of the expression $f$ with respect to the default independent variable.
In [24]:
syms x z
y = x^3+z^2;
diff(y)
 
ans =
 
3*x^2
 

  • diff(f,'t'): Returns the derivative of the expression $f$ with respect to the variable $t$.
In [25]:
syms x z
y = x^3+z^2;
diff(y,'z')
 
ans =
 
2*z
 

  • diff(f,n): Returns the $n$th derivative of the expression $f$ with respect to the default independent variable.
In [26]:
syms x z
y = x^3+z^2;
diff(y,2)
 
ans =
 
6*x
 

  • diff(f,'t',n) : Returns the $n$th derivative of the expression $f$ with respect to the variable $t$.
In [27]:
syms x z
y = x^3+z^2;
diff(y,'z',2)
 
ans =
 
2
 

Symbolic Integration

  • int(f): Returns the integral of the expression $f$ with respect to the default independent variable.
In [28]:
syms x z
y = x^3+z^2;
int(y)
 
ans =
 
x^4/4 + x*z^2
 

  • int(f,'t'): Returns the integral of the expression $f$ with respect to the variable $t$.
In [29]:
syms x z
y = x^3+z^2;
int(y,'z')
 
ans =
 
x^3*z + z^3/3
 

  • int(f,a,b): Returns the integral of the expression $f$ with respect to the default independent variable, between the numeric bounds $a$ and $b$.
In [30]:
syms x z
y = x^3+z^2;
int(y,2,3)
 
ans =
 
z^2 + 65/4
 

  • int(f,'t',a,b): Returns the integral of the expression $f$ with respect to the variable $t$, between the numeric bounds $a$ and $b$.
In [31]:
syms x z
y = x^3+z^2;
int(y,'z',2,3)
 
ans =
 
x^3 + 19/3
 

  • int(f,'t','a','b'): Returns the integral of the expression $f$ with respect to the variable $t$, between the symbolic bounds $a$ and $b$.
In [32]:
syms x z
y = x^3+z^2;
int(y,'z','a','b')
 
ans =
 
- a^3/3 - a*x^3 + b^3/3 + b*x^3
 

Converting Symbolic Expressions and Equations

  • g = matlabFunction(f): Converts $f$ to a MATLAB function with the handle $g$. $f$ can be a symbolic expression, function, or a vector of symbolic expressions or functions.
In [33]:
syms x y
s = sqrt(x^2+y^2);
g = matlabFunction(sin(s)/s)
g =

  function_handle with value:

    @(x,y)sin(sqrt(x.^2+y.^2)).*1.0./sqrt(x.^2+y.^2)


In [34]:
syms x y
f(x,y) = sqrt(x^2+y^2);
g = matlabFunction(f)
g =

  function_handle with value:

    @(x,y)sqrt(x.^2+y.^2)