Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

EXPERIMENT 09

Introduction to Symbolic Mathematics in MATLAB

Objectives:
• Familiarizing students with the symbolic math toolkit in MATLAB

Equipment required:
• MATLAB installed on PCs

Background Knowledge:
The Symbolic Math Toolbox is a useful tool to help you do symbolic mathematical analysis. It is convenient
and the results are more presentable. Symbolic Math Toolbox provides functions for solving, plotting, and
manipulating symbolic math equations. You can create, run, and share symbolic math code using the
MATLAB Live Editor. The toolbox provides functions in common mathematical areas such as calculus,
linear algebra, algebraic and ordinary differential equations, equation simplification, and equation
manipulation. Symbolic Math Toolbox lets you analytically perform differentiation, integration,
simplification, transforms, and equation solving. You can perform dimensional computations and
conversions using SI and US unit systems. Your computations can be performed either analytically or using
variable-precision arithmetic, with the results displayed in mathematical typeset.
We will use the Symbolic Toolkit in MATLAB to perform differentiation, integration(indefinite integrals
and definite integrals) and substitution. We will also plot equations and functions to find solution of
equations (graphical approximations, single equations, multiple simultaneous equations, non-linear
equations).
The first step is to tell MATLAB that the variables to be used are symbolic. This is most easily done using
the “syms” command. Symbolic objects are essentially strings. The command syms x creates a symbolic
object called x which has the corresponding string representation x. For example, in the following code we
will use x, y, a, b and c as symbolic quantities.

Use the whos command in MATLAB to check the class of these variables.

Note that these variables are symbolic, rather than the usual double precision numeric variables.
Differentiation:
The MATLAB symbolic toolbox is useful for solving simple calculus problems. For example, to find the
derivative of function f with respect to x, where f = 𝑒 −𝑎𝑥 ∗ (−𝑏𝑥) and a, b are unspecified constants, we
write the following code:

The result obtained, however, is quite complex and needs simplification. Use MATLAB to simplify it by
typing:

MATLAB uses several algorithms for algebraic simplification and chooses the shortest of results as a final
answer. It is possible that for some complex expressions, no simpler expression will be found.
𝑑2 𝑓
MATLAB can also take higher order differentials. For example, to find for the already defined function
𝑑𝑥 2
f, type:

To differentiate an expression that contains more than one symbolic variable, specify the variable with
respect to which the expression should be differentiated. The diff command then calculates the partial
derivative of the expression with respect to that variable.
Integration:
The MATLAB symbolic toolbox is also useful for solving complex integral problems. Integration can be
of the following two types:

• Definite
• Indefinite

Symbolic Toolkit can solve indefinite integrals and simplify them as well as solve the definite integrals
over a certain limit.
Indefinite Integrals:

To find an indefinite integral of a function f with respect to variable x, where f = 𝑒 −2𝑐𝑥 ∗ sin⁡(3𝑎𝑥), type:

Check the answer, by differentiating the result.

The ‘diff’ command should return g, but the result doesn’t look the same as g. This is because MATLAB
doesn’t always give things in the simplest format. In this case, we again use the simple command.

Note that MATLAB does not add the constant of integration (“C”) customarily shown for indefinite
integrals.
Definite Integrals:
+𝜋
To find definite integral, i.e., within specified limits, of a function f with respect to variable x ( ∫−𝜋 𝑓𝑑𝑥 )
type:

Sometimes, MATLAB is unable to find a symbolic solution for an integral, such as with:

A numerical solution can nevertheless be found using the “quad” command.


Create the function to be integrated in the MATLAB editor and save it to your Current Directory, which
must also be in the path (File / Set Path).
For example, for our exp(sin(x)) function, enter the following script into the editor and save as func1.m.

Then, type:
Substitution:
Sometimes, it is required to substitute one value or parameter with another in an expression. ‘subs’
command is used to perform this function. To substitute a with 3 and c with 2 in int_f, type:

You may get a different result if you assign the values to the variables before an integration or differentiation
is performed. Try it in the above example and see what happens. In this case, with a definite integral, it’s
not serious because the only difference is the form of the result. For an indefinite integral, substituting for
the variable of integration (e.g., x) prior to integration would give the wrong result altogether.
Conversion to a double precision numeric variable:
Often MATLAB's symbolic engine produces a symbolic answer, as in:
where  is retained in symbolic form (pi). Irrational numbers may also result, e.g. 4/3 rather than 1.3333.
For use in subsequent numerical calculations, convert symbolic results into numeric “double” variables by
using the “double” command. Use subs command to give values to the constants, then type:

