Presentation is loading. Please wait.

Presentation is loading. Please wait.

A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.

Similar presentations


Presentation on theme: "A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB."— Presentation transcript:

1 A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB

2 2.1 First Steps : To allow MATLAB to carry out 1 + 1, type the following at the prompt: >> 1+1 (enter) MATLAB responds with ans = 2 The result is logged in variable called “ans”. In fact “ans” is now a variable that you can use again. For example you can type “ ans*ans” to check that 2 × 2 = 4: >> ans*ans ans = 4 Here MATLAB has updated the value of “ans” to be “4”. The variable “ans” is the MATLAB default variable. It changes each time that you execute a command.

3 I NTRODUCTION TO M ATLAB Remark : The spacing of operators in formulae does not matter. The following formulas both give the same answer: 3 * 2 – 1 / 2 * 4 + 1>> >> 1 +3 * 2 -1 / 2 * 4 The order of operations is made clearer to readers of your MATLAB code if you type carefully: >>1+3 * 2 – (1 / 2) * 4

4 I NTRODUCTION TO M ATLAB 2.1.2 Matrices The basic object that MATLAB deals with is a matrix. A matrix is an array of numbers. For example the following are matrices: The size of a matrix is the number of rows by the number of columns. The first matrix is a 3 ×3 matrix. The (2,3)-element is one million “1e6” that stands for 1 × 10, and the (3,2)-element is pi = π = 3.14159... The second matrix is a row-vector, the third matrix is a column-vector containing the number i, which is a pre-defined MATLAB variable equal to the square root of −1. The last matrix is a 1 × 1 matrix, also called a scalar.

5 I NTRODUCTION TO M ATLAB 2.1.3 Variables and assignments Variable_name = numerical value or computable Expression  Variables in MATLAB are named objects and are assigned using the equals sign “=“. They can contain  upper and lowercase letters  maximum name length of 63 characters  any number of ‘_’ characters  numerals.  They must start with a letter.  Matlab is case sensitive: A and a are different variables.  Don’t use a MATLAB built-in functions as a variable The following are valid MATLAB variable assignments: >> a = 1 >> speed = 1500 >> BeamFormerOutput_Type1 = v*Q*v’ >> name = ’John Smith’ >> a = 1 >> speed = 1500 >> BeamFormerOutput_Type1 = v*Q*v’ >> name = ’John Smith’

6 I NTRODUCTION TO M ATLAB These are invalid assignments: >> 42for1 = ’yes’ >> first one = 2 >> clear=4*3,pi=72, ans=pi*5 >> 42for1 = ’yes’ >> first one = 2 >> clear=4*3,pi=72, ans=pi*5 To assign a variable without getting an echo from MATLAB we use the co semi-colon character “ ;”. Compare the followings: >> a =2 ; >> >> a =2 ; >> >> a = 2 a = 2 >> a = 2 a = 2 Try typing the following: >> a = 2;b = 3; >> c = a+b; >> who Lists the variables currently in the memory >> whos Same as who with information about their bytes and class >> clear Removes all variables from the memory >> home Clean the command window display >> clc Clean the command window display

7 2.1.4 How to generate a series of numbers To generate a series of numbers we write all the numbers in square brackets and we separate them either using commas or blanks. It is your turn! Try some examples yourselves I NTRODUCTION TO M ATLAB 2.1.5 How to generate a series of numbers (The Colon Operator “:”) To generate a vector of equally-spaced elements MATLAB provides the colon operator “:”  The syntax “x:y” means roughly “generate the ordered set of numbers from x to y with increment 1 (default value) between them.”  The syntax “x:d:y” means roughly “generate the ordered set of numbers from x to y with increment d between them. >>[1 2 3 4 5] ans = 1 2 3 4 5 >>[1,2,3,4,5] ans = 1 2 3 4 5 >>1:5 ans = 1 2 3 4 5 >>u =1:2:10 u = 1 3 5 7 9

8 I NTRODUCTION TO M ATLAB 2.1.6 The command “Linspace” To generate a vector of evenly spaced points between two end points, you can use the command linspace( startpoint,endpoint,npoints ) >> x = linspace(0,1,10) x = Columns 1 through 8 0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 Columns 9 through 10 0.8889 1.0000 x = 0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 0.8889 1.0000 The previous command generates 10 evenly spaced points from 0 to 1. Typing linspace( startpoint,endpoint) will generate a vector of 100 points. If the window is small

9 I NTRODUCTION TO M ATLAB 2.2 Typing into MATLAB 2.2.1 Command Line Editing If you make a mistake when entering a MATLAB command, you do not have to type the whole line again. The arrow keys can be used to save much typing: ↑ ctrl-p Recall previous line ↓ ctrl-n Recall next line ← ctrl-b Move back one character → ctrl-f Move forward one character ctrl- → ctrl-r Move right one word ctrl- ← ctrl-l Move left one word home ctrl-a Move to beginning of line end ctrl-e Move to end of line esc ctrl-u Clear line del ctrl-d Delete character at cursor Backspace ctrl-h Delete character before cursor ctrl-k Delete (kill) to end of line ↑ ctrl-p Recall previous line ↓ ctrl-n Recall next line ← ctrl-b Move back one character → ctrl-f Move forward one character ctrl- → ctrl-r Move right one word ctrl- ← ctrl-l Move left one word home ctrl-a Move to beginning of line end ctrl-e Move to end of line esc ctrl-u Clear line del ctrl-d Delete character at cursor Backspace ctrl-h Delete character before cursor ctrl-k Delete (kill) to end of line

