Document Sample

Lecture 2 - Matlab Introduction CVEN 302 May 30, 2001 Lecture Goals • Vectors Operations • Matrix Operations • Plot & Graphics • Matlab files • Matlab controls Vector A vector is defined as a combination of variables values to with components of xj , where j = 1,…n values. x x1 , x2 , x3 ,... x1 x xT 2 x3 Vectors • Matlab is designed for vector and matrix manipulation some of the basic commands are given as 1 3 t ,3,4,6 1 t 4 6 t [1 1 4 or t [1;4;6] t 4 6 6] Vectors • t‟ represents the transpose of the vector “t”. • Individual components can be represented by t = [ 4,5,6,9], where t(3) = 6. • [ ] represent the start and finish of the vector and/or matrix. ( ) represent components of the vector. • A period “.” represent an elemental set of functions, such as multiplication, division,etc. Vector element Operations • Individual addition A+B A+B • Individual subtraction A–B A-B • Individual multiplication A*B A.*B • Individual division (left) A/B A./B • Individual division (right) A\B A.\B • Individual power AB A.^B Hierarchy of the vector operations Precedence (1) - Parenthesis (2) - Exponential from left to right (3) - Multiplication and division from left to right. (4) - Addition and subtraction from left to right. Vector Operations • Vector product - A is 1 x n vector A * A' 1 x n * n x 1 1 x 1 A'*A n x 1* 1 x n n x n • The magnitude of the vector is a dot product of the vector. A * A' 1 x n * n x 1 1 x 1 A A * A' Vector Examples Matrix • A matrix is a two dimensional arrays, where the matrix B is represented by a [ m x n ] b1,1 b1,n B bm ,1 bm ,n Matrix Operations • For addition and subtraction, the matrix sizes must match up. If you are adding to each component of the matrix you can do a simple scalar addition. • Examples: [A] + [B] = [C] [A] + 3 = [D] Matrix Operation • Multiplication of matrices will need to match up the columns to the row values of the following matrix. Scalar multiplication will work. • Division is different. You will either divide member by member, where the matrices are the same size or you will need to find the inverse of the matrix. Matrix Multiplication Examples Graphical Representation • Matlab has a function known as “plot( ), where the values are plotted on an x-y plane. • General format of the graph is given as, plot(x,y,‟symbols‟) • The symbols represent the color, point shape, and the line type. Plot symbols commands Colors Symbols Lines y – yellow . – point - – solid line m – mag o – circle : – dots c – cyan x – xmark -. – line dot r – red + – plus - - – dashes g – green * – star b – blue s – square w – white d – diamond k – black v – triangle down ^ – triangle up < – left < – right p – pentagram h – hexagram Plot Commands t = linspace(0, 2*pi); - results in 100 data points y1 = cos(t); - cosine of the points y2 = sin(t); - sine of the points y3 = y1.*y2; - cos(t)*sin(t) plot(t,y1,‟-‟) ; - plots cosine verse t with a straight line. plot(t,y3,‟r:‟) - plots cosine*sine verse t with red dots. Plot Commands Example: plot(t,y1,‟-‟,t,y2,‟g*‟,t,y3,‟r-.‟) - plots all 3 axis( [0 2*pi -1.5 1.5]) - adds axes legend(„cos(t)‟,‟sin(t)‟,‟cos(t)*sin(t)‟) - legend Note that the [ ] represent an array and ( ) represent a function, and „ „ represent the symbols. Matlab commands for file management The files can be written as a script, which can be loaded into the memory. From the command line: • “echo” - causes the file to be echoed to the screen. • “what” - shows the type of file in the current directory. • “type” - will present show the file contents Matlab Files There are three types of files: • Data files – Matlab files have their own format for saving data – ascii files are standard text files, which can be printed out or used in excel files. • m-files represent the program files. • function files are functions similar to „sin(x)‟, cos(x), etc. Data Files Data files can be written in two forms: • Matlab format • ascii format Matlab data format MatLab generates a data t = linspace(0, 2*pi) file, which will have x = cos(t) look like: save data1 t x “filename”.mat clear what File can be loaded into the memory with: data1.mat load “filename” load data1 ACSII format An ascii file type can be t = linspace(0 2*pi) x = sin(t) created by adding a flag save data2.dat t x -ascii on the end of the save dir data2.dat command clear save “filename”.dat -ascii load data2.dat -ascii Loading Data Files Matlab Files ASCII Files load “filename” load “filename.dat” -ascii The file will load the file into The file will be loaded as an the same format as it was data array and will require saved. you to modify to obtained the data vectors. Data file example t=linspace(0,2*pi); (data1 will have created y1 = sin(x); t and y1 vectors.) save data1 t y1; clear save data2.dat t y1 -ascii clear (created data1.mat and will show up in home (created a ascii file with directory.) the data) load data1; load data2 -ascii Data file example continued (loaded an ascii file into memory as data2 array) whos data2 2X100 double array t= data2(1,:); y1= data2(2,:); : assigns the row to the vector Homework • Check the Homework files – create a data file – input a simple program – run the program – plot the results

DOCUMENT INFO

Shared By:

Categories:

Tags:
matlab commands, command window, vectors and matrices, the matrix, matlab environment, lecture notes, image processing, matlab variables, homework assignments, final project, data structures, matlab tutorial, office hours, matlab introduction, programming in matlab

Stats:

views: | 12 |

posted: | 7/30/2010 |

language: | English |

pages: | 26 |

OTHER DOCS BY ild18893

How are you planning on using Docstoc?
BUSINESS
PERSONAL

By registering with docstoc.com you agree to our
privacy policy and
terms of service, and to receive content and offer notifications.

Docstoc is the premier online destination to start and grow small businesses. It hosts the best quality and widest selection of professional documents (over 20 million) and resources including expert videos, articles and productivity tools to make every small business better.

Search or Browse for any specific document or resource you need for your business. Or explore our curated resources for Starting a Business, Growing a Business or for Professional Development.

Feel free to Contact Us with any questions you might have.