The main MATLAB commands used in this tutorial are:`grunt`,`versatile`,`carrot`,`know`,`dekonw`,`inv. no`,`npr`,`poli`,`tf`,`zero`

## Contents

- Vector
- Functions
- Plot
- Polynomials as vectors
- Polynomials using the variable s
- Matrix
- Using M files in MATLAB
- Get help with MATLAB

MATLAB is an interactive program for numerical calculations and data visualization; it is widely used by automation engineers for analysis and design. Many different toolkits are available to extend MATLAB's core functionality to different application areas; in these tutorials, we will make extensive use of the Control Systems Toolbox. MATLAB is supported in Unix, Macintosh, and Windows environments; a student version of MATLAB for PCs is available. More information on MATLAB can be found on the websiteMathWorksDom.

The idea behind these tutorials is that you can view them in one window by running MATLAB in another window. You should be able to repeat all the diagrams and calculations from the tutorials by cutting and pasting the tutorial text into the MATLAB command window orm-best.

## Vector

Let's start by creating something simple, like a vector. Enter each element of the vector (separated by a space) in parentheses and make it equal to the variable. For example, to create a vector**A**, enter the following in the MATLAB command window (you can**Copy**I**Placement**from browser to MATLAB to make it easier) and MATLAB should return:

for = [1 2 3 4 5 6 9 8 7]

for = 1 2 3 4 5 6 9 8 7

Suppose you want to create a vector with elements 0 to 20 evenly spaced in steps of two (this method is often used to create a time vector):

t = 0:2:20

t = 0 2 4 6 8 10 12 14 16 18 20

Manipulating vectors is almost as easy as creating them. Let's first assume you want to add 2 to each element of the vector**A**. The equation looks like this:

b = za + 2

b = 3 4 5 6 7 8 11 10 9

Now suppose you want to add two vectors. This is easy if both vectors have the same length. Just add two as shown below:

do = a + b

do = 4 6 8 10 12 14 20 18 16

Subtracting vectors of the same length works exactly the same way.

## Functions

To make life easier, MATLAB includes many standard functions. Each function is a block of code that performs a specific task. MATLAB includes all standard functions such as`shame`,`to want`,`journal`,`to the power of`,`square`like many others. Commonly used constants such as`PI number`, I`W`z`J`for the square root of -1, are also built into MATLAB.

sin(pi/4)

odp = 0,7071

To specify function usage, type`help [function name]`in the MATLAB command window.

With MATLAB, you can even write your own functions** function**Command; follow the link to learn how to write your own functions and check out the list of functions we created for this tutorial.

## Plot

