A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M...

17
AL IMAM MOHAMMAD BIN SAUD ISLAMIC UNIVERSITY COLLEGE OF SCIENCES DEPARTMENT OF MATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB

Transcript of A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M...

Page 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.

AL IMAM MOHAMMAD BIN SAUD ISLAMIC UNIVERSITY

COLLEGE OF SCIENCESDEPARTMENT OF MATHEMATICS

MATLAB 251 : MATH SOFTWARE

Introduction to MATLAB

Page 2: 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.1 First Steps :To allow MATLAB to carry out 1 + 1, type the following at the prompt:>> 1+1 (enter)MATLAB responds withans =

2The 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*ansans =

4Here 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.

Page 3: 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.

INTRODUCTION TO MATLAB

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

Page 4: 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.

INTRODUCTION TO MATLAB

2.1.2 MatricesThe basic object that MATLAB deals with is a matrix. A matrix is anarray 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-vectorcontaining 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.

i12 3 9

-1-1200 0 1e6 1 2 4 42

-i1sqrt(2) pi 3 1

6

Page 5: 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.

INTRODUCTION TO MATLAB

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’

Page 6: 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.

INTRODUCTION TO MATLAB

These are invalid assignments:

>> 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 = 2a =

2Try 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

Page 7: 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.1.4 How to generate a series of numbersTo 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

INTRODUCTION TO MATLAB

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:5ans = 1 2 3 4 5

>>u =1:2:10u = 1 3 5 7 9

Page 8: 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.

INTRODUCTION TO MATLAB

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.0000x =

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 100points.

If the window is small

Page 9: 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.

INTRODUCTION TO MATLAB

2.2 Typing into MATLAB2.2.1 Command Line Editing

If you make a mistake when entering a MATLAB command, you do nothave to type the whole line again. The arrow keys can be used to savemuch 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 cursorBackspace ctrl-h Delete character before cursor ctrl-k Delete (kill) to end of line

Page 10: 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.

INTRODUCTION TO MATLAB

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

.

Page 11: 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.

INTRODUCTION TO MATLAB

2.2.2 Long LinesIf 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 longvariable names:

Or to define a long text string:

>>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;]’.

2.2.3 Copying and PastingYour 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.

Page 12: 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.

INTRODUCTION TO MATLAB

2.2.4 Basic Datatypesdouble: Floating point number, the most common datatype in MATLAB

➔Other numeric datatypes: float, int32, uint8, ... rather uncommon

>>a = 1; whos ('a');Name Size Bytes Classa 1x1 8 double arrayGrand total is 1 element using 8 bytes

Complex numbers:

Warning: “i” can beredefined

)overwritten!(

>>a = complex(1,-2)a=

1.0000 - 2.0000i

>>a = 1-2*ia=

1.0000 - 2.0000i

Strings: >>a = ['test123']

a= test123

Page 13: 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.

INTRODUCTION TO MATLAB

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]; >>aa=

b: 2 c: [2 3 4]

Page 14: 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.

INTRODUCTION TO MATLAB

2.3 Matrices2.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 34 5 67 8 9

>>a = [1 2 3 4 5 67 8 9]

a= 1 2 34 5 67 8 9

Page 15: 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.3.2 Arithmetic operators A. For scalars

OperotorDescriptionExample

+Addition5+3

-Subtraction4-2

*.Multiplication3*.4

/.Right Division4/.2

\.Left Division2\.4

^.Power2^.5

Order of precedence:1 .Parenthesis

2 .Exponentiation3 .Multiplication and Division

4 .Addition and Subtraction

INTRODUCTION TO MATLAB

Page 16: 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.

INTRODUCTION TO MATLAB

Order of precedence: 1 .Parenthesis

2 .Exponentiation 3 .Multiplication and Division

4 .Addition and Subtraction 5 .Column operator

B. For matrices

OperatorDescriptionExample

+AdditionA+B

-SubtractionA-B

*MultiplicationA*B

/Right divisionA/B

\Left divisionB\A

^PowerA^2

’.TransposeA’.

:Column opertorA(:,1:3)

Page 17: 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.

INTRODUCTION TO MATLAB

C. Operator PrecedenceYou 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 (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to)=~(

8 .Element-wise AND)&( 9 .Element-wise OR)|(