matlab basics. the following screen will appear when you start up matlab. all of the commands that...

32
MATLAB Basics

Upload: alfred-wilkinson

Post on 01-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

MATLAB

Basics

Page 2: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

The following screen will appear when you start up Matlab.All of the commands that will be discussed should be typed at the >> prompt.

Page 3: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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.

Page 4: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

This variable can be used later, but don't forget that it will automatically be overwritten when a new computation is done.

Page 5: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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

Page 6: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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

Page 7: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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.

Page 8: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

Calculus 

Page 9: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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.

Page 10: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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)

Page 11: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

MATLAB

Complex Numbers

Page 12: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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

Page 13: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

Arithmetic with imaginary numbers is very straightforward.

Page 14: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt
Page 15: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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

Page 16: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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

Page 17: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

MATRICES

Page 18: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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.

Page 19: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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

Page 20: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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

Page 21: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

Finding the determinant or the inverse of a matrix is also simple.

Page 22: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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

Page 23: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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. 

Page 24: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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.

Page 25: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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

Page 26: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

MATLAB

POLYNOMIALS & RATIONAL FUNCTIONS

Page 27: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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

Page 28: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

To multiply x² + 2x + 1 and x + 1, we use

Page 29: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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.

Page 30: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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

Page 31: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

The partial fraction expansion of (2s + 5) / (s3 + 5s² + 8s + 4) is found by

Page 32: MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt

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)