Sie sind auf Seite 1von 64

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?
Matlab
High Level
Languages such as
C, Pascal etc.

Assembly

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 R2012b


Command Line
(Series of MATLAB
commands)

.m files
(Scripts, Functions)

.mat files
(Parameters,
Variables)

Introduction to MATLAB R2012b


No need for types. i.e.,
int a;
double b;
float c;

All variables are created with double precision unless


specified and they are matrices.
Example:
>>x=5;
>>x1=2;

After these statements, the variables are 1x1 matrices with


double precision

Array, Matrix
a vector

x = [1 2 5 1]

x =
1

a matrix

x = [1 2 3; 5 1 4; 3 2 -1]

x =
1
5
3

transpose

2
1
2

3
4
-1
y = x

y =
1
2
5
1

Long Array, Matrix

t =1:10
t =

k =2:-0.5:-1
k =

1.5

0.5

= [1:4; 5:8]

x =
1
5

2
6

3
7

4
8

-0.5

-1

10

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

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


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

x = A(1,:)
x=

c=x./y

d = x .^2

b=

c=
0.33 0.5 -3

d=

y = A(3 ,:)
y=

1 2 3

b = x .* y

3 8 -3
3 4 -1

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

1 4 9

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.
>>x=linspace(0,1,10);

Calculate y function which is given as


>>y=x;

Plot the y
>>plot(x,y)
>>grid

Ploting in MATLAB
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

Ploting in MATLAB
xlabel('x')
ylabel('y')
title(y=x')
and open the Figure 1 again.

Now write

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
y=x
1
0.9
0.8
0.7

0.6
0.5
0.4
0.3
0.2
0.1
0

0.1

0.2

0.3

0.4

0.5
x

0.6

0.7

0.8

0.9

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

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
else
Matlab 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
Some Dummy Examples

For loop syntax


for i=Index_Array
Matlab Commands
end

for i=1:100
Some Matlab Commands;
end
for j=1:3:200
Some Matlab Commands;
end
for m=13:-0.2:-21
Some 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 Example
function 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 example
function 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

For example

Another Example
x=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#1
1. Solve the following linear system
equation using Cramer Rule in MATLAB.

MATLAB HW#1
2. By applying

find the inverse of following matrix in MATLAB

Verify your result by using inv command.

MATLAB HW#1
3. Find a matrix X such that AX=B for the given A
and B matrices below

Das könnte Ihnen auch gefallen