Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Matlab Tutorial for University of Delaware Mechanical Engineering Students, Lab Reports of Advanced Control Systems

A comprehensive tutorial on using matlab, a high-level programming language and interactive environment for mathematical computing. It covers topics such as entering simple matrices, complex numbers and pi, accessing matrix elements, generating vectors, saving and reloading work, and basic plotting. Students in the mechanical engineering department at the university of delaware can benefit from this tutorial to enhance their understanding and skills in using matlab for mathematical and engineering applications.

Typology: Lab Reports

Pre 2010

Uploaded on 09/02/2009

koofers-user-z9e
koofers-user-z9e 🇺🇸

3

(1)

10 documents

1 / 10

Toggle sidebar

Related documents


Partial preview of the text

Download Matlab Tutorial for University of Delaware Mechanical Engineering Students and more Lab Reports Advanced Control Systems in PDF only on Docsity! University of Delaware Department of Mechanical Engineering -1- A Matlab Tutorial Matlab stands for MATrix LABoratory. It was developed in the 1970's at the University of New Mexico and Stanford out of the LINPACK and EISPACK Fortran matrix packages. Matlab basically works with one kind of object, a complex valued 2-D matrix. The number l for example is a l×l complex matrix with an imaginary part of 0. l×l matrices are usually designated scalars. l×N or N×l matrices are called vectors. Before we get any further, note that Matlab does not have 3-D (M×N×L) or larger matrices! Unlike some other math programs like Mathcad, Matlab is basically a high-level programming language with a built in interpreter. Some knowledge of programming is helpful when working with Matlab. Entering Simple Matrices Since Matlab works with matrices, we have to assign a variable name to a matrix for example if you type: a=[ l 2 3; 4 5 6; 7 8 9 ] when you hit return Matlab responds with a= 1 2 3 4 5 6 7 8 9 So, to enter a matrix we give it a variable name, an "=" sign, a left bracket "[" to tell it we're starting a matrix, the rows of the matrix separated by semicolons (and element per row separated by spaces) and ended with a right bracket "]". Well, what if we didn't use the semicolon, but hit return instead to enter a= [ 1 2 3 4 5 6 7 8 9 ] Matlab would return with: a= 1 2 3 4 5 6 7 8 9 So instead of using semicolons, we can just hit return. All right, what if we entered this: -2- a= [1, 2, 3;4, 5, 6; 7, 8, 9] Matlab would return with: a= 1 2 3 4 5 6 7 8 9 So, Matlab treats commas just like spaces. OK, now suppose we want to enter a scalar, like the number 143. We could treat it like a 1-D matrix and type in a=[1] and Matlab would respond a= 1 But, that seems like too much typing. So, Matlab is pretty smart and lets you just enter a=l Notice that I kept saying Matlab responds with ... that's because unless you tell Matlab otherwise, it will tell you what it put into the variable. That is ok for scalars and small vectors and matrices, but not for big ones. To keep Matlab from printing the result you can put a semicolon (;) at the end of the line. Complex Numbers and pi Matlab works with complex matrices. So, how does one enter a complex number? It's quite simple. In math notation you might write a=1+i. You can do the same with Matlab. To the input a=l+i Matlab responds a= 1.0000+1.0000i In fact you can even do a=1+3i and Matlab responds -5- b= 1 2 3 10 4 5 6 11 7 8 9 12 The transpose operator is something that you will probably use quite often. Arithmetic Operations ( +-*/^ ) Like most other programming languages Matlab uses the +, -, *, /, ^ symbols for addition, subtraction, multiplication, division and exponentiation. But note, since Matlab assumes matrices, it uses matrix operations. For scalars, however, they work just as you would expect. For example, the command c=2*(1+9)/5 yields the response c= 4 If I have a and r as above though and try to give the command b=a*r Matlab responds ??? Error using ==>* Inner matrix dimensions must agree. Matlab tried to multiply a 3×3 matrix by a 1×3 matrix. However, the command b=a*r' yields the response b= 68 167 266 Suppose that we want to multiply all the elements in one matrix by all the elements in another. To do that we put a period (.) before the * operator. (It works for division too). For example the command b=a(l,:).*r -6- yields b= 10 22 36 To square all the elements in the vector x for example you could give the commands y=x.*x or y=x.^2 To divide all the elements by 10, you would give the command “y=x/10;”. To get 1 over all the elements in x you would give the command y=1./x (note the space between 1 and the earlier versions of Matlab would mistake 1./x as the same as y=1.0/x which will give an error unless x is a scalar!) Since Matlab is designed for doing math, all the standard functions (sin, cos, tan, cosh, sinh, etc) are there. Type help elfun to get a list of elementary math functions. Generating Vectors You will often want to generate vectors of numbers, for example, generate a vector t with discrete times, or a vector f with discrete frequencies. To do this we can use our old friend the : operator. The general format for generating the vector is a=startnum: spacing: endnum For example the command a=0:0.01:1; generates a vector a containing the numbers 0 to 1 at an 0.01 spacing. (note the semicolon to keep all 100 elements from printing out) There are also two other useful commands, linspace and logspace. a= linspace(0,1,100); generates 100 linearly spaced elements between 0 and 1 this is useful for generating time domain signals. a=logspace(0,1,100); generates 100 logarithmically spaced elements between 10^0 and 10^1 this is often used for generating frequencies for fft's or frequency response functions. Saving, reloading and listing the workspace Since Matlab is usually not just used as a super-matrix calculator, you often have many variables and will want to save and reload your work. To get a listing of the names of all the variables you use the command who. To get a listing with more information you use the command whos. To save your workspace you use the command save filename and to reload it you use the command load filename. Help, paging and command recall -7- One nice feature of Matlab is online help for all the commands. To get help on the command called xxxxx you would type the command help xxxxx. The help that comes up is nearly as detailed as the manuals that come with Matlab. So, this means the online help is pretty good but the manuals aren't much better. As you start using help and getting larger workspaces you will notice that quite often help pages are larger than one screen full. On the unix and windows versions of Matlab you can use the built in pager to show you one screen at a time. To start the pager, give the command more on and to turn it off give the command more off. You can recall previous commands by pressing the up arrow. You can then edit the command and rerun it by pressing enter. For those of you familiar with emacs, you can also use most of the emacs line editing commands for editing (like ctrl-k, ctrl-e, ctrl-a). A recent addition to the Matlab help functions is one called lookfor. You give lookfor a word, and it searches the function descriptions to see which ones contain the word. So, if you are looking for a function to invert a matrix, you would type lookfor inverse and Matlab would tell you all the functions that have to do the inverse of things. Clearing variable and the workspace Once and a while you will want to clear out a variable so that you can use the name again, or you just want to clear out the whole workspace without exiting and starting Matlab back up again. To clear a variable, issue the command clear variablename. To clear the entire workspace use the command clear all by itself. BE CAREFUL-it wipes out all the variables in memory without confirming. Creating and using .m files Since Matlab basically is a programming language, it is easy to create and use program files. The simple method of using .m files is to just put all the commands that you would normally type in to the Matlab prompt into a file with a .m extension. Then, to run the commands in the file, you just type the name of the file (without the .m) and it runs it. Comments are denoted with % signs. Anything after a % sign is ignored. Many times you will not be able to fit an entire command on one line. To indicate that the command stretches to the next line, you place an ellipsis (3 or more periods) at the end of the line. For example, the following would be a valid command s=1-1/2+1/3-1/4+1/5-1/6.... +1/7-1/8 (note, the spaces before the +17 were added for clarity only, they don't need to be there, but they can be if you want them) You can also create functions with .m files. To see an example of how a function works, you can
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved