Basic
Computer
Programming
Dr. Deniz Kutluay
Symbolic Math
• SYMBOLIC OBJECTS AND SYMBOLIC
EXPRESSIONS
• CHANGING THE FORM OF AN EXISTING
SYMBOLIC EXPRESSION
• SOLVING ALGEBRAIC EQUATIONS
• DIFFERENTIATION
• INTEGRATION
• SOLVING AN ORDINARY DIFFERENTIAL
EQUATION
• PLOTTING SYMBOLIC EXPRESSIONS
• NUMERICAL CALCULATIONS WITH SYMBOLIC
EXPRESSIONS
Mission & Goals
Mission
Listen to me carefully, and practice some examples using Matlab by following me
Goal #1 Goal #2 Goal #3
To be able to To perform symbolic To compute
manipulate operations such as numerical
mathematical differentiation or calculations with
expressions in terms integration of symbolic expressions.
of symbolic variables. mathematical
expressions.
Basic Computer Programming Dr. Deniz Kutluay
Symbolic objects and
symbolic expressions
Creating Symbolic Objects
A symbolic object can be a variable
(without a preassigned numerical Symbolic objects can be variables or numbers. They
value), a number, or an expression can be created with the sym and/or syms commands.
made of symbolic variables and
numbers.
A symbolic expression is a mathematical
expression containing one or more
symbolic objects.
001
Basic Computer Programming Dr. Deniz Kutluay
Symbolic objects and
symbolic expressions
Creating Symbolic Objects
A single letter or a combination of
several letters (no spaces). Examples:
'a', 'x', 'yad'.
A combination of letters and digits
starting with a letter and with no
spaces, Examples: 'xh12', 'r2d2'.
A number. Examples: '15', '4'.
002
Basic Computer Programming Dr. Deniz Kutluay
Symbolic objects and
symbolic expressions
Creating Symbolic Objects
As mentioned, symbolic objects can
also be numbers.
The numbers don't have to be typed as
strings.
The display of symbolic objects starts
at the beginning of the line and is not
indented as is the display of numerical
variables. 003
Basic Computer Programming Dr. Deniz Kutluay
Symbolic objects and
symbolic expressions
Creating Symbolic Objects
Several symbolic variables can be
created in one command by using the
syms command.
The command creates symbolic objects
that have the same names as the
symbolic variables.
004
Basic Computer Programming Dr. Deniz Kutluay
Symbolic objects and
symbolic expressions
Creating Symbolic Expressions
Symbolic expressions are mathematical
expressions written in terms of symbolic
variables.
When a symbolic expression, which
includes mathematical operations that
can be executed, is entered, MATLAB
executes the operations as the
expression is created.
005
Basic Computer Programming Dr. Deniz Kutluay
Symbolic objects and
symbolic expressions
Creating Symbolic Expressions
An expression that is created can
include both symbolic objects and
numerical variables.
However, if an expression includes a
symbolic object (or several), all the
mathematical operations will be carried
out exactly.
006
Basic Computer Programming Dr. Deniz Kutluay
Symbolic objects and
symbolic expressions
Additional facts about symbolic
expressions and symbolic objects:
The double(S) command can be used
to convert a symbolic expression
(object) S that is written in an exact
form to numerical form.
007
Basic Computer Programming Dr. Deniz Kutluay
Symbolic objects and
symbolic expressions
Additional facts about symbolic expressions
and symbolic objects:
A symbolic object that is created can
also be a symbolic expression written
interms of variables that were not first It is impossible to perform symbolic math operations
created as symbolic objects. associated with the individual variables in the object.
It is important to understand that in
this case, the variables a, b, c, and x For example, it will not be possible to differentiate f with
included in the object do not exist respect to x.
individually as independent symbolic
objects 008
Basic Computer Programming Dr. Deniz Kutluay
Symbolic objects and
symbolic expressions
The findsym Command and the Default
Symbolic Variable
The findsym command can be used to
find which symbolic variables are
present in an existing symbolic
expression.
The findsym(S) command displays the
names of all the symbolic variables that
are in the expression S in alphabetical
order.
009
Basic Computer Programming Dr. Deniz Kutluay
Symbolic objects and
symbolic expressions
Default Order for one-letter Symbolic Variables
The default order starts with x, and followed by letters, according to their closeness to x. If
there are two letters equally close to x, the letter that is after x in alphabetical order is first
(y before w, and z before v). (Consider v-w-x-y-z)
The default symbolic variable in a symbolic expression is the first variable in the default
order.
The default symbolic variable in an expression S can be identified by typing findsym(S,1) .
010
Basic Computer Programming Dr. Deniz Kutluay
Changing the form of
an existing symbolic
expression
The expressions created by MATLAB The collect command:
might not be in the simplest form or in a
form that the user prefers. The collect command collects the terms in the
expression that have the variable with the same
The form of an existing symbolic power.
expression can be changed to the
desired form. In the new expression, the terms will be ordered
in decreasing order of power.
The collect, expand, and factor
commands can be used to perform the
mathematical operations that are
implied by their names. 011
Basic Computer Programming Dr. Deniz Kutluay
Changing the form of
an existing symbolic
expression
The collect command:
The collect command collects the
terms in the expression that have the
variable with the same power.
In the new expression, the terms will
be ordered in decreasing order of
power.
012
Basic Computer Programming Dr. Deniz Kutluay
Changing the form of
an existing symbolic
expression
The collect command:
The collect command collects the
terms in the expression that have the
variable with the same power.
In the new expression, the terms will
be ordered in decreasing order of
power.
013
Basic Computer Programming Dr. Deniz Kutluay
Changing the form of
an existing symbolic
expression
The expand command:
It carries out products of terms that
include summation (used with at least
one of the terms), and it uses
trigonometric identities and
exponential and logarithmic laws to
expand corresponding terms that
include summation
014
Basic Computer Programming Dr. Deniz Kutluay
Changing the form of
an existing symbolic
expression
The factor command:
The factor command changes an
expression that is a polynomial to a
product of polynomials of a lower
degree.
015
Basic Computer Programming Dr. Deniz Kutluay
Changing the form of
an existing symbolic
expression
The simplify command:
The simplify command uses
mathematical operations (addition,
multiplication, rules of fractions,
powers, logarithms, etc.) and
functional and trigonometric
identities to generate a simpler form
of the expression.
016
Basic Computer Programming Dr. Deniz Kutluay
Changing the form of
an existing symbolic
expression
The simple command:
The simple command finds the form
of the expression with the fewest
number of characters.
MATLAB creates several forms of the
expression by applying the collect,
expand, factor, and simplify
commands, and other simplification
functions
017
Basic Computer Programming Dr. Deniz Kutluay
Changing the form of
an existing symbolic
expression
018
Basic Computer Programming Dr. Deniz Kutluay
Solving algebraic
equations
Solving a single equation:
A system of equations can be solved
for several variables with the solve
function
If the equation has one variable, the
solution is numerical.
If the equation has several symbolic
variables, a solution can be obtained
for any of the variables in terms of
the others. 019
Basic Computer Programming Dr. Deniz Kutluay
Solving algebraic
equations
Solving a single equation:
The argument eq can be the name of
a previously created symbolic
expression, or an expression that is
typed in. MATLAB solves the equation
eq = 0.
An equation of the form f(x) = g(x)
can be solved by typing the equation
(including the = sign) as a string for
eq.
020
Basic Computer Programming Dr. Deniz Kutluay
Solving algebraic
equations
Solving a single equation:
If the equation to be solved has more
than one variable, the solve(eq)
command solves for the default
symbolic variable.
A solution for any of the variables can
be obtained with the solve(eq,var)
command by typing the variable
name for var.
021
Basic Computer Programming Dr. Deniz Kutluay
Solving algebraic
equations
Solving a single equation:
It is also possible to use the solve
command by typing the equation to
be solved as a string, without having
the variables in the equation first
created as symbolic objects.
However, if the solution contains
variables, the variables do not exist
as independent symbolic objects.
022
Basic Computer Programming Dr. Deniz Kutluay
Solving algebraic
equations
Solving a system of equations:
If the number of equations and the
number of variables are the same,
the solution is numerical.
If the number of variables is greater
than the number of equations, the
solution is symbolic for the desired
variables in terms of the other
variables.
The variables for which solutions are
obtained are chosen by MATLAB
according to the default order. 023
Basic Computer Programming Dr. Deniz Kutluay
Solving algebraic
equations
Solving a system of equations:
When the number of variables is
greater than the number of
equations n, the user can select the
variables for which the system is
solved.
This is done by using the second
format of the solve command and
entering the names of the variables
var1, var2,…., varn.
024
Basic Computer Programming Dr. Deniz Kutluay
Solving algebraic
equations
Solving a system of equations:
The output from the solve command, which is the solution of the system, can have two
different forms. One is a cell array and the other is a structure.
A cell array is an array in which each of the elements can be an array.
A structure is an array in which the elements (called fields) are addressed by textual field
designators. The fields of a structure and the elements of a cell array can be arrays of
different sizes and types.
025
Basic Computer Programming Dr. Deniz Kutluay
Solving algebraic
equations
Solving a system of equations: [varA, varB, varC] = solve (eq1,eq2,eq3)
When a cell array is used in the output
of the solve command, the command
has the following form (in the case of a
system of three equations).
Notice that the system of two
equations is solved by MATLAB for x
and y in terms of t, since x and y are Consider the order of
r-s-t-u-v-w-x-y-z
the first two variables in the default
order.
026
Basic Computer Programming Dr. Deniz Kutluay
Solving algebraic
equations
Solving a system of equations:
The system, however, can be solved
for different variables.
As an example, the system is solved
next for y and t in terms of x.
[varA, varB] = solve (eq1,eq2,var1,var2)
027
Basic Computer Programming Dr. Deniz Kutluay
Solving algebraic
equations
Solving a system of equations:
When a structure is used in the output
of the solve command, the command
has the form (in the case of a system
of three equations).
AN = solve (eq1, eq2, eq3)
To display the content of a field, the
user has to type the address of the
field.
028
structure_name.field_name
Basic Computer Programming Dr. Deniz Kutluay
Differentiation
Symbolic differentiation can be carried
out by using the diff command.
Either S can be the name of a
previously created symbolic
expression, or an expression can be
typed in for S.
In the diff(S) command, if the
expression contains one symbolic
variable, the differentiation is carried
out with respect to that variable.
029
Basic Computer Programming Dr. Deniz Kutluay
Differentiation
If the expression contains more than
one variable, the differentiation is
carried out with respect to the default
symbolic variable.
In the diff(S,var) command the
differentiation is carried out with
respect to the variable var.
The second or higher (nth) derivative
can be determined with the diff(S,n)
or diff(S,n,var) command, where n is
a positive number.
030
Basic Computer Programming Dr. Deniz Kutluay
Integration
Symbolic integration can be carried out For indefinite integration
by using the int command. The
command can be used for determining
indefinite integrals and definite
integrals.
Either S can be the name of a previously
created symbolic expression, or an
expression can be typed in for S.
In the int(S) command, if the expression
contains one symbolic variable, the
integration is carried out with respect to
that variable. 031
Basic Computer Programming Dr. Deniz Kutluay
Integration
For indefinite integration
If the expression contains more than
one variable, the integration is carried
out with respect to the default symbolic
variable.
In the int(S,var) command, the
integration is carried out with respect to
the variable var.
032
Basic Computer Programming Dr. Deniz Kutluay
Integration
For definite integration
a and b are the limits of integration.
The limits can be numbers or symbolic
variables.
For example, determination of the
definite integral with MATLAB:
033
Basic Computer Programming Dr. Deniz Kutluay
Solving an ordinary
differential equation
An ordinary differential equation (ODE)
can be solved symbolically with the A first-order ODE A second-order ODE
dsolve command.
The command can be used to solve a
single equation or a system of
equations. A solution is a function y = f(t) that satisfies the equation.
Only single equations will be learned in
this course.
034
Basic Computer Programming Dr. Deniz Kutluay
Solving an ordinary
differential equation
General solution:
The solution can be general or particular.
A general solution contains constants.
eq is the equation to be solved. It has to be
In a particular solution the constants are typed as a string even if the variables are
determined. symbolic objects.
In the dsolve('eq') command the independent
variable is assumed by MATLAB to be t (default).
In the dsolve('eq', ‘var') command the user
defines the independent variable by typing it for
var (as a string).
035
Basic Computer Programming Dr. Deniz Kutluay
Solving an ordinary
differential equation
In specifying the equation the letter D A second derivative is typed as D2, third
denotes differentiation. If y is the derivative as D3, and so on. For example, the
dependent variable and t is the equation
𝑑𝑦
independent variable, Dy stands for 𝑑𝑡 .
For example, the equation
is typed in as
is typed in as
036
Basic Computer Programming Dr. Deniz Kutluay
Solving an ordinary
differential equation
The variables in the ODE equation that is A general solution of the first-order ODE
typed in the dsolve command do not
have to be previously created symbolic
variables.
In the solution MATLAB uses C1, C2, C3,
and so on, for the constants of
integration.
037
Basic Computer Programming Dr. Deniz Kutluay
Solving an ordinary
differential equation
The variables in the ODE equation that is
typed in the dsolve command do not
have to be previously created symbolic A general solution of the second-order ODE
variables.
In the solution MATLAB uses C1, C2, C3,
and so on, for the constants of
integration.
038
Basic Computer Programming Dr. Deniz Kutluay
Solving an ordinary
differential equation
The following examples illustrate the
solution of differential equations that
contain symbolic variables in addition
to the independent and dependent
variables
039
Basic Computer Programming Dr. Deniz Kutluay
Solving an ordinary
differential equation
A particular solution of an ODE can be Particular solution:
obtained if boundary (or initial)
conditions are specified.
For solving equations of higher order,
additional boundary conditions have
to be entered in the command.
If the number of conditions is less than
the order of the equation, MATLAB
returns a solution that includes
constants of integration (C1, C2, C3,
and so on). 040
Basic Computer Programming Dr. Deniz Kutluay
Solving an ordinary
differential equation
Particular solution:
The boundary conditions are typed in as
strings in the following:
For example, the first-order ODE with the initial condition
The argument 'var' is optional and is
used to define the independent variable
in the equation. If none is entered, the
default is t.
041
Basic Computer Programming Dr. Deniz Kutluay
Solving an ordinary
differential equation
Particular solution:
The second-order ODE with the initial
conditions
042
Basic Computer Programming Dr. Deniz Kutluay
Plotting symbolic
expressions
Plotting a symbolic expression S that
contains one or two variables can be
done by using ezplot command.
S can be the name of a previously
created symbolic expression, or an
expression can be typed in for S.
It is also possible to type the expression
to be plotted as a string without having
the variables in the expression first
created as symbolic objects.
043
Basic Computer Programming Dr. Deniz Kutluay
Plotting symbolic
expressions
If S has one symbolic variable, a plot of
S(var) versus (var) is created, with the
values of var (the independent variable)
on the horizontal axis, and the values of
S(var) on the vertical axis.
044
Basic Computer Programming Dr. Deniz Kutluay
Plotting symbolic
expressions
If the symbolic expression S has two
symbolic variables, var1 and var2, the
expression is assumed to be a function
with the form S(varl,var2) = 0.
MATLAB creates a plot of one variable
versus the other variable.
The variable that is first in alphabetic
order is taken to be the independent
variable.
045
Basic Computer Programming Dr. Deniz Kutluay
Plotting symbolic
expressions
The ezplot command can also be used
to plot a function that is given in a
parametric form.
In this case two symbolic expressions,
S1 and S2, are involved, where each
expression is written in terms of the
same symbolic variable.
The command creates a plot of S2(var)
versus S1(var).
046
Basic Computer Programming Dr. Deniz Kutluay
Plotting symbolic
expressions
In the ezplot(S1, S2) command the
domain of the independent variable is
0 < var < 2π (default domain).
In the ezplot(S1, S2, [min, max])
command the domain for the
independent variable is defined by min
and max:
min< var <max.
047
Basic Computer Programming Dr. Deniz Kutluay
Numerical calculations
with symbolic
expressions
There may be a need to substitute 1- Substituting a numerical value for one symbolic
variable:
numbers for the symbolic variables and
calculate the numerical value of the A numerical value (or values) can be substituted for one
expressions. symbolic variable when a symbolic expression has one
or more symbolic variables.
This can be done by using the subs
command.
The subs command has several forms
and can be used in different ways.
048
Basic Computer Programming Dr. Deniz Kutluay
Numerical calculations
with symbolic
expressions
The value of S is calculated for each
value of number and the result is
assigned to R, which will have the same
size as number.
049
Basic Computer Programming Dr. Deniz Kutluay
Numerical calculations
with symbolic
expressions
If S has one variable, the output R is
numerical.
If S has several variables and a
numerical value is substituted for only
one of them, the output R is a symbolic
expression.
050
Basic Computer Programming Dr. Deniz Kutluay
Numerical calculations
with symbolic
expressions
The variables are typed as a cell array 2- Substituting a numerical value for two or more
symbolic variables:
(inside curly braces { } ).
A numerical value (or values) can be substituted for two
A cell array is an array of cells where or more symbolic variables when a symbolic expression
each cell can be an array of numbers or has several symbolic variables.
text.
The first cell in the numbers cell array
(number1) is substituted for the variable
that is in the first cell of the variable cell
array (var1), and so on.
051
Basic Computer Programming Dr. Deniz Kutluay
Numerical calculations
with symbolic
expressions
If all the numbers that are substituted
for variables are scalars, the outcome
will be one number or one expression (if
some of the variables are still symbolic).
052
Basic Computer Programming Dr. Deniz Kutluay
Numerical calculations
with symbolic
expressions
If, for at least one variable, the
substituted numbers are an array, the
mathematical operations are executed
element-by-element and the outcome is
an array of numbers or expressions.
It should be emphasized that the
calculations are performed element-by-
element even though the expression S is
This also means that all the arrays substituted for
not typed in the element-by-element different variables must be of the same size.
notation.
053
Basic Computer Programming Dr. Deniz Kutluay
Numerical calculations
with symbolic
expressions
A second method for substituting
numerical values for symbolic variables
is to first assign numerical values to the
variables and then use the subs
command.
Once the symbolic variables are
redefined as numerical variables they
can no longer be used as symbolic.
054
Thank
You
Dr. Deniz Kutluay
Electrical-Electronics Engineering
Design Architect at Vestel
[Link]@[Link]
[Link]@[Link]