OSU ENGR H192 - Lecture 18 - Intro to MATLAB

Unformatted text preview:

Introduction to MATLABMATLABSlide 3Slide 4MATLAB Variable NamesMATLAB Special VariablesMATLAB Math & Assignment OperatorsOther MATLAB symbolsMATLAB MatricesSlide 10Slide 11Slide 12Slide 13Extracting a Sub-MatrixSlide 15Slide 16Reading Data from filesPlotting with MATLABSlide 19Slide 20Some Useful MATLAB commandsSlide 22A Useless, But Interesting, MATLAB commandMATLAB Relational OperatorsMATLAB Logical OperatorsMATLAB Logical FunctionsMATLAB Display formatsMATLAB Selection StructuresMATLAB Repetition StructuresScalar - Matrix AdditionScalar - Matrix SubtractionScalar - Matrix MultiplicationScalar - Matrix DivisionLect 18 P. 1 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionIntroduction to MATLABLecture 18Lect 18 P. 2 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionMATLAB•MATLAB is a program for doing numerical computation. It was originally designed for solving linear algebra type problems using matrices. It’s name is derived from MATrix LABoratory.•MATLAB has since been expanded and now has built-in functions for solving problems requiring data analysis, signal processing, optimization, and several other types of scientific computations. It also contains functions for 2-D and 3-D graphics and animation.Lect 18 P. 3 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionMATLAB•The MATLAB environment is command oriented somewhat like UNIX. A prompt appears on the screen and a MATLAB statement can be entered. When the <ENTER> key is pressed, the statement is executed, and another prompt appears. •If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results will appear before the next prompt.•The following slide is the text from a MATLAB screen.Lect 18 P. 4 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionMATLAB To get started, select MATLAB Help or Demos from the Help menu.EDU >> a=5;EDU >> b=a/2b = 2.5000EDU >>Lect 18 P. 5 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionMATLAB Variable Names•Variable names ARE case sensitive•Variable names can contain up to 63 characters (as of MATLAB 6.5 and newer)•Variable names must start with a letter followed by letters, digits, and underscores.Lect 18 P. 6 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionMATLAB Special Variablesans Default variable name for resultspi Value of  eps Smallest incremental numberinf InfinityNaN Not a number e.g. 0/0i and j i = j = square root of -1realmin The smallest usable positive real numberrealmax The largest usable positive real numberLect 18 P. 7 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionMATLAB Math & Assignment OperatorsPower ^ or .^ a^b or a.^bMultiplication * or .* a*b or a.*bDivision / or ./ a/b or a./b or \ or .\ b\a or b.\aNOTE: 56/8 = 8\56 - (unary) + (unary)Addition + a + bSubtraction - a - bAssignment = a = b (assign b to a)Lect 18 P. 8 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionOther MATLAB symbols>> prompt. . . continue statement on next line, separate statements and data% start comment which ends at end of line; (1) suppress output(2) used as a row separator in a matrix : specify rangeLect 18 P. 9 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionMATLAB Matrices•MATLAB treats all variables as matrices. For our purposes a matrix can be thought of as an array, in fact, that is how it is stored. •Vectors are special forms of matrices and contain only one row OR one column.•Scalars are matrices with only one row AND one columnLect 18 P. 10 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionMATLAB Matrices•A matrix with only one row AND one column is a scalar. A scalar can be created in MATLAB as follows:EDU» a_value=23a_value = 23Lect 18 P. 11 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionMATLAB Matrices•A matrix with only one row is called a row vector. A row vector can be created in MATLAB as follows (note the commas):EDU>> rowvec = [12 , 14 , 63]rowvec = 12 14 63Lect 18 P. 12 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionMATLAB Matrices•A matrix with only one column is called a column vector. A column vector can be created in MATLAB as follows (note the semicolons):ED>> colvec = [13 ; 45 ; -2]colvec = 13 45 -2Lect 18 P. 13 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionMATLAB Matrices•A matrix can be created in MATLAB as follows (note the commas AND semicolons):EDU>> matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9]matrix = 1 2 3 4 5 6 7 8 9Lect 18 P. 14 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionExtracting a Sub-Matrix•A portion of a matrix can be extracted and stored in a smaller matrix by specifying the names of both matrices and the rows and columns to extract. The syntax is: sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ; where r1 and r2 specify the beginning and ending rows and c1 and c2 specify the beginning and ending columns to be extracted to make the new matrix.Lect 18 P. 15 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionMATLAB Matrices•A column vector can be extracted from a matrix. As an example we create a matrix below:EDU>> matrix=[1,2,3;4,5,6;7,8,9]matrix = 1 2 3 4 5 6 7 8 9•Here we extract column 2 of the matrix and make a column vector:EDU>> col_two=matrix( : , 2)col_two = 2 5 8Lect 18 P. 16 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionMATLAB Matrices•A row


View Full Document

OSU ENGR H192 - Lecture 18 - Intro to MATLAB

Documents in this Course
Strings

Strings

22 pages

Load more
Download Lecture 18 - Intro to MATLAB
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 18 - Intro to MATLAB and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 18 - Intro to MATLAB 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?