Download presentation

Presentation is loading. Please wait.

Published byHarry Hunt Modified over 4 years ago

1
What is MATLAB ? MATrix LABratory –Originally, it was a front-end to FORTRAN matrix routines developed in the 1970’s @ U. of New Mexico and Stanford –Today it is a powerful, interactive mathematics ENVIRONMENT with many powerful capabilities (matrices, symbolic math, analysis) Not unlike a UNIX shell

2
Support Materials MATLAB is available @ the ECE front desk, and at the ODU bookstore –The only way to master MATLAB is to use it (just like any programming language or skill) User’s Guide (comes with student edition) Internet FAQ’s (e.g. www.mathworks.com) MATLAB Primer (Bound copy ~$3.00)

3
Accessing Matlab Start Menu.. Programs.. Matlab To exit.. –>>quit

4
Entering Matrices MATLAB works with essentially one kind of object – a rectangular numerical MATRIX with possibly complex entries 1 x 1 interpreted as scalars 1 x n or m x 1 interpreted as vectors Entered by explicit list of elements, or Generated by built-in statements and functions Created in M-files Loaded from external data files

5
Entering matrices (contd.) Example A = [1,2,3; 4,5,6; 7,8,9] or A = [ –1 2 3 –4 5 6 –7 8 9 ] creates a 3 x 3 matrix and assigns it to a variable A., or blank separates the element in a matrix Avoid blank spaces while listing a number in exponential form (e.g. 2.34e-9) Large Matrix best done in M – file (easy to edit) Built in functions: rand, magic, hilb

6
rand (n) creates a n x n matrix with random entries uniformly distributed between 0 and 1 rand (m x n) will create an m x n matrix magic (n) will create a an integral n x n matrix which is a magic square hilb(n) will create the n x n Hilbert matrix Individual matrix and vector entries can be referenced with indices (only positive integers) within the parentheses –E.g. A(2,3) refers to entry in second row and third column. –X(3) woild denote third coordinate of a vector x. Entering matrices (contd.)

7
Matrix Operations Addition+ Substraction - Multiplicationx Power^ Transpose` Left Division\ Right division/ –E.g. x = A\bis the solution of A * x = b –x = b/A is the solution of x * A = b

8
Array Operations Addition & substraction Operate entrywise Other can be made entrywise by preceding them with a period – for *,^,\,/ –E. g. [1 2 3 4].*[1 2 3 4] will yield [1 4 9 16] –[1 2 3 4].^2 will yield [1 4 9 16] Useful in MATLAB graphics

9
Statements, Expressions & Variables MATLAB is an expression language – CASE SENSITIVE Statements are of the form –Variable = expression, or simply –Expression Expressions are composed from operators, functions, and variable names. Result is a Matrix assigned to the variable for future use. If variable name and = sign are omitted, then a variable ans (for answer) is created. Statement terminated with a CR, use … to continue to next line Same line use comma to separate statements Last character semicolon suppresses the printing Who – lists all the variables Clear – clears the variables Runaway Display can be stopped by CTRL-C

10
Matrix Building Functions Convenient Matrix Building Functions are –Eye –Zeros –Ones –Diag –Triu –Tril –Rand –Hilb –Magic –Toeplitz

11
For,While, if – and relations MATLAB flow control statements operate like those in most computer languages For –x =[]; for i = 1:4, x = [x,i^2],end –x =[]; for i = 4:-1:1, x = [x,i^2],end While –While relation –Statements –End If –If relation –Statements –end

12
Relations <less than >greater than <= less than or equal >= greater than or equal == equal ~= not equal & and | or ~ not

13
Scalar & Vector functions Scalar –Sinasinexpabsround –Cosacoslogsqrtfloor –Tanatanremsignceil Vector –Maxsummedianany –Minprodmeanall –Sortstd

14
Matrix Functions –Eigcholsvdinvluqr –Hessschurrrefexpmsqrtmpoly –Detsizenormcondrank

15
Command Line Editing & Recall Use left & right arrows Backspace & delete keys Home, end, Delete Up/Down arrow keys

16
Submatrices & Colon Notation To achieve fairly complex data manipulation Colon Notation (generate vectors and reference submatrices Expression 1:5 generates [1 2 3 4 5] Expressions 0.2:0.2:1.2 generates [0.2 0.4 0.6 0.8 1.0 1.2] Expression 5:-1:1 gives [5 4 3 2 1] X= [0.0:0.1:2.0]’;y=sin(x);[x,y] gives a table of sines Colon Notation – used to access submatrices of a matix –A(1:4,3), A(:,3), A(1:4,:), A(:, [2,4]) –A(:[2,4,5]) = B(:,1:3) –A(:[2,4]) = A(:,[2,4])*[1,2:3,4]

17
M files To execute a sequence of statements Script files –Sequence of normal MATLAB statements –Variables are global –Used to enter data into a large matrix –Entry errors can be easily edited Function files –Provide extensibility to MATLAB –Create new functions specific to a problem –Variables are local –We can however declare them global if so desired.

18
Function files Example function a = randint(m,n) a= floor (10 * rand(m,n) Place in a file called randint.m first line declares function name,input arguments and output arguments without this line the file would be a script file A statement z = randint(4,5) will pass 4,5 to m,n in the function file with the output result passed to variable z.

19
Function file (contd.) Example 2 –Function [mean, stdev] = stat (x); –[m,n] = size(x); –If m == 1 –M = n; –End –Mean = sum(x)/m –Stdev = sqrt (sum(x.^2)/m – mean.^2); % to write comment statements

20
Text Strings, error messages, input Hardcopy Text Strings – use single quotes Use disp to display text strings Use error to display error messages Use input to interactively input data Use diary to get a hardcopy –To turn off use diary off

21
Graphics Use plot to create linear x,y plots –x = -4:0.1:4; y = sin(x); plot (x,y) –x = -1.5:0.01:1.5; y = exp(-x.^2); plot (x,y) –t = 0:.001:2*pi;x=cos(3*t);y=sin(2*t),plot(x,y) Use shg to see the graphics screen Labelling –Titlexlabelylabelgtexttextaxis –Default is auto scaling Multiple plots –Hold Linetypes and pointtypes

22
Graphics (contd.) 3-D mesh plots –Use function mesh 3-D perspective of elements of matrix z Mesh (eye(10)) xx = -2:.1:2;yy=xx;[x,y] = meshdom(xx,yy);z = exp(-x.^2 - -y.^2);mesh(z)

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google