64
Introduction to MATLAB R2012b

Intro R2012b Slides

Embed Size (px)

DESCRIPTION

Matlab

Citation preview

Page 1: Intro R2012b Slides

Introduction to MATLAB R2012b

Page 2: Intro R2012b Slides

What is Matlab?• Matlab is basically a high level language which

has many specialized toolboxes for making things easier for us

• How high?

Assembly

High Level Languages such as

C, Pascal etc.

Matlab

Page 3: Intro R2012b Slides

MATLAB R2012 Interface

Page 4: Intro R2012b Slides

Command Window

Page 5: Intro R2012b Slides

Basic Arithmetic Operations in Command Window

Page 6: Intro R2012b Slides

Command Window

Page 7: Intro R2012b Slides

Workspace

• Double click on “m” variable (m is a 3x4 matrix)

Page 8: Intro R2012b Slides

Command History

Page 9: Intro R2012b Slides

Current Folder Tab

• We will not use current folder tab so oftenly, so minimize it for now.

Page 10: Intro R2012b Slides

Again, MATLAB R2012b General Interface (without “Current Folder” tab)

Page 11: Intro R2012b Slides

MATLAB R2012 Interface: Home, Plots, Apps

Page 12: Intro R2012b Slides

MATLAB R2012 Interface:Home

Page 13: Intro R2012b Slides

MATLAB R2012 Interface:Home

Page 14: Intro R2012b Slides

Introduction to MATLAB R2012b: Editor

Page 15: Intro R2012b Slides

Introduction to MATLAB R2012b: Editor

Page 16: Intro R2012b Slides

Introduction to MATLAB R2012b

Page 17: Intro R2012b Slides

Introduction to MATLAB R2012b: Editor

Page 18: Intro R2012b Slides

Introduction to MATLAB R2012b: Functions

Page 19: Intro R2012b Slides

Introduction to MATLAB R2012b

Page 20: Intro R2012b Slides

Introduction to MATLAB R2012b

Page 21: Intro R2012b Slides

Introduction to MATLAB R2012b

Page 22: Intro R2012b Slides

Introduction to MATLAB R2012b

Page 23: Intro R2012b Slides

Introduction to MATLAB R2012b

Page 24: Intro R2012b Slides

Introduction to MATLAB R2012b

Page 25: Intro R2012b Slides

Introduction to MATLAB R2012b

Page 26: Intro R2012b Slides

Function Usage in MATLAB R2012b

Page 27: Intro R2012b Slides

Functions in MATLAB

• You do not need always writing new functions.• There are also embedded functions in MATLAB.• Write “help” command in the command window

and see the list of help topics of MATLAB.• If you would like to get some knowledge for any

function, write “help function_name” in command window. For example, write “help factorial” .

• Now, use factorial function to compute factorial of 5 by using the given knowledge.

Page 28: Intro R2012b Slides

Functions in MATLAB

• Another example: Write “help linspace” in command window.

• Can you say what linspace command does ?Write in command window:

t=linspace(0,1,100)

What will the content of “t” be?

• There are lots of other functions in MATLAB.

Page 29: Intro R2012b Slides

Introduction to MATLAB R2012bCommand Line

(Series of MATLAB commands)

.mat files(Parameters,

Variables)

.m files (Scripts, Functions)

Page 30: Intro R2012b Slides

Introduction to MATLAB R2012b• No need for types. i.e.,

• All variables are created with double precision unless specified and they are matrices.

• After these statements, the variables are 1x1 matrices with double precision

int a;double b;float c;

Example:>>x=5;>>x1=2;

Page 31: Intro R2012b Slides

Array, Matrix• a vector x = [1 2 5 1]

x =1 2 5 1

• a matrix x = [1 2 3; 5 1 4; 3 2 -1]

x =1 2 35 1 43 2 -1

• transpose y = x’ y =1251

Page 32: Intro R2012b Slides

Long Array, Matrix • t =1:10

t =1 2 3 4 5 6 7 8 9 10

• k =2:-0.5:-1

k =2 1.5 1 0.5 0 -0.5 -1

• B = [1:4; 5:8]

x =1 2 3 45 6 7 8

Page 33: Intro R2012b Slides

Matrix Index• The matrix indices begin from 1 (not 0 (as in C))• The matrix indices must be positive integer

Given:

A(-2), A(0)

Error: ??? Subscript indices must either be real positive integers or logicals.

A(4,2)Error: ??? Index exceeds matrix dimensions.

Page 34: Intro R2012b Slides

Concatenation of Matrices• x = [1 2], y = [4 5], z=[ 0 0]

A = [ x y]

1 2 4 5

B = [x ; y]

1 2

4 5

C = [x y ;z] Error:??? Error using ==> vertcat CAT arguments dimensions are not consistent.

Page 35: Intro R2012b Slides

Operators (arithmetic)+ addition- subtraction* multiplication/ division^ power‘ complex conjugate transpose

Page 36: Intro R2012b Slides

Matrices Operations

Given A and B:

Addition Subtraction Product Transpose

Page 37: Intro R2012b Slides

Operators (Element by Element)

