Académique Documents
Professionnel Documents
Culture Documents
2.6. Functions
In MATLAB you will use bothbuilt-in functions as well as functions that you create
yourself.
Built-in Functions
MATLAB has many built-in functions. These include sqrt, cos, sin, tan, log, exp, and atan
(for arctan) as well as more specialized mathematical functions such as gamma. MATLAB
also has several built-in constants, including pi. As examples consider:
1) >> log(exp(3))
ans =
3
The function log is the natural logarithm, called ln in many texts.
2) >> sin(2*pi/3)
ans =
0.8660
User-Defined Functions
In this section we will show how to use inline to define your own functions.
Heres how to define the polynomial function f (x) = x2 + x + 1:
>> f = inline(x2 + x + 1, x)
f=
Inline function:
f(x) = x^2 + x + 1
The first argument to inline is a string containing the expression defining the function. The
second argument is a string specifying the independent variable.
Once the function is defined, you can evaluate it:
>> f(4)
ans =
21
MATLAB functions can operate on vectors as well as scalars. To make an inline function
that can act on vectors, we useMATLABs vectorize function.
Here is the vectorized version of f (x) = x2 + x + 1:
>> f1 = inline(vectorize(x2 + x + 1), x)
f1 =
Inline function:
f1(x) = x.^2 + x + 1
Note that ^ has been replaced by .^. Now you can evaluate f1 on a vector e.g:
>> f1(1:5)
ans =
3 7 13 21 31
As remark, one can also define functions of two or more variables:
>> g = inline(u2 + v2, u, v)
g=
Inline function:
g(u,v) = u^2+v^2
2.7. Loops
A loop specifies that a command or group of commands should be repeated several times.
The easiest way to create a loop is to use a for statement.
The loop begins with the for statement and ends with the end statement.
For example, to generate a list of n factorial where n! = 1 2 3 . . . (n 1) n.
Here is a simple example that computes and displays 10! = 10 9 8 2 1
>> f = 1;
for n = 2:10
f = f*n;
end
f
f=
3628800
The command between those statements is executed a total of nine times, once for each
value of n from 2 to 10. We used a semicolon to suppress intermediate output within the
loop.
2.8 Complex numbers
MATLAB does most of its computations using complex numbers, that is, numbers of the
form a + bi, where a and b are real numbers. In Matlab, the special values i and j stand
for 1 . Although you may never have occasion to enter a complex number in a
MATLAB session, MATLAB often produces an answer involving a complex number. For
example, many polynomials with real coefficients have complex roots as well as some
common functions return complex values for certain values of the argument. For example,
>> log(-1)
ans =
0 + 3.1416i
The symbol i may be used to assign complex values, e.g.
z = 2 + 3*i represents the complex number 2+3i (real part 2, imaginary part 3).
NB: in response to the input prompt (remember, no semicolon).
The imaginary part of a complex number may also be entered without an asterisk, e.g. 3i.
All the arithmetic operators (and most functions) work with complex numbers, e.g. sqrt(2
+ 3*i), exp(i*pi).
There are some functions that are specific to complex numbers. If z is a complex number
real(z), imag(z), conj(z) and abs(z) all have the obvious meanings. A complex number may
be represented in polar coordinates, i.e. z = rei. angle(z) returns between and .
abs(z) returns the magnitude r.
For complex matrices, the operations and . behave differently.
The operator is the complex conjugate transpose, meaning rows and columns are
interchanged, and signs of imaginary parts are changed.
The . operator, on the other hand, does a pure transpose without taking the complex
conjugate. E.g, consider the complex matrix a with the statement
a = [1+i 2+2i; 3+3i 4+4i]
which results in
a=
1.0000 + 1.0000i 2.0000 + 2.0000i
3.0000 + 3.0000i 4.0000 + 4.0000i
The statement
a
then results in the complex conjugate transpose
ans =
1.0000 - 1.0000i 3.0000 - 3.0000i
2.0000 - 2.0000i 4.0000 - 4.0000i
whereas the statement
a.
results in the pure transpose
ans =
1.0000 + 1.0000i 3.0000 + 3.0000i
2.0000 + 2.0000i 4.0000 + 4.0000i
4. Compute to 15 digits:
(a) cosh(0.1).
(b) ln(2). (Hint: The natural logarithm in MATLAB is called log, not ln.)
(c) arctan(1/2). (Hint: The inverse tangent function inMATLABis called atan, not arctan.)
5) Solve the system of linear equations
3x + 4y + 5z = 2
2x 3y + 7z= 1
x 6y + z = 3.
Check your answer using matrix multiplication .
6) What are the values of x and a after the following statements have been executed?
Z=3.5-1.25i;
a= -2i;
R=z-a/3;
Y=cos(R-i);
Conj (y);
y;
P=R^a;