10 I NTRODUCTION TO M ATLAB Remark 1: If you finish editing in the of a line, you do not have to put cursor at the end of the line before pressing the return key; you can press when the cursor is anywhere on the command line Remark 2: Repeated use of the ↑ key recalls earlier commands. If you type the first few characters of a previous command and then press the ↑ key. MATLAB will recall the last command that began with those characters. Subsequent use of ↑ will recall earlier commands that began with those characters.

11 I NTRODUCTION TO M ATLAB 2.2.2 Long Lines If you want to type a MATLAB command that is too long to fit on one line, you can continue on to the next by ending with a space followed by three full stops “…” For example, to type an expression with long variable names: Or to define a long text string: >> Final_Ans = Matrix(row_indices,column_indices) +... Another_vector*SomethingElse; >> Final_Ans = Matrix(row_indices,column_indices) +... Another_vector*SomethingElse; >> Mission = [’DST’’s objective is to give advice that’... ’is professional, impartial and informed on the’... ’application of science and technology that is best’... ’suited to French’’s defence and security needs.’]; >> Mission = [’DST’’s objective is to give advice that’... ’is professional, impartial and informed on the’... ’application of science and technology that is best’... ’suited to French’’s defence and security needs.’]; 2.2.3 Copying and Pasting Your windowing system’s copy and paste facility can be used to enter text into the MATLAB command line. For example all of MATLAB’S built-in commands have some helpful text that can by accessed by typing help followed by the name of the command. Try typing help contour into MATLAB and you will see a description of how to create a contour plot. At the end of the help message is an example. You can use the mouse to select the example text and paste it into the command line. Try it now and you should see a contour plot appear in the figure window.

12 I NTRODUCTION TO M ATLAB 2.2.4 Basic Datatypes  double: Floating point number, the most common datatype in MATLAB ➔ Other numeric datatypes: float, int32, uint8,... rather uncommon >> a = 1; whos ('a'); Name Size Bytes Class a 1x1 8 double array Grand total is 1 element using 8 bytes  Complex numbers: Warning: “i” can be redefined (overwritten)! >> a = complex(1,-2) a = 1.0000 - 2.0000i >> a = 1-2*i a = 1.0000 - 2.0000i  Strings: >> a = ['test123'] a = test123

13 I NTRODUCTION TO M ATLAB 2.2.5 Advanced Datatypes  Cell Arrays: Generalised matrix ➔ Elements can have differing datatypes ➔ Elements can have different sizes  Structures ➔ Combine various data in a 'field/value' type structure ➔ Fields and associated datatypes do not have to be declared (and can change at run-time) >> a = {[1,2,3], 'test123'} a = [1x3 double] 'test123‘ >> a.b = 2; a.c = [2,3,4]; >> a a = b: 2 c: [2 3 4]

14 I NTRODUCTION TO M ATLAB 2.3 Matrices 2.3.1 Typing Matrices To type a matrix into MATLAB you must  begin with a left square bracket [  separate elements in a row with commas or spaces  use a semicolon “;” to separate rows or the “enter” key  end the matrix with the right square bracket ]. >>a = [1 2 3;4 5 6;7 8 9] a = 1 2 3 4 5 6 7 8 9 >>a =[1,2,3;4,5,6;7,8,9] a = 1 2 3 4 5 6 7 8 9 >>a = [1 2 3 4 5 6 7 8 9] a = 1 2 3 4 5 6 7 8 9

15 2.3.2 Arithmetic operators A. For scalars ExampleDescriptionOperotor 5+3Addition+ 4-2Subtraction- 3.*4Multiplication.* 4./2Right Division./ 2.\4Left Division.\ 2.^5Power.^ Order of precedence: 1. Parenthesis 2. Exponentiation 3. Multiplication and Division 4. Addition and Subtraction I NTRODUCTION TO M ATLAB

16 Order of precedence: 1. Parenthesis 2. Exponentiation 3. Multiplication and Division 4. Addition and Subtraction 5. Column operator B. For matrices ExampleDescriptionOperator A+BAddition+ A-BSubtraction- A*BMultiplication* A/BRight division/ B\ALeft division\ A^2Power^ A.’Transpose.’ A(:,1:3)Column opertor:

17 I NTRODUCTION TO M ATLAB C. Operator Precedence You can build expressions that use any combination of arithmetic, relational, and logical operators. Precedence levels determine the order in which MATLAB evaluates an expression. Within each precedence level, operators have equal precedence and are evaluated from left to right. The precedence rules for MATLAB operators are shown in this list, ordered from highest precedence level to lowest precedence level: 1. Parentheses () 2. Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^) 3. Unary plus (+), unary minus (-), logical negation (~) 4. Multiplication (.*), right division (./), left division(.\), matrix multiplication (*), matrix right division (/), matrix left division (\) 5. Addition (+), subtraction (-) 6. Colon operator (:) 7. Less than ( ), greater than or equal to (>=), equal to (==), not equal to (~=) 8. Element-wise AND (&) 9. Element-wise OR (|)


Download ppt "A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB."

Similar presentations


Ads by Google