Anda di halaman 1dari 8

Matlab

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.

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.
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.

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. 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.

Complex Numbers The variable i is already defined as the square root of -1. We can verify this by calculating i * i. 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

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.

Matrices of the same dimensions can be added and subtracted, and conformable matrices can be multiplied. Finding the determinant or the inverse of a matrix is also simple.

For example, if we have 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. 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.

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 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

If the left hand side of the equation didn't contain two variables, the answer would only have the quotient and the remainder would be discarded. Matlab also has a function that will give the partial fraction decomposition of a rational function. This is very useful when working with Laplace transforms. The function residue takes two polynomials and returns the residues, the poles, and the direct term (quotient). The partial fraction expansion of (2s + 5) / (s3 + 5s + 8s + 4) is found in the figure. There is a pole at 1 and a repeated pole at 2. There is no direct term since the order of the numerator was less than the order of the denominator. (2s + 5) / (s3 + 5s + 8s + 4) = -3 / (s + 2) -1 / (s + 2) + 3 / (s + 1)