Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf ·...

Preview:

Citation preview

Introduction to MATLAB R2012b

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

MATLAB R2012 Interface

Command Window

Basic Arithmetic Operations in Command Window

Command Window

Workspace

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

Command History

Current Folder Tab

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

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

MATLAB R2012 Interface: Home, Plots, Apps

MATLAB R2012 Interface:Home

MATLAB R2012 Interface:Home

Introduction to MATLAB R2012b: Editor

Introduction to MATLAB R2012b: Editor

Introduction to MATLAB R2012b

Introduction to MATLAB R2012b: Editor

Introduction to MATLAB R2012b: Functions

Introduction to MATLAB R2012b

Introduction to MATLAB R2012b

Introduction to MATLAB R2012b

Introduction to MATLAB R2012b

Introduction to MATLAB R2012b

Introduction to MATLAB R2012b

Introduction to MATLAB R2012b

Function Usage in MATLAB R2012b

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.

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.

Introduction to MATLAB R2012bCommand Line

(Series of MATLAB commands)

.mat files(Parameters,

Variables)

.m files (Scripts, Functions)

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;

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

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

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.

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.

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

Matrices Operations

Given A and B:

Addition Subtraction Product Transpose

Operators (Element by Element)

.*element-by-element multiplication

./ element-by-element division

.^element-by-element power

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

Determinant of a matrix

Power of a matrix

Power of a matrix

=

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

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

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?

Ploting in MATLAB

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.

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

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

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

Flow Control

• if • for • while • break

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

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

Control Structures

• While Loop Syntax

while (condition)Matlab Commands

end

Dummy Example

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

end

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

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

“For” example

for i=1:100

x(i)=i^2 ;

end

1

2 3

“For” example

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

Another Example

Another Example

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.

MATLAB HW#11. Solve the following linear system

equation using Cramer Rule in MATLAB.

MATLAB HW#12. By applying

find the inverse of following matrix in MATLAB

Verify your result by using “inv” command.

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

and B matrices below

Recommended