Real and Complex Numbers
3.1 Rational Numbers
A rational number is a number of the form p/q, where p and q are integers. That is, the rational numbers are those numbers that can be represented as the quotient of two integers. The way in which MATLAB treats rational numbers is different from the majority of calculators.
If we ask a calculator to calculate the sum 2/4 + 24/144, most will return something like 0.6666667, which is no more than an approximation of the result.
MATLAB can work with rational numbers in exact mode, so the result of arithmetic expressions involving rational numbers is always another rational number. To this end, it is necessary to activate the rational format with the command format rat. However, if the reader so wishes, MATLAB can also return decimal approximations of rational numbers, activating any other type of format (e.g. format short or format long). MATLAB evaluates rational expressions in exact mode as follows:
>> format rat
>> 2/4 + 24/144
ans =
2/3
By dealing with rational numbers exactly as ratios of integers, rounding errors are not introduced in calculations with fractions, which can become very serious, as evidenced by the theory of errors. Note that, once the rational format is enabled, when MATLAB is asked to add two rational numbers, it returns a rational number as a ratio of integers and thus represents it symbolically, as you can see in the following examples:
>> (2/3-3/5) * 5/2
ans =
1/6
>> 2 + 2/5
ans =
12/5
In the second example, the number 12/5 is exactly equivalent in value to 2.4, but MATLAB represents these numbers in different ways.
Once the rational format is enabled, operations with rational numbers will be exact until a different format is introduced, in which case we can also obtain decimal approximations.
A floating point or decimal number is interpreted as exact if the rational format has been enabled. If there is a number with a floating point expression, MATLAB will treat it as an exact expression and represent it as a rational number. If an irrational number appears in a rational expression, MATLAB will approximate it by a fraction and then work with it in rational form.
>> 1/2 + 2.4/144
ans =
31/60
>> .5/7 + pi
ans =
5626/1751
Another way to work with accurate results with rational numbers, without having to enable the rat format, is to use the command simplify. Using this command allows you to work with rational expressions exactly, even if the expressions contain irrational numbers. The use of this command requires that the numeric expressions be considered as symbolic, so you will need to prepend to all numeric expressions the command sym.
>> simplify(sym(2/5+3/4))
ans =
23/20
>> simplify(sym(2/5+3/4+pi))
ans =
2415951884441619/562949953421312
>> simplify(sym(2/5+3/4)+pi)
ans =
23/20 + pi
There are certain commands that can be used when working with rational numbers. Among the most important are the following:
Here are some examples:
>> simplify (sym(125/1500))
ans =
1/12
>> maple('simplify(125/1500)')
ans =
1/12
>> maple('normal(125/1500)')
ans =
1/12
>> [n,m]=numden(sym(125/1500))
n =
1
m =
12
>> maple('denom(125/1500)')
ans =
12
>> maple('numer(125/1500)')
ans =
1
>> maple('convert(125.1500,fraction)')
ans =
2503/20
>> maple('convert(125.1500,rational)')
ans =
2503/20
EXERCISE 3-1
Perform the following operations with rational numbers:
a) 3/5+2/5+7/5
b) 1/2+1/3+1/4+1/5+1/6
c) 1/2-1/3+1/4-1/5+1/6
d) (2/3-1/6)-(4/5+2+1/3)+(4-5/7)
e) ((1/5*4/7)/(4/3-2/5))/(6-(5/9-1/7)*5+1/4)
f) ((-1)4 /(2/5)3)-2 / ((-3/7)2 -(2/5)-1)
g) [(2-1/5)2 /(3-2/9)-1]/[(6/7*5/4-(2/7)/(1/2))]3 /(1/2-1/3)
a)
>> format rat
>> 3/5+2/5+7/5
ans =
12/5
b)
>> 1/2+1/3+1/4+1/5+1/6
ans =
29/20
c)
>> 1/2-1/3+1/4-1/5+1/6
ans =
23/60
d)
>> (2/3-1/6)-(4/5+2+1/3)+(4-5/7)
ans =
137/210
e)
>> (1/5*4/7/(4/3-2/5))/(6-(5/9-1/7)*5+1/4)
ans =
216/7385
f)
>> ((-1)^4/(2/5)^3)^(-2)/((-3/7)^2-(2/5)^(-1))
ans =
-53/29972
g)
>> (2-1/5)^2/(3-2/9)^(-1)/(6/7*5/4-(2/7)/(1/2))^3/(1/2-1/3)
ans =
432
Alternatively, the operations can be performed as follows:
>> simplify(sym(3/5+2/5+7/5))
ans =
12/5
>> simplify(sym(1/2+1/3+1/4+1/5+1/6))
ans =
29/20
>> simplify(sym(1/2-1/3+1/4-1/5+1/6))
ans =
23/60
>> simplify(sym((2/3-1/6)-(4/5+2+1/3)+(4-5/7)))
ans =
137/210
>> simplify(sym((1/5*4/7/(4/3-2/5))/(6-(5/9-1/7)*5+1/4)))
ans =
216/7385
>> simplify(sym((-1)^4/(2/5)^3)^(-2)/((-3/7)^2-(2/5)^(-1)))
ans =
53/29972
>> simplify(sym((2-1/5)^2/(3-2/9)^(-1)/(6/7*5/4-(2/7)/(1/2))^3/(1/2-1/3)))
ans =
432
EXERCISE 3-2
Perform the following rational operations:
a) 3/a+2/a+7/a
b) 1/2a+1/3a+1/4a+1/5a+1/6a
c) 1/2a+1/3b+1/4a+1/5b+1/6c
To treat operations with expressions that contain the symbolic variable a, it is necessary to prepend the command syms a to declare the variable a as symbolic, and then use simplify. The commands normal, maple and simplify can also be used.
a)
>> syms a
>> simplify(3/a+2/a+7/a)
ans =
12/a
>> 3/a+2/a+7/a
ans =
12/a
>> maple('3/a+2/a+7/a')
ans =
12/a
b)
>> 1/(2*a)+1/(3*a)+1/(4*a)+1/(5*a)+1/(6*a)
ans =
29/20/a
c)
>> syms a b c
>> 1/(2*a)+1/(3*b)+1/(4*a)+1/(5*b)+1/(6*c)
ans =
1/60*(45*b*c+32*a*c+10*a*b)/a/b/c
EXERCISE 3-3
Simplify the following rational expressions as much as possible:
a) (1-a9)/(1-a3)
b) 1/2a+1/3a+1/4a+1/5a+1/6a
c) 1/2a+1/3b+1/4a+1/5b+1/6c
d) (3a+2a+7a)/(a3 +a)
e) 1/(1+a)+1/(1+a)2 +1/(1+a)3
f) 1+a/(a+b)+a2 /(a+b)2
a)
>> syms a
>> simplify((1-a^9)/(1-a^3))
ans =
a^6+a^3+1
>> maple('simplify((1-a^9)/(1-a^3))')
ans =
a^6+a^3+1
>> maple('normal((1-a^9)/(1-a^3))')
ans =
a^6+a^3+1
b)
>> simplify((1/2)*a+(1/3)*a+(1/4)*a+(1/5)*a+(1/6)*a)
ans =
29/20*a
c)
>> syms a b c
>> [n, d] = numden (1 /(2*a) + 1 /(3*b) + 1 /(4*a) + 1 /(5*b) + 1 /(6*c))
n =
45 * b * c + 32 * a * c + 10 * a * b
d =
60 * a * b * c
The result is an algebraic fraction whose numerator is n and whose denominator is d.
d)
>> simplify ((3*a+2*a+7*a) /(a^3+a))
ans =
12 /(a^2+1)
e)
>> maple ('normal (1 /(1+a) + 1 /(1+a) ^ 2 + 1 /(1+a) ^ 3)')
ans =
(3 + 3 * a + a ^ 2) /(1+a) ^ 3
f)
>> maple('simplify(1+a/(a+b)+a^2/(a+b)^2)')
ans =
(3 * a ^ 2 + 3 * a * b + b ^ 2) / (a + b) ^ 2
3.2 Continued Fractions
MATLAB enables you to work with continued fractions using the following commands. With the exception of the first and last commands, these can be found in Maple’s numtheory library:
Here are some examples:
>> rat(3/5)
ans =
1 + 1/(-3 + 1/(2))
>> maple('cfrac(3/5)')
ans =
1/(1+1/(1+1/2))
>> pretty(sym(maple('cfrac(3/5)')))
1
-----------
1
1 + -------
1 + 1/2
>> pretty(sym(maple('cfrac(125.1500)')))
1
125 + -----------
1
6 + -------
1 + 1/2
>> maple('convert(125.1500, confrac, 4)')
ans =
[125, 6, 1, 2]
>> maple nthnumer([125, 6, 1, 2],3)
ans =
2503
>> maple nthdenom([125, 6, 1, 2],3)
ans =
20
EXERCISE 3-4
Express the following numbers as continued fractions:
a) 7/9
b) the roots of the polynomial x6 - x 5 - 6x4 + 6x3 + 8x2 - 8x + 1
c) the roots of the polynomial - 117260219 x6 + 139540883 x5 + 17033080 x4 + 800302 x3 + 18628 x2 x + 216 + 1
d) 11/9999997
e) 311/2
>> maple('with(numtheory)');
>> maple cfrac(7/9)
ans =
1/(1+1/(3+1/2))
>> pretty(sym(maple('cfrac(7/9)')))
1
-----------
1
1 + -------
3 + 1/2
>> maple('cfracpol(x^6 - x^5 - 6*x^4 + 6*x^3 + 8*x^2 - 8*x + 1)')
ans =
[-2, 44, 1, 3, 3, 1, 1, 1, 3, 2, 3, ... ],
[-2, 1, 1, 6, 1, 7, 34, 1, 12, 1, 5, ...],
[0, 6, 1, 2, 4, 3, 1, 1, 3, 1, 63, ... ],
[0, 1, 2, 1, 2, 2, 16, 1, 1, 5, 11, ... ],
[1, 1, 1, 1, 7, 6, 10, 2, 29, 20, 1, ...],
[1, 1, 10, 3, 1, 13, 1, 1, 3, 1, 4, ... ]
>> maple('cfracpol( -117260219*x^6+139540883*x^5 + 17033080*x^4 + 800302*x^3 +18628*x^2+216*x+1)')
ans =
[-1, 1, 41, 7, 1, 7, 34, 1, 12, 1, 5, ...],
[-1, 1, 42, 1, 1, 6, 1, 2, 4, 3, 1, ... ],
[-1, 1, 42, 1, 1, 1, 2, 1, 2, 2, 16, ... ],
[-1, 1, 42, 1, 2, 1, 1, 1, 7, 6, 10, ... ],
[-1, 1, 42, 1, 2, 1, 10, 3, 1, 13, 1, ...],
[1, 3, 3, 1, 1, 1, 3, 2, 3, 4, 1, ... ]
>> pretty(sym(maple('cfrac(11/9999997)')));
ans =
1
------------------------
1
909090 + ---------------
1
1 + -----------
1
1 + -------
1 + 1/3
>> pretty(sym(maple('cfrac(31^(1/2))')));
ans =
1
5 + --------------------------------------------
1
1 + ----------------------------------------
1
1 + ------------------------------------
1
3 + --------------------------------
1
5 + ----------------------------
1
3 + ------------------------
1
1 + --------------------
1
1 + ----------------
1
10 + -----------
1
1 + -------
1 + ...
3.3 Irrational Numbers
Irrational numbers, because of their special nature, have always created difficulties in numerical calculations. The impossibility of representing an irrational accurately in numeric mode (using ten decimal digits, for example) is the cause of most of the problems. MATLAB can represent the results with greater accuracy or to any accuracy specified by the user. Nevertheless, by definition, an irrational number cannot be represented exactly as the ratio of two integers. If ordered to find the square root of 17, MATLAB will return 4.12310562561766. However, the result can be treated symbolically using the maple command, so that sqrt[17] will be represented by 17 ^(1/2):
>> sqrt(17)
ans =
4.12310562561766
>> maple('sqrt(17)')
ans =
17 ^(1/2)
Note that if the square root of a floating point (decimal) number, e.g. √17.0, is requested, MATLAB always calculates an approximate result, even using the command maple.
>> maple ('sqrt (17.0)')
ans =
4.123105625617661
The main difficulty presented by the treatment of irrational numbers is the impossibility of representing them accurately, unless they are treated as symbolic constants, so that if it is asked to calculate the sum of the square root of 2 and the square root of 3 (using the function sqrt), MATLAB will return as a result 2^(1/2) + 3^(1/2), since this is the only way to accurately represent the sum (using the command maple). In all subsequent calculations involving 2^(1/2) and 3^(1/2) they will be operated on symbolically following the mathematical rules of calculation with radicals.
Of course we can also work with decimal approximations of irrational numbers, as we pointed out above.
Among the commands used to work with irrational numbers, we have the following:
EXERCISE 3-5
Perform the following operations with irrational numbers:
a) 3 + 2 - 58 + 7
b) + 3 - /2
c) 4a1/3 - 3b1/3 - 5a1/3 - 2b1/3 + ma1/3
d) + - -
e)
f)
g) (3a2 b5)1/3 (9a3 b2)1/4 (3ab)1/6
h)
a)
First, we use the command simplify:
>> syms a
>> simplify(3*sqrt(a) + 2*sqrt(a) - 5*sqrt(a) + 7*sqrt(a))
ans =
7 * a ^(1/2)
We can also use different variants of the maple(simplify), maple and maple(combine) commands, which yields a completely simplified result:
>> maple('simplify(3*sqrt(a) + 2*sqrt(a) - 5*sqrt(a) + 7*sqrt(a))')
ans =
7*a^(1/2)
>> maple('simplify(3*sqrt(a) + 2*sqrt(a) - 5*sqrt(a) + 7*sqrt(a),radical)')
ans =
7*a^(1/2)
>> maple('combine(3*sqrt(a) + 2*sqrt(a) - 5*sqrt(a) + 7*sqrt(a),sqrt(a))')
ans =
7 * a ^(1/2)
b)
We can perform numerical operations directly in MATLAB, obtaining decimal results either to default accuracy or to a predefined accuracy:
>> sqrt (2) + 3 * sqrt (2) - sqrt (2) / 2
ans =
4.9497
We can consider the calculation as an algebraic expression, in which case the result obtained is not fully simplified:
>> sym(sqrt(2)+3*sqrt(2)-sqrt(2)/2)
ans =
sqrt(49/2)
We can apply the command simplify to completely simplify the result:
>> simplify(sym(sqrt(2)+3*sqrt(2)-sqrt(2)/2))
ans =
7/2 * 2 ^(1/2)
We can use different variants of the commands maple(simplify) and maple(combine), in which case we also obtain a completely simplified result:
>> maple('simplify(sqrt(2)+3*sqrt(2)-sqrt(2)/2)')
ans =
7/2*2^(1/2)
>> maple('simplify(sqrt(2)+3*sqrt(2)-sqrt(2)/2,radical)')
ans =
7/2*2^(1/2)
>> maple('combine(sqrt(2)+3*sqrt(2)-sqrt(2)/2,power)')
ans =
7/2*2^(1/2)
c)
>> syms a b m
>> simplify(4*a^(1/3)- 3*b^(1/3)-5*a^(1/3)- 2*b^(1/3)+m*a^(1/3))
ans =
-a^(1/3)-5*b^(1/3)+m*a^(1/3)
>> maple('simplify(4*a^(1/3)- 3*b^(1/3)-5*a^(1/3)- 2*b^(1/3)+m*a^(1/3))')
ans =
-a^(1/3)-5*b^(1/3)+m*a^(1/3)
>> maple('simplify(4*a^(1/3)-3*b^(1/3)-5*a^(1/3)-2*b^(1/3)+m*a^(1/3),radical)')
ans =
-a^(1/3)-5*b^(1/3)+m*a^(1/3)
>> maple('combine(4*a^(1/3)-3*b^(1/3)-5*a^(1/3)- b^(1/3)+m*a^(1/3),radical)'))
ans =
-a^(1/3)-5*b^(1/3)+m*a^(1/3)
d)
If we consider the operation directly, we do not get the exact result:
>> sym(sqrt(432)+sqrt(75)-sqrt(363)-sqrt(108))
ans =
2 ^(-48)
If we use the command maple(‘simplify’), the exact result is obtained.
>> maple('simplify(sqrt(432)+sqrt(75)-sqrt(363)-sqrt(108))')
ans =
0
e)
>> maple('simplify(sqrt(3*a)*sqrt(27*a))')
ans =
9 * a
or also:
>> simplify(sym (sqrt(3*a) * sqrt(27*a)))
ans =
9*a
f)
>> simplify(a^(1/2)*a^(1/3))
ans =
a^(5/6)
g)
>> maple('simplify((3*a^2*b^5)^(1/3)*(9*a^3*b^2)^(1/4)*(3*a*b)^(1/6),symbolic)')
ans =
3*a^(19/12)*b^(7/3)
h)
>> maple('radsimp(sqrt(a*(a^(1/5))))')
ans =
a ^(3/5)
EXERCISE 3-6
Simplify the following irrational expressions by rationalizing the denominators:
a) b) c) d) e)
In these cases of rationalization, the simple use of the command simplify solves the problem. You can also use the command radsimp.
a)
>> simplify (sym (2/sqrt (2)))
ans =
2^(1/2)
b)
>> maple('simplify( sqrt(2)/2^(1/3))')
ans =
2^(1/6)
c)
>> maple('simplify(2/4^(1/3))')
ans =
2^(1/3)
d)
>> simplify(sym(3/sqrt(3)))
ans =
3 ^(1/2)
e)
>> maple ('simplify(a/sqrt(a), radical)')
ans =
a ^(1/2)
EXERCISE 3-7
Simplify the following expression:
>> maple('readlib(radnormal):radnormal(sqrt(6-2*sqrt(2)- 2*sqrt(3)+2*sqrt(6)))')
ans =
-1 + 2 ^(1/2) + 3 ^(1/2)
EXERCISE 3-8
Rationalize the following expressions:
>> maple('readlib(rationalize):rationalize(1/(2+sqrt(5)))')
ans =
-2 + 5 ^(1/2)
>> maple('readlib(rationalize):rationalize( 1/(a^(1/2)+b^(1/3)) )')
ans =
(-b^(1/3)+a^(1/2))*(a^2+a*b^(2/3)+b^(4/3))/(a^3-b^2)
>> maple('readlib(rationalize):rationalize(1/(1+sqrt(2)) + 1/(1-sqrt(2)) )')
ans =
-2
>> maple('readlib(rationalize):rationalize( 1/sqrt(1+sqrt(2)) + 1/sqrt(1-sqrt(2)) )')
ans =
-((1-2 ^(1/2)) ^(1/2) + (2 ^(1/2) + 1) ^(1/2)) * (1-2 ^(1/2)) ^(1/2) * (2 ^(1/2) + 1) ^(1/2)
3.4 Algebraic Numbers
MATLAB can operate with algebraic numbers using two types of representations, one in the form of radicals and the other in terms of roots of polynomials (RootOf(polynomial)).
Using the command convert, MATLAB can convert a radical representation to a RootOf representation and vice versa, when possible.
The commands for working with algebraic numbers are as follows:
EXERCISE 3-9
a) Convert the following radical expressions to RootOf expressions:
i)
ii)
b) Perform the reverse conversions of (i) and (ii) above.
a)
i)
>> maple('convert(sqrt(2),RootOf)')
ans =
RootOf(_Z^2-2)
ii)
>> maple('convert((1-2^(1/2))^(1/3),RootOf)')
ans =
RootOf(_Z^3-1+RootOf(_Z^2-2))
b)
>> maple('convert(RootOf(_Z^2-2),radical)')
ans =
2^(1/2)
>> maple('convert(RootOf(_Z^3-1+RootOf(_Z^2-2)),radical)')
ans =
(1-2 ^(1/2)) ^(1/3)
3.5 Real Numbers
The disjoint union of the set of rational numbers and the set of irrational numbers is the set of real numbers. Included in the set of rational numbers is the set of integers, so all functions applicable to real numbers will also be valid for integers, rational and irrational numbers.
3.6 Common Functions with Real Arguments
MATLAB provides a full range of predefined functions, most of which are discussed later in this book. Within the group of functions with real arguments offered by MATLAB, the following are the most important:
Trigonometric functions | ||
---|---|---|
Function | Inverse | |
sin(x) |
asin(x) | |
cos(x) |
acos(x) | |
tan(x) |
atan(x) | |
csc(x) |
acsc(x) | |
sec(x) |
asec(x) | |
cot(x) |
acot(x) | |
atan2(x) (inverse tangent in the fourth quadrant) |
||
Hyperbolic functions | ||
Function |
Inverse |
|
sinh(x) |
asinh(x) |
|
cosh(x) |
acosh(x) |
|
tanh(x) |
atanh(x) |
|
csch(x) |
acsch(x) |
|
sech(x) |
asech(x) |
|
coth(x) |
acoth(x) | |
Exponential and logarithmic functions | ||
exp(x) exponential of x to base e (e ^ x) |
||
log(x) logarithm of x to base e |
||
log10(x) base 10 logarithm of x |
||
log2(x) base 2 logarithm of x |
||
pow2(x) power to base 2 of x |
||
sqrt(x) square root of x |
||
Specific functions of a numeric variable | ||
abs(x) the absolute value of the real number x |
||
floor(x) the greatest integer less than or equal to the real x |
||
ceil(x) the lowest integer greater than or equal to the real x |
||
round(x) the integer closest to the real x |
||
fix(x) removes the decimal part of the real x |
||
rem(a,b) gives the remainder of the division of a by b |
||
sign(x) gives the sign of the real x (1 if x > 0, - 1 if x < 0) |
First, we see how the function round rounds a real number:
>> round(2.574)
ans =
3
>> round(2.4)
ans =
2
>> round(sqrt(17))
ans =
4
The function ceil is illustrated in the following two cases:
>> ceil (4.2)
ans =
5
>> ceil (4.8)
ans =
5
The floor function is illustrated in the following two examples:
>> floor (4.2)
ans =
4
>> floor (4.8)
ans =
4
The fix function simply removes the decimal part of a real number:
>> fix (5.789)
ans =
5
3.7 Complex Numbers
Operations on complex numbers are well implemented in MATLAB. MATLAB follows the convention that i or j represents the key value in complex analysis, the imaginary number √- 1. All the usual arithmetic operators can be applied to complex numbers, and there are also some specific functions which have complex arguments. Both the real and the imaginary part of a complex number can be a real number or a symbolic constant, and operations with them are always performed in exact mode, unless otherwise instructed or necessary, in which case an approximation of the result is returned. As the imaginary unit is represented by the symbol i or j, the complex numbers are expressed in the form a+bi or a+bj. Note that you don't need to use the product symbol (asterisk) before the imaginary unit:
>> (2-3i)*(1-i)/(-1+2i)
ans =
-1.8000 + 1.4000i
>> format rat
>> (2-3i)*(1-i) /(-1+2i)
ans =
-9/5 + 7/5i
3.8 Common Functions with Complex Arguments
MATLAB performs the usual arithmetic operations with complex numbers, but in addition, there are several features built into the program especially designed to work with complex variables.
The function real(z) returns the real part of the complex number z:
>> real(3 + 4i)
ans =
3
The function imag(z) returns the imaginary part of z:
>> imag(3 + 4i)
ans =
4
The function conj(z) returns the conjugate of z:
>> conj(3 + 4i)
ans =
3 - 4i
The function abs(z) returns the modulus (absolute value) of z:
>> abs(3 + 4i)
ans =
5
The function angle(z) returns the argument of z:
>> angle(3 + 4i)
ans =
0.9273
In addition to these specific functions of a complex variable, there are many other functions that can also be applied to complex numbers.
For example, the function round(z) rounds both the real part and the imaginary part of z:
>> round(2.7-8.4i)
ans =
3.0000 - 8.0000i
There are many more functions in MATLAB that work with complex numbers, including sin, cos, exp, log, etc. Some of the most important are presented in the following table:
Trigonometric functions | |
---|---|
Function | Inverse |
sin(z) |
asin(z) |
cos(z) |
acos(z) |
tan(z) |
atan(z) |
csc(z) |
acsc(z) |
sec(z) |
asec(z) |
cot(z) |
acot(z) |
atan2(z) (inverse tangent in the fourth quadrant) | |
Hyperbolic functions | |
Function |
Inverse |
sinh(z) |
asinh(z) |
cosh(z) |
acosh(z) |
tanh(z) |
atanh(z) |
csch(z) |
acsch(z) |
sech(z) |
asech(z) |
coth(z) |
acoth(z) |
Exponential and logarithmic functions | |
exp(z) exponential of z to base e (e ^ Z) |
|
log(z) base e logarithm of z |
|
log10(z) base 10 logarithm of z |
|
sqrt(z) square root of z |
|
log2(z) base 2 logarithm of z |
|
pow2(z) base 2 power of z |
|
Specific functions for real and imaginary parts | |
floor(z) applies the function floor(z) to real(z) and imag(z) | |
ceil(z) applies the function ceil(z) to real(z) and imag(z) | |
round(z) applies the function round(z) to real(z) and imag(z) | |
fix(z) applies the function fix(z) to real(z) and imag(z) | |
Specific functions of a complex variable | |
abs(z) or maple(‘abs (z)’): modulus (absolute value) of z | |
angle(z) or maple(‘argument(z)’): argument of z | |
conj(z) or maple(‘conjugate (z)’): conjugate of z | |
real(z) or maple(‘Re(z)’): real part of z | |
imag(z) or maple(‘Im (z)’): imaginary part of z |
It should be noted that, as every real number is a complex number (with zero imaginary part), all functions defined above are also valid for real variables.
A number of commands used to simplify and transform complex expressions also play an important role in working with complex numbers. These include the following:
Here are some examples:
We convert 1 + i to polar form:
>> maple('convert(1+i,polar)')
ans =
polar (2 ^(1/2), 1/4 * pi)
We simplify the value of sqrt ((1+2*i) /(1-2*i)):
>> maple('evalc(sqrt((1+2*i)/(1-2*i)))')
ans =
1/5 * 5 ^(1/2) + 2/5 * i * 5 ^(1/2)
We convert the complex number 2 * cos(Pi/4) + 2 * I * sin(Pi/4) to binary form:
>> maple('convert(2*cos(Pi/4)+2*I*sin(Pi/4),trig)')
ans =
2 ^(1/2) + I * 2 ^(1/2)
We convert the complex number 3 * exp(Pi*I/4) to trigonometric and polar form:
>> maple('convert(3*exp(Pi*I/4),trig)')
ans =
3*cosh(1/4*pi*I)+3*sinh(1/4*pi*I)
>> maple('convert(3*exp(Pi*I/4),polar)')
ans =
polar(3*exp(1/4*pi*Re(I)),argument(exp(1/4*pi*I)))
EXERCISE 3-10
Given the complex numbers X = 3 + 2i, Y = 3-2i and Z = i ^ 307, calculate X+Y+Z, X * Y * Z, x/y and y/z.
>> 3+2*i + 3-2*i - i^307
ans =
6.0000 + 1.0000i
>> (3+2*i)*(3-2*i)*i^307
ans =
0.0000 -13.0000i
>> (3+2*i)/(3-2*i)
ans =
0.3846 + 0.9231i
>> (3-2*i)/i^307
ans =
2.0000 + 3. 0000i
EXERCISE 3-11
Given the complex numbers X = 2 + 2i and Y = -3-3√3 i, calculate Y3 and X2/ Y90.
>> expand(sym(-3-3*sqrt(3)*i)^3)
ans =
216
>> maple('evalc((2+2*i)^2/(-3-3*sqrt(3)*i)^90)')
ans =
1/1350586945294983814787399212987393606090501966076362410331422244995072 * i
EXERCISE 3-12
Calculate the value of .
>> (i^8-i^(-8))/(3-4*i) + 1
ans =
1.0000 - 0.0000i
Alternatively we can write:
>> maple('simplify(evalc(i^8-i^(-8))/(3-4*i) + 1)')
ans =
1
EXERCISE 3-13
Calculate the modulus and the argument of each of the following complex numbers:
>> abs(i^i)
ans =
0.2079
>> angle(i^i)
ans =
0
>> abs(i^(3+i))
ans =
0.2079
>> angle(i^(3+i))
ans =
-1.5708
>> abs((i^i)^i)
ans =
1
>> angle((i^i)^i)
ans =
-1.5708
>> maple('simplify(evalc(abs((1+sqrt(3)*i)^(1-i))))')
ans =
2*exp(1/3*pi)
>> maple('simplify(evalc(argument((1+sqrt(3)*i)^(1-i))))')
ans =
-log (2) + 1/3 * pi
The moduli and arguments of the first three complex numbers have been obtained approximately. They can be calculated exactly as follows:
>> maple ('simplify(evalc(abs(i^i)))')
ans =
exp(-1/2*pi)
>> maple('simplify(evalc(argument(i^i)))')
ans =
0
>> maple('simplify(evalc(abs(i^(3+i))))')
ans =
exp(-1/2*pi)
>> maple('simplify(evalc(argument(i^(3+i))))')
ans =
-1/2*pi
>> maple('simplify(evalc(abs(i^(i^i))))')
ans =
1
>> maple('simplify(evalc(argument(i^(i^i))))')
ans =
1/2 * exp(-1/2*pi) * pi
EXERCISE 3-14
Solve the following equations:
a) cos(z) = 2
b) cos(z) = a, where a is a real number.
a)
>> maple ('evalc (solve (cos (z) = 2))')
ans =
i * log (2 + 3 ^(1/2))
b)
>> maple ('evalc (solve (cos (z) = a, z))')
ans =
acos(1/2 * ((a+1) ^ 2) ^(1/2)-1/2 * ((a-1) ^ 2) ^(1/2)) + i * signum (a) * log(1/2 * ((a+1) ^ 2) ^(1/2) + 1/2 * ((a-1) ^ 2) ^(1/2) + ((1/2 * ((a+1) ^ 2) ^(1/2) + 1/2 * ((a-1) ^ 2) ^(1/2)) ^ 2-1) ^(1/2))
EXERCISE 3-15
Solve the following equations:
a) 1+x+x2 +x3 +x4 +x5 = 0
b) x2 +(6-i)x+8-4i = 0
c) tan(z) = 3i/5
a)
>> solve('1+x+x^2+x^3+x^4+x^5 = 0')
ans =
[ -1]
[-1/2+1/2*i*3^(1/2)]
[-1/2-1/2*i*3^(1/2)]
[ 1/2+1/2*i*3^(1/2)]
[ 1/2-1/2*i*3^(1/2)]
b)
>> solve('x^2+(6-i)*x+8-4*i = 0')
ans =
[ -4]
[-2 + i]
c)
>> maple ('evalc(solve(tan (z) = 3 * i / 5))')
ans =
(i * atanh(3/5))
EXERCISE 3-16
Perform the following operations:
a) isin(1 + i)
b) (2+Ln(i))1/i
c) (1+i)i
d) iLn(1+i)
e) (1+i*sqrt(3))1-i
a)
>> maple('evalc(i^(sin(1+i)))')
ans =
exp (-1/2 * cos(1) * sin(1) * pi) * cos(1/2 * (1) * cosh(1) * pi) +
i * exp(-1/2 * cos(1) * sin(1) * pi) * sin(1/2 * (1) * cosh(1) * pi)
To approximate the result, we use the command “numeric”.
>> numeric(maple('simplify(evalc(i^(sin(1+i))))'))
ans =
-0.1667 + 0.3290i
b)
>> maple('simplify(evalc((2+log(i))^(1/i)))')
ans =
exp (atan(1/4*pi)) * cos(-log (2) + 1/2 * log(16+pi^2))-i * exp(atan(1/4*pi)) * sin(-log (2) + 1/2 * log(16+pi^2))
We can find the approximate result in this case directly in MATLAB (since it contains no complex trigonometric expressions):
>> (2+log(i))^(1/i)
ans =
1.1581 - 1.5639i
c)
>> maple('simplify(evalc((1+i)^i))')
ans =
exp(-1/4*pi) * cos(1/2 * log(2)) + i * exp(-1/4*pi) * sin(1/2 * log(2))
Now we find the approximate result:
>> (1+i)^i
ans =
0.4288 + 0.1549i
d)
>> maple('simplify(evalc(i^log(1+i)))')
ans =
exp(-1/8*pi^2) * cos (1/4 * log (2) * pi) + i * exp(-1/8*pi^2) * sin(1/4 * log(2) * pi)
The approximate result is obtained directly:
>> i^log(1+i)
ans =
0.2491 + 0.1508i
e)
>> maple('simplify(evalc((1+i*sqrt(3))^(1-i)))')
ans =
2 * exp(1/3*pi) * cos(-log (2) + 1/3 * pi) + 2 * i * exp(1/3*pi) * sin(-log (2) + 1/3 * pi)
The approximate result is obtained directly:
>> (1+i*sqrt(3))^(1-i)
ans =
5.3458 + 1.9759i
EXERCISE 3-17
Find the following:
a) the fourth roots of - 1 and 1
b) the fifth roots of 2 + 2i and - 1 + i√3
c) the real part of tan(iLn((a+ib)/(a-ib)))
d) the imaginary part of (2 + i)cos(4-i)
a)
>> solve('x^4+1=0')
ans =
[ 1/2* 2^(1/2) +1/2* i* 2^(1/2)]
[- 1/2* 2^(1/2) +1/2* i* 2^(1/2)]
[ 1/2* 2^(1/2) -1/2* i* 2^(1/2)]
[- 1/2* 2^(1/2) -1/2* i* 2^(1/2)]
We can obtain the approximate result easily:
>> numeric(solve('x^4+1=0'))
ans =
0.7071 + 0.7071i
-0.7071 + 0.7071i
0.7071 - 0.7071i
-0.7071 - 0.7071i
>> solve('x^4-1=0')
ans =
[ 1]
[-1]
[ i]
[-i]
b)
>> solve('x^5-2-2*i=0')
ans =
[ (2+2*i)^(1/5)]
[ (1/4*5^(1/2)-1/4+1/4*i*2^(1/2)*(5+5^(1/2))^(1/2))*(2+2*i)^(1/5)]
[(-1/4*5^(1/2)-1/4+1/4*i*2^(1/2)*(5-5^(1/2))^(1/2))*(2+2*i)^(1/5)]
[(-1/4*5^(1/2)-1/4-1/4*i*2^(1/2)*(5-5^(1/2))^(1/2))*(2+2*i)^(1/5)]
[ (1/4*5^(1/2)-1/4-1/4*i*2^(1/2)*(5+5^(1/2))^(1/2))*(2+2*i)^(1/5)]
We now find the approximate result:
>> numeric(solve('x^5-2-2*i=0'))
ans =
1.2160 + 0.1926i
0.1926 + 1.2160i
-1.0970 + 0.5589i
-0.8706 - 0.8706i
0.5589 - 1.0970i
c)
>> solve('x^5+1-sqrt(3)*i=0')
ans =
[ (-1+i*3^(1/2))^(1/5) ]
[ ( 1/4*5^(1/2)-1/4+1/4*i*2^(1/2)*(5+5^(1/2))^(1/2))*(-1+i*3^(1/2))^(1/5) ]
[ (-1/4*5^(1/2)-1/4+1/4*i*2^(1/2)*(5-5^(1/2))^(1/2))*(-1+i*3^(1/2))^(1/5) ]
[ (-1/4*5^(1/2)-1/4-1/4*i*2^(1/2)*(5-5^(1/2))^(1/2))*(-1+i*3^(1/2))^(1/5) ]
[ ( 1/4*5^(1/2)-1/4-1/4*i*2^(1/2)*(5+5^(1/2))^(1/2))*(-1+i*3-^(1/2))^(1/5)]
The approximate result is immediate:
>> numeric(solve('x^5+1-sqrt(3)*i=0'))
ans =
1.0494 + 0.4672i
-0.1201 + 1.1424i
-1.1236 + 0.2388i
-0.5743 - 0.9948i
0.7686 - 0.8536i
d)
>> maple('simplify(evalc(Re(tan(i*log((a+i*b)/(a-i*b))))))')
ans =
-2*a*b/(a^2-b^2)
e)
>> maple('simplify(evalc(Im((2+i)^cos(4-i))))')
ans =
5 ^ (1/2 * cos(4) * cosh(1)) * exp(-sin(4) * sin(1) * atan(1/2))
* sin(1/2 * sin(4) * sin(1) * log(5) + cos(4) * cosh(1) * atan(1/2))
Now we find the approximate result:
>> numeric(maple('evalc(Im((2+i)^cos(4-i)))'))
ans =
-0.6211
3.9 Divisibility in the Complex Field. The Ring of Gaussian Integers
Within the set C of complex numbers, the subset G = {zC / z = a + bi, a,bZ} formed by all the complex numbers with integer real and imaginary parts is a ring, known as the ring of Gaussian integers. Within this ring, divisibility can be studied in a similar manner to the study of divisibility in the ring of integers.
There are some functions in MATLAB that allow you to work in this ring, as long as the extended symbolic math Toolbox is available. Among them are the following:
Here are some examples:
We factorize the number 30 in the ring of Gaussian integers.
>> maple('with(GaussInt):GIfactor(30)')
ans =
(i) *(1+i) ^ 2 *(1+2*i) *(1-2*i) *(-3)
The prime factors of 30 in the ring of Gaussian integers are: -3 with multiplicity 1,-1-2i with multiplicity 1,-1 + 2i with multiplicity 1, 1 + i with multiplicity 2 and i with multiplicity 1.
You can also find the set of prime factors of 30 or the list of prime factors together with their orders of multiplicity.
>> maple('with(GaussInt):GIfacset(30)')
ans =
{-3, 1+i, -1+2*i, -1-2*i}
>> maple('with(GaussInt):GIfactors(30)')
ans =
[i, [[1 + i, 2], [- 1 + 2 * i, 1], [- 1-2 * i, 1], [1, - 3]]]
You can also determine if a Gaussian integer is prime or not.
The number 1 + i is prime in the ring of Gaussian integers:
>> maple('with(GaussInt):GIprime(1+i)')
ans =
true
EXERCISE 3-18
Solve the following equation in the ring of Gaussian integers:
x4 - 17*x3 - 29*i*x3 - 188*x2 + 339*i*x2 + 1682*x - 86*i*x - 1178 - 1244*i = 0
>> maple('with(GaussInt):GIroots(x^4-17*x^3-29*i*x^3-188*x^2+339*i*x^2+ 1682*x-86*i*x-1178-1244*i)')
ans =
[[5+8*i, 1], [1+i, 1], [7+11*i, 1], [4+9*i, 1]]
EXERCISE 3-19
Check if the number 1000 - 500i is prime. If not, find all of its divisors and decompose it into prime factors. Also find the closest Gaussian integer to the least common multiple and the greatest common divisor of 1000-500i, 100-50i and 10-5i. Finally, find the norm of 1000-500i.
>> maple('with(GaussInt)'); maple('GIprime(1000-500*i)')
ans =
false
>> maple('with(GaussInt)');maple('GIdivisor(1000-500*i)')
ans =
{1, 2, 4, 5, 10, 20, 25, 50, 100, 125, 500, 500+1000*i, 1+i, 1+2*i, 250, 12+16*i, 13+9*i, 4+22*i, 14+2*i, 11+2*i, 4+8*i, 28+96*i, 9+13*i, 55+10*i, 6+2*i, 62+34*i, 20+40*i, 100+200*i, 60+80*i, 5+5*i, 45+65*i, 25+25*i, 125+125*i, 30+10*i, 150+50*i, 70+10*i, 750+250*i, 2+i, 2+2*i, 3+4*i, 2+11*i, 8+4*i, 8+44*i, 1+3*i, 7+i, 4+2*i, 6+8*i, 2+6*i, 26+18*i, 4+3*i, 7+24*i, 16+12*i, 44+8*i, 3+i, 1+7*i, 31+17*i, 5+10*i, 20+15*i, 2+4*i, 8+6*i, 22+4*i, 14+48*i, 2+14*i, 18+26*i, 10+5*i, 25+50*i, 100+75*i, 15+20*i, 50+25*i, 125+250*i, 80+60*i, 220+40*i, 40+20*i, 400+300*i, 200+100*i, 15+5*i, 5+35*i, 5+15*i, 75+25*i, 25+175*i, 35+5*i, 25+75*i, 375+125*i, 10+20*i, 40+30*i, 110+20*i, 20+10*i, 50+100*i, 200+150*i, 30+40*i, 100+50*i, 250+500*i, 10+10*i, 10+70*i, 90+130*i, 10+30*i, 50+50*i, 50+350*i, 50+150*i, 250+250*i}
>> maple('with(GaussInt)');maple('GIfactor(1000-500*i)')
ans =
(-i)*(1+i)^4*(-1+2*i)^3*(-1-2*i)^4
>> maple('with(GaussInt)');maple('GIgcd(1000-500*i,100-50*i,10-5*i)')
ans =
5+10*i
>> maple('with(GaussInt)');maple('GIlcm(1000-500*i,100-50*i,10-5*i)')
ans =
500+1000*i
>> maple('with(GaussInt)');maple('GInorm(1000-500*i)')
ans =
1250000
EXERCISE 3-20
Find the square-free factorization of the Gaussian integers 1574 + 368 * i and 3369067456 + 16670818364 * i
>> maple('with(GaussInt):GIsqrfree(1574+368*i)')
ans =
[i, [[65+148*i, 1], [-1+3*i, 2]]]
>> maple('with(GaussInt):GIsqrfree(3369067456 + 16670818364*i)')
ans =
[i, [[-3-2*i, 1], [-7+2*i, 2], [1+i, 4], [5-2*i, 5], [1+4*i, 6]]]
EXERCISE 3-21
Find the Smith normal form of the matrix H of Gaussian integers whose rows are the vectors [- 4 + 7 * i, 8 + 10 * i, - 6 – 8 * i], [- 5 + 7 * i, 6 - 6 * i, 5 * i] and [-10 + i, 1 - 3 * i, - 10 + 5 * i]. Find the corresponding transformation matrices. Also find the step reduced Hermite normal form of the matrix H and the corresponding transformation matrix.
>> maple('with(GaussInt):H:= array([[-4+7*i,8+10*i,-6-8*i],[-5+7*i,6-6*i,5*i],[- 10+i,1-3*i,-10+5*i]])')
ans =
H := matrix([[-4+7*i, 8+10*i, -6-8*i], [-5+7*i, 6-6*i, 5*i], [-10+i, 1-3*i, -10+5*i]])
>> maple('with(GaussInt):GIsmith(H)')
ans =
matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1797+791*i]])
The matrix is displayed in standard row and column format using pretty.
>> pretty(sym(maple('with(GaussInt):GIsmith(H)')))
[1 0 0 ]
[ ]
[0 1 0 ]
[ ]
[0 0 1797 + 791i]
>> pretty(sym(maple('with(GaussInt):GIsmith(H,A,B),eval(A),eval(B)')));
[1 0 0 ]
[ ]
[0 1 0 ],
[ ]
[0 0 1797 + 791i ]
[ 1 -1 0 ]
[ ]
[-5521 - 954i 16188 - 11418i 1405 + 13698i ],
[ ]
[ 7440 - 10079i 6598 + 43799i -28787 - 10920i ]
[1 778 + 3519i -3051937 + 837129i]
[ ]
[0 1 44 + 877i ]
[ ]
[0 247 + 54i -36491 + 218995i ]
>> pretty(sym(maple('with(GaussInt):GIhermite(H,M),eval(M)')))
[1 0 679 - 58i] [-14 + 25i 51 + 16i -49 + 3i]
[ ] [ ]
[0 1 + i 121 - 712i], [ 22 + 21i 30 - 48i -10 + 51i]
[ ] [ ]
[0 0 503 + 1294i] [-57 - 13i -2 + 109i -34 - 94i]
Here we have defined a matrix simply by placing its rows between square brackets, separated by commas. As we will see later, this is a standard way to define arrays in MATLAB.
EXERCISE 3-22
Factorize the polynomial x4 - 17x3 - 29ix3 - 188x2 + 339ix2 + 1682x - 86ix – 1178 - 1244i in the ring of Gaussian integers. Also find its roots in the ring of Gaussian integers.
>> maple('with(GaussInt):GIfacpoly(x^4-17*x^3-29*I*x^3-188*x^2+339*I*x^2 +1682*x-86*I*x-1178-1244*I)')
ans =
[1, [[x-4-9*i, 1], [x-5-8*i, 1], [x-1-i, 1], [x-7-11*i, 1]]]
The factorization is: (x-4-9i)(x-5-8i)(x-1-i)(x-7-11i). Now we find the roots, which must logically be 1 + i, 4 + 9i and 11i 7 + 5 + 8i, all with multiplicity 1.
>> maple('with(GaussInt):GIroots(x^4-17*x^3-29*i*x^3-188*x^2+339*i*x^2 +1682*x-86*i*x-1178-1244*i)')
ans =
[[4 + 9 * i, 1], [5 + 8 * i, 1], [1 + i, 1], [7 + 11 * i, 1]]
3.10 Approximation and Precision
The accuracy of the output of numerical operations with MATLAB can be relaxed using special approximation techniques, returning results to a certain degree of precision.
MATLAB represents results with accuracy, but even if internally you are always working with exact calculations to avoid rounding errors, you can enable different approximate representation formats, which sometimes facilitate the interpretation of results. The following commands can be used for numerical approximation:
Here are some examples:
For each format, find a numerical approximation of √17:
>> sqrt(17)
ans =
4.1231
>> format long; sqrt(17)
ans =
4.12310562561766
>> format long e; sqrt(17)
ans =
4.123105625617660e+000
>> format short e; sqrt(17)
ans =
4.1231e+000
>> format long g; sqrt(17)
ans =
4.12310562561766
>> format short g; sqrt(17)
ans =
4.1231
>> format bank; sqrt(17)
ans =
4.12
>> format hex; sqrt (17)
ans =
40107e0f66afed07
Now we give some examples of the calculation of the value of sqrt(17) specifying the precision that we desire:
>> vpa 'sqrt(17)' 10
ans =
4.123105626
>> digits(15); maple('evalf(sqrt(17))')
ans =
4.12310562561766
>> digits(15); maple('evalhf(sqrt(17))')
ans =
4.123105625617661
>> digits (15); maple ('evala (sqrt (17))')
ans =
17 ^(1/2)
We find the decimal approximation of π to 100 digits of precision.
>> vpa 'pi' 100
ans =
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
>> digits(100); maple('evalf(pi)')
ans =
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
Now we convert the numbers 12345 * 10-4, 456 * 10-4 and 12345 * 1018 -12345 * 10-18 to floating point format:
>> maple('Float(123456,-4)')
ans =
12.3456
>> maple('Float(456,-4)')
ans =
.456e-1
>> maple('Float(12345,18)')
ans =
. 12345e23
>> maple ('Float(12345, - 18) ')
ans =
. 12345e-13
We convert 1234598678 * 10-3 to a floating-point number:
>> maple ('Float(1234598678, - 3)')
ans =
1234598.678
We now consider the integral of x4 between 0 and 1 as an inert function and then calculate its numerical value:
>> maple('Int(x^4,x=0..1)')
ans =
Int(x^4,x = 0 .. 1)
>> maple('value(Int(x^4,x=0..1))')
ans =
1/5
3.11 Types of Numbers and Expressions
In MATLAB, you can work with different types of numbers and expressions. It is possible to declare the corresponding type of each number or expression, as well as to check the type of a given number or expression. These types of numbers and expressions cover integers, real numbers and complex numbers, and are used in arguments of functions whose definition is preceded by the command maple.
Among the types of numbers in MATLAB we have the following:
Having described the types of numbers and the most important numerical expressions that can be declared in MATLAB, we now summarize the commands that handle them:
Here are some examples:
>> maple('type ((3+2*i), complex)')
ans =
true
>> maple('type(257,prime)')
ans =
true
>> maple('whattype([1,2,3])')
ans =
list
>> maple('whattype({1,2,3})')
ans =
set
3.12 Random Numbers
The automatic generation of (pseudo) random numbers is a problem well handled by MATLAB.
MATLAB provides the function rand to generate uniformly distributed random numbers and the function randn to generate normally distributed random numbers. The following functions can be used to generate random floating point numbers:
Here are some examples:
>> [rand, rand (1), randn, randn (1)]
ans =
0.8310 0.0346 1.1650 0.6268
>> [rand(2), randn(2)]
ans =
0.0535 0.6711 0.0751 -0.6965
0.5297 0.0077 0.3516 1.6961
>> [rand(4,3), randn(4,3)]
ans =
0.6326 0.2470 0.6515 - 0.0562 0.4005 0.7286
0.7564 0.9826 0.0727 0.5135 - 1.3414 - 2.3775
0.9910 0.7227 0.6316 0.3967 0.3750 - 0.2738
0.7534 0.8847 0.7562 1.1252 - 0.3229
>> maple with(linalg):randmatrix(3,3,symmetric)
ans =
matrix([[-85,-55,-35], [-55,-37, 97], [-35, 97, 50]])