.*element-by-element multiplication

./ element-by-element division

.^element-by-element power

Page 38: Intro R2012b Slides

The use of “.” – “Element” Operation

K= x^2Erorr:??? Error using ==> mpower Matrix must be square.B=x*yErorr:??? Error using ==> mtimes Inner matrix dimensions must agree.

A = [1 2 3; 5 1 4; 3 2 1]A =

1 2 35 1 43 2 -1

y = A(3 ,:)

y= 3 4 -1

b = x .* y

b=3 8 -3

c = x . / y

c= 0.33 0.5 -3

d = x .^2

d= 1 4 9

x = A(1,:)

x=1 2 3

Page 39: Intro R2012b Slides

Determinant of a matrix

Page 40: Intro R2012b Slides

Power of a matrix

Page 41: Intro R2012b Slides

Power of a matrix

=

Page 42: Intro R2012b Slides

Ploting in MATLAB

• Create an x-array of 10 samples between 0 and 1.

• Calculate y function which is given as

• Plot the y

>>x=linspace(0,1,10);

>>y=x;

>>plot(x,y)>>grid

Page 43: Intro R2012b Slides

Ploting in MATLAB

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Page 44: Intro R2012b Slides

Ploting in MATLAB

• Now write xlabel('x') ylabel('y') title(‘y=x')

and open the Figure 1 again.

Can you see labels on the axis of plot and title?

Page 45: Intro R2012b Slides

Ploting in MATLAB

Page 46: Intro R2012b Slides

Ploting in MATLAB

• Write in command window “hold” now.• This command will overwrite the new plot on

to current figure without delete it. • At this step, we will use “stem” command

instead of plot.• Now, write stem(x,y, 'r');• Open Figure 1 again.

Page 47: Intro R2012b Slides

Ploting in MATLAB

Open a new figure by using figure comand and do the same plot for 100 points.

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1y=x

y

x

Page 48: Intro R2012b Slides

Ploting in MATLAB

• There are several properties of plot command.• To learn these properties, remember that

there is a help command in MATLAB.• Learn how to use the commands related to

plot command such as axis, subplot, ...

Page 49: Intro R2012b Slides

Operators (relational, logical)

• == Equal to• ~= Not equal to• < Strictly smaller• > Strictly greater• <= Smaller than or equal to• >= Greater than equal to• & And operator• | Or operator

Page 50: Intro R2012b Slides

Flow Control

• if • for • while • break

Page 51: Intro R2012b Slides

Control Structures • If Statement Syntax

if (Condition_1)Matlab Commands

elseif (Condition_2)Matlab Commands

elseif (Condition_3)Matlab Commands

elseMatlab Commands

end

Some Dummy Examples

if ((a>3) & (b==5))Some Matlab Commands;

end

if (a<3)Some Matlab Commands;

elseif (b~=5) Some Matlab Commands;

end

if (a<3)Some Matlab Commands;

else Some Matlab Commands;

end

Page 52: Intro R2012b Slides

Control Structures

• For loop syntax

for i=Index_ArrayMatlab Commands

end

Some Dummy Examples

for i=1:100Some Matlab Commands;

end

for j=1:3:200Some Matlab Commands;

end

for m=13:-0.2:-21Some Matlab Commands;

end

for k=[0.1 0.3 -13 12 7 -9.3]Some Matlab Commands;

end

Page 53: Intro R2012b Slides

Control Structures

• While Loop Syntax

while (condition)Matlab Commands

end

Dummy Example

while ((a>3) & (b==5))Some Matlab Commands;

end

Page 54: Intro R2012b Slides

If Examplefunction compare(x,y)

if x>y

display('x is greater than y');

else if x<y

display('y is greater than x');

else

display('x is equal to y');

end % Ending for else if

end % Ending for if

Page 55: Intro R2012b Slides

If examplefunction compare(x,y)

if x>y

display('x is greater than y');

else if x<y

display('y is greater than x');

else

display('x is equal to y');

end % Ending for else if

end % Ending for if

Page 56: Intro R2012b Slides

“For” example

for i=1:100

x(i)=i^2 ;

end

1

2 3

Page 57: Intro R2012b Slides

“For” example

Page 58: Intro R2012b Slides

Another Examplex=10*randn(1,100);

for i=1:100

if x(i) > 0

y(i)=1;

elseif x(i)<0

y(i)=-1;

else

y(i)=0;

end

end

Page 59: Intro R2012b Slides

Another Example

Page 60: Intro R2012b Slides

Another Example

Page 61: Intro R2012b Slides

Another Example

• Now, get the same result of y by using the MATLAB function “sign(x)”

• Write in command window “help sign”.• Learn how to use sign command and use it for

determining the sign of elements of x.

Page 62: Intro R2012b Slides

MATLAB HW#11. Solve the following linear system

equation using Cramer Rule in MATLAB.

Page 63: Intro R2012b Slides

MATLAB HW#12. By applying

find the inverse of following matrix in MATLAB

Verify your result by using “inv” command.

Page 64: Intro R2012b Slides

MATLAB HW#13. Find a matrix X such that AX=B for the given A

and B matrices below