Sei sulla pagina 1di 14

# Matlab

Matlab

Note: This tutorial was made using Matlab v. 5.3. Any other versions may have differences in syntax or lack some of the
functionality referred to later in the tutorial.

Matlab is a powerful mathematical tool and this tutorial is intended to be an introduction to some of the functions that you
might find useful. For more detail about other parts of the program, refer to the User's Manual and the help files.

Basics
The following is a picture of what will appear when you start up Matlab.

All of the commands that will be discussed should be typed at the >> prompt.

Matlab is a program that works with matrices. Scalars are simply 1 x 1 matrices and functions of single variables are
notated as a vector (a 1 x n or n x 1 matrix). The rest of the tutorial will be divided into five parts, each introducing a
different feature of Matlab. The parts are: basic arithmetic, calculus, complex numbers, matrices, and polynomials and
rational functions.

## file:///D|/my%20books/lab%20view/Matlab.htm (1 of 14)5/8/2007 7:03:36 PM

Matlab

Basic Arithmetic
Look at the following picture.

As you can see, simple computations are very easy. Just type in exactly what you want computed and press enter. If no
variables are used, the answer will automatically be assigned to the variable ans. This variable can be used later, but don't
forget that it will automatically be overwritten when a new computation is done.

## file:///D|/my%20books/lab%20view/Matlab.htm (2 of 14)5/8/2007 7:03:36 PM

Matlab

Variables are also easy to use. Simply type a variable name, the equal sign, and then some value to be assigned.

At any time, you can type "whos" at the command line to see what variables have been assigned.

## file:///D|/my%20books/lab%20view/Matlab.htm (3 of 14)5/8/2007 7:03:36 PM

Matlab

Other common mathematical functions are available such as square root , trigonometric functions, exponential functions,
etc. Refer to the help window under the help menu for the syntax of these functions. Most of the common mathematical
functions are listed in the file matlab/elfun.

Calculus
Matlab provides an easy way to compute both derivatives and integrals. To perform symbolic differentiation or integration,
you must first declare a symbolic variable. This is done by typing "syms x" where x is the variable name. In the following
example, x is declared as symbolic and then used to find the indefinite integral and derivative of a function.

## file:///D|/my%20books/lab%20view/Matlab.htm (4 of 14)5/8/2007 7:03:36 PM

Matlab

The definite integral can also be computed by adding the lower and upper bounds, separated by commas, after the function
to integrate. (Note that a function of a valid symbol is still required)

For more information on the symbolic calculus functions, see the toolbox/symbolic section of the help window.

## file:///D|/my%20books/lab%20view/Matlab.htm (5 of 14)5/8/2007 7:03:36 PM

Matlab

Complex Numbers
The variable i is already defined as the square root of -1. We can verify this by calculating i * i.

## file:///D|/my%20books/lab%20view/Matlab.htm (6 of 14)5/8/2007 7:03:36 PM

Matlab

The functions abs and angle allow us to convert the complex number from rectangular to polar form.

Angle returns the phase angle in radians, but converting to degrees we see that the answer is what we expected.

## file:///D|/my%20books/lab%20view/Matlab.htm (7 of 14)5/8/2007 7:03:36 PM

Matlab

Matrices
Matrices are the basis of Matlab, so manipulating them is very simple. First, you input matrices by placing the values in
brackets, with semicolons separating the rows.

## file:///D|/my%20books/lab%20view/Matlab.htm (8 of 14)5/8/2007 7:03:36 PM

Matlab

Matrices of the same dimensions can be added and subtracted, and conformable matrices can be multiplied.

Matlab

## file:///D|/my%20books/lab%20view/Matlab.htm (10 of 14)5/8/2007 7:03:36 PM

Matlab

These functions are particularly useful in circuit analysis, where it is necessary to solve several simultaneous equations. For
example, if analysis yielded the equations

## ● 7v1 - 4v2 - 2v3 = 3

● -4v1 + 9v2 - 2v3 = 0
● -2v1 - 2v2 + 5v3 = -12

It would be very easy to solve for the unknowns. First, create a 3x3 matrix with the coefficients of v1, v2, and v3, then
create a 1x3 matrix with the right hand side of the equations. Finally, multiply the inverse of the first matrix with the
second matrix, and the resulting matrix contains the answers.

● v1 = -1.3597
● v2 = -1.3813
● v3 = -3.4964

This method also will work with symbolic variables. For example, you can solve for equations when using a Laplace
transform by declaring s as a symbol ("syms s") and then entering the values into the matrix.

## file:///D|/my%20books/lab%20view/Matlab.htm (11 of 14)5/8/2007 7:03:36 PM

Matlab

Sometimes the way the answers are given is somewhat confusing. The answers to the last example are

## ● i1 = (s² + 3s + 4) / [(s+3)(s3 + 8s² + 11s + 20)]

● i2 = 2s / [(s+3)(s3 + 8s² + 11s + 20)]

## Polynomials and Rational Functions

Matlab also provides tools for manipulating polynomials and rational functions. To use these tools, the polynomial should
be represented as a vector with the leftmost number being the highest power and the rightmost number being the constant.
For example, x² + 2x + 1 would be represented as [1 2 1].

The roots function gives the roots of the polynomial and polyval evaluates the polynomial at the given value. Multiplying
and dividing polynomials can be done with conv and deconv

## file:///D|/my%20books/lab%20view/Matlab.htm (12 of 14)5/8/2007 7:03:36 PM

Matlab

Note that deconv will return two vectors, the first contains the coefficients for the quotient polynomial, and the second
contains the coefficients for the remainder polynomial. The following example divides x3 + 3x² + 3x + 2 by x + 1

## file:///D|/my%20books/lab%20view/Matlab.htm (13 of 14)5/8/2007 7:03:36 PM

Matlab

If the left hand side of the equation didn't contain two variables, the answer would only have the quotient and the remainder