This command gives the results in numeric form.


Graphing Equations and Functions:
MATLAB’s symbolic engine has commands for plotting symbolic equations and functions. These
commands start with “ez.” . We use “ezplot” command if we want to plot in two-dimensions.

For example, to plot the equation for a circle of radius 5, 𝑥 2 ⁡ + ⁡ 𝑦 2 = 25, follow the following steps:

1. Move everything to the left-hand side of the equation, i.e., 𝑥 2 ⁡ + ⁡ 𝑦 2 − ⁡25 = 0.


2. Define the variables, in our case x & y, as symbolic variables.
3. Define a variable equal to the left-hand side of the equation, inside single straight quotation marks.
4. Use ezplot command to plot the equation.

Declare x and y as symbolic objects. Type the given code, to plot the circle for 𝑥 2 ⁡ + ⁡ 𝑦 2 = 25.

The [-6,6,-5,5] defines the axes for the plot. It tells MATLAB that the x scale is to go from -6 to +6, and
the y scale is to go from -5 to 5.
Solution of equations:
The symbolic toolbox in MATLAB can solve many types of equations, including non-linear ones and
several simultaneous equations. The ‘solve’ command is used to find the solution of equations in MATLAB.
The steps in solving one or more equations using “solve” are:
1. Define the variables in the equations as symbolic using the “syms” command.
2. Define the equations.
3. Solve the equations using the “solve” command.
4. If there is more than one solution, decide which is physically reasonable and select it.
5. Check the solution by substituting it back into the original equation(s).
For example, to find the two solutions to the classic quadratic equation 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0, one can follow
either of the given 3 methods:

Note that single quotation marks must be placed around an equation that is assigned to a variable.

‘pretty’ command is used in MATLAB to transform the result in a more readable way.
If you don't specify the right side of the equation, solve assumes that the expression equals zero. So, here
we could have used:

The results in the command window are:

A similar procedure can be used to solve simultaneous linear and non-linear equations.
Example:
Find the solution to the following set of linear equations:
6𝑥 + 9𝑦 + 4𝑧 = 5
3𝑦 + 2𝑧 + 9𝑥 = 10
3𝑧 + 3𝑥 + 5𝑦 = 2
To find the solution to the given set of equation, type the following code in the script file:

The results in the command window are:


Notice that we did not have to enter x, y and z in any particular order in defining the equation variables.
However, the variables in the output are in alphabetical order, regardless of how they are written in the
command. So, take care to list the variables in the brackets in alphabetical order. If you use [z,x,y] rather
than [x,y,z] in the above example the result produced for z is actually x, that for x is actually y, and that for
y is actually z.
Symbolic Toolbox is also used to solve simultaneous non-linear equations. To solve simultaneous non-
linear equations, i.e., 𝑦 = 2𝑒 2𝑥 and 𝑦 = 6 − ⁡ 𝑥 3 , type the following code:

The results in the command window are:

We can also obtain an approximate solution by plotting these two equations and looking at their
intersections.
We first move everything to the left-hand side of the equations, since ‘ezplot’ plots expressions and not
equations.

The results in the command window are:

‘ezplot’ results in the following figure:


The "hold on" command tells MATLAB to write the following plots on top of the first one, rather than
replacing it. Approximate values of the solutions (intersections) can be obtained in the Figure window by
clicking on Tools / Zoom In, clicking on a desired intersection to enlarge that area of the graph, clicking on
Tools / Data Cursor, and then clicking on that intersection again to give the approximate values of x and y
there.

Lab Tasks:
1. Differentiate the following expression:
𝑒 −2𝑏𝑦 ∗ 𝑦 4𝑎 ∗ cos(𝑐𝑦)
Simplify your answer.
2. Integrate the given expression:
+𝜋
∫ [𝑒−2𝑏𝑦 ∗ cos(𝑐𝑦)] 𝑑𝑦
−𝜋
Substitute b = 3 and c = 5 after integration to get the results.
3. Integrate the following expression:
cos(𝑎 ∗ 𝑦)
Evaluate the numeric results for fixed variables after integration.
4. Take a vector from x=-5:5 and plot e(3x-6) in script file.
Now repeat the same by using Symbolic Mathematics and plot by using ezplot command.
5. Use MATLAB to solve the following systems of simultaneous equations:

6x - 3y + 8z =10
-9x +7y +3z = 2
x +2y +6z = -5
Conclusion:

You might also like