Title: MATLAB Basics Symbolic math
1MATLAB Basics - Symbolic math
- Engineering mathematics, Week 4
Neural Signal Processing Laboratory at Yonsei BME
2Algebra
- Symbolic Math ToolboxUsing MATLAB's Symbolic
Math Toolbox, you can carry out algebraic or
symbolic calculation, such as factoring
polynomials or solving algebraic equations - To perform symbolic computations, you must use
syms to declare the variables you plan to use to
be symbolic variables -
syms x y (x - y) (x - y)2 ans (x -
y)3 expand(ans) ans x3-3x2y3xy2-y3
factor(ans) ans (x-y)3
3Simplify, Simple
- MATLAB has a command called simplify, which you
can sometimes use to express a formula as simply
as possible
syms x y simplify((x3 - y3) / (x - y)) ans
x2xyy2
- MATLAB has a more robust command, called simple,
that sometimes does a better job than simplify.
syms x y sin(x) cos(y) cos(x) sin(y)
simple(ans) ans sin(x y)
4Symbolic expressions, variable precision, and
exact arithmetic (i)
- MATLAB uses floating point arithmetic for its
calculations. Using the Symbolic Math Toolbox,
you can also do exact arithmetic with symbolic
expressions.
cos(pi / 2) ans 6.1232e-017
- The answer is written in floating point format
and means 6.1232 x 10-17. However, we know that
cos(p/2) is really equal to 0. The inaccuracy is
due to the fact that typing pi in MATLAB gives an
approximation to p accurate to about 15 digits,
not its exact value.
5Symbolic expressions, variable precision, and
exact arithmetic (ii)
- To compute an exact answer, instead of an
approximate answer, we must create an exact
symbolic representation of p/2 by typing
sym('pi/2')
cos(sym('pi / 2')) ans 0 This is the
expected answer !
Another example
1/2 1/3 ans 0.8333
sym('1/2') sym('1/3') ans 5/6
6Symbolic expressions, variable precision, and
exact arithmetic (iii)
- Finally, you can also do variable-precision
arithmetic with vpa. For example, to print 50
digits of , type
vpa('sqrt(2)', 50) ans 1.41421356237309504880
16887242096980785696718753769
7Solving equations (i)
- You can solve equations involving variables with
solve or fzero. For example, to find the
solutions of the quadratic equation
, type
solve('x2 - 2x - 4 0') ans 5(1/2)1
1-5(1/2)
- Note that the equation to be solved is specified
as a string. The answer consists of the exact
(symbolic) solutions . To get
numerical solutions, type double(ans) or vpa(ans)
to display more digits
8Solving equations (ii)
- The command solve can solve higher degree-degree
polynomial equations, as well as many other types
of equations.
x, y solve('x2 - y 2', 'y - 2x 5') x
122(1/2) 1-22(1/2) y
742(1/2) 7-42(1/2) Note that the answer
is symbolic representation.
9Solving equations (iii)
- Some equations cannot be solved symbolically, and
in these cases solve tries to find a numerical
answer.
solve('sin(x) 2 - x') ans
1.1060601577062719106167372970301
- Sometimes there is more than one solution, and
you may not get what you expected.
solve('exp(-x) sin(x)') ans
-2.0127756629315111633360706990971 2.703074511
5909622139316148044265i The answer is a
complex number
10Solving equations (iv)
- Though it is a valid solution of the equation,
there are also real number solutions (red arrows).
fzero(inline('exp(-x) - sin(x)'), 0.5) ans
0.5885 fzero(inline('exp(-x) - sin(x)'), 3) ans
3.0964