Creating graphs in MATLAB is also easy. Suppose you want to plot a sine wave as a function of time. First, create a time vector (the semicolon after each statement tells MATLAB we don't want to see all the values), then calculate the sin value each time. commands after`grunt`function (`title`,`xlabel`,`label`) adds notes to the action.

t = 0:0.25:7;y = sin(t);graph(t,y)title("Sine wave versus time")xlabel("Time (Seconds)")label('Amplitude')

The graph contains approximately one period of a sine wave. Basic plotting is very easy in MATLAB, a`grunt`the command has a wide variety of options to add. you can visitto planpage for more information.

## Polynomials as vectors

In MATLAB, a polynomial is represented by a vector. To create a polynomial in MATLAB, you simply enter each coefficient of the polynomial into the vector in descending order. For example, suppose you have the following polynomial:

(1)

To transfer this to MATLAB, just input it as a vector like this:

x = [1 3 -15 -2 9]

x = 1 3 -15 -2 9

MATLAB can interpret a vector of length n+1 as a polynomial of order n. So if your polynomial is missing some coefficients, you need to put zeros in the appropriate place of the vector. For example,

(2)

will be displayed in MATLAB as:

jot = [1 0 0 0 1]

jot = 1 0 0 0 1

The value of the polynomial can be found using`versatile`function. For example, to find the value of the above polynomial w`s = 2`,

z = goes([1 0 0 0 1],2)

z = 17

You can also extract the roots of polynomials. This is useful if you have a higher order polynomial such as

(3)

Finding the root directory will be as simple as entering the following command:

roots ([1 3 -15 -2 9])

odp = -5,5745 2,5836 -0,7951 0,7860

Suppose you want to multiply two polynomials. The product of two polynomials is determined by the convolution of their coefficients. MATLAB function`know`will do it for you.

x = [1 2];y = [1 4 8];z = konv(x,y)

z = 1 6 16 16

Dividing two polynomials is just as easy. The`dekonw`the function returns both the remainder and the result. Let's part ways`z`Already`G`and we'll see if we succeed`X`.

[xx, R] = dekonv(z,y)

xx = 1 2R = 0 0 0 0

As you can see, it's just a polynomial/vector`X`from the past. If`G`didn't come in`z`evenly, the residue vector would be something other than zero.

## Polynomials using the variable s

Another way to represent polynomials is to use a Laplace variable`S`in MATLAB. This method is the most commonly used in these tutorials. Let's omit the details of the Laplace domain for now and introduce only the polynomials s`S`changeable. To define a variable, type in the MATLAB command window:

`s = tf('S')`

s = s Continuous time transfer function.

Recall the above polynomial:

(4)

To display this in MATLAB, enter the following command in the MATLAB command window:

polinom = s^4 + 3*s^3 - 15*s^2 - 2*s + 9

polynomial = s^4 + 3 s^3 - 15 s^2 - 2 s + 9 Continuous-time transfer function.

instead of using`carrot`function we can use`zero`a function that finds the root of a polynomial.

null (polynomial)

odp = -5,5745 2,5836 -0,7951 0,7860

As you can see, the result is the same as above`carrot`order and coefficients of polynomials.

You can also multiply two polynomials by`S`changeable. Let's redefine it`X`I`G`.

x = s + 2; y = s^2 + 4*s + 8; z = x * y

z = s^3 + 6 s^2 + 16 s + 16 Continuous-time transfer function.

The resulting polynomial has the same coefficients as the resulting vector`know`function above.

## Matrix

Entering a matrix in MATLAB is the same as entering vectors, except that each row of elements is separated by a semicolon (;) or a return character:

B = [1 2 3 4; 5 6 7 8; 9 10 11 12] B = [ 1 2 3 4 5 6 7 8 9 10 11 12 ]

B = 1 2 3 4 5 6 7 8 9 10 11 12B = 1 2 3 4 5 6 7 8 9 10 11 12

Arrays in MATLAB can be manipulated in many ways. First, you can find the matrix transpose with the apostrophe key:

C = B'

do = 1 5 9 2 6 10 3 7 11 4 8 12

It is worth noting that if*C*complex, the apostrophe would actually result in a complex conjugate transposition. To get the transpose in this case, use`.'`(both commands are the same if the matrix is not composite).

Now you can multiply two matrices*B*I*C*Together. Remember that order matters when multiplying matrices.

D = B * CD = C * B

D = 30 70 110 70 174 278 110 278 446D = 107 122 137 152 122 140 158 176 137 158 179 200 152 176 200 224

Another possibility of matrix manipulation is that you can multiply the corresponding elements of two matrices with`.*`operator (for this to work, the matrices must be the same size).

mi = [1 2; 3 4]F = [2 3; 4 5]G = E.* F

E = 1 2 3 4F = 2 3 4 5G = 2 6 12 20

If you have a square matrix, e.g*mi*, you can also multiply it by itself as many times as you like, raising it to the given power.

E^3

odp = 37 54 81 118

If you want to expand each element of the matrix, use a cube per element.

E.^3

odp = 1 8 27 64

You can also find matrix inversion:

X = inv(E)

X = -2,0000 1,0000 1,5000 -0,5000

or its eigenvalues:

eig (E)

odp = -0,3723 5,3723

There is even a function to find the coefficients of the characteristic polynomial of a matrix. The`poli`function creates a vector containing the coefficients of the characteristic polynomial.

p = poly(E)

p = 1,0000 -5,0000 -2,0000

Recall that the eigenvalues of a matrix are equal to the roots of its characteristic polynomial:

roots (p)

odp = 5,3723 -0,3723

Printing in MATLAB is quite simple. Just follow the steps below:

**prochowiec**

- To print a graph or a
`m-best`on Macintosh, just click the picture or`m-best`, To choose**Print**below**Duration**I went, I hit**give**.

**ramen**

- To print a graph or a
`m-best`on a Windows PC, just select**Print**van**Duration**menu in the graph window or`m-best`and guessed it**give**.

**Unix**

- To print a drawing on a Unix workstation, type:
`print -P`. - To save the drawing and print it later, type:
`print plot.ps`. - A little later, you can print the drawing using the command
`lpr -P plot.ps`If you are using an HP workstation to print, use this command instead`lpr -d plot.ps`. - to print
`m-best`, just print it like you print any other file using the command`lpr-P`. If you are using an HP workstation to print, use this command instead*file name*.M`lpr -d plot.ps`.*file name*.M

## Using M files in MATLAB

There are slightly different things you need to know for each platform.

**prochowiec**

- There is a built-in editor for this
`blisters m`; to choose**New M Stand**van**Duration**Menu. You can also use any other editor you like (but be sure to save and load files in text format after starting MATLAB).

**ramen**

- Running MATLAB from Windows is very similar to running it from a Macintosh. But you should know that yours
`m-best`is saved in the clipboard. So you need to make sure it is saved as`filename.m`.

**Unix**

- The editor must be run separately from MATLAB. The best strategy is to create a folder for all files
`blisters m`, I`CD`to this folder before starting MATLAB and the editor. To start MATLAB from an Xterm window, just type:`matlab`.

You can type commands directly into MATLAB or put all the commands you need in one`m-best`and just run the file. If you put everything`blisters m`in the same folder you run MATLAB from, MATLAB will always find them.

## Get help with MATLAB

MATLAB has pretty good online help, type:

`help command name`

for more information on each task. You need to know the name of the command you are looking for; a list of all the resources used in this manual can be found in**command list**; the link to this page is in the upper right corner of this page.

There are some notes at the end of this manual.

You can check the value of a specific variable at any time by entering its name.

B

B = 1 2 3 4 5 6 7 8 9 10 11 12

You can also put more than one statement on a line as long as you separate them with a semicolon or comma.

You may have also noticed that until you assign a specific operation or result to a variable, MATLAB stores it in a temporary variable called`answer`.

Released with MATLAB® 9.2