Sei sulla pagina 1di 32

MATLAB Workshop

Title Page
MATLAB Basics
Plotting

MATLAB Basics
Mohamed Taha
Communication Engineering Department
Princess Sumaya University
mtaha@psut.edu.jo

Page 1 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

MATLAB Basics

What is MATLAB
Title Page

It is an abbreviation to MATrix LABoratory


Front end for a matrix library

MATLAB Basics
Plotting

Page 2 of 32

It is an interpreted language

JJ

II

It contains huge amount of built in functions and


accept user defined functions

Compatible with almost all programming languages


(C ++ , JAVA, .net applications), etc.

- ,
Full Screen
Search

Memory allocations and de-allocations are done automatically

Close
MATLAB
Workshop
Lecture 1

MATLAB data structures and data types


Basic data structure is matrix
MATLAB calls matrices arrays

Title Page
MATLAB Basics
Plotting

Basic data type is double


Page 3 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

MATLAB Layout

Title Page
MATLAB Basics
Plotting

Page 4 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

Interactive computing with MATLAB


Using MATLAB as a calculator
ans is a default variable used if a variable is not
specified to hold the result
At the command prompt
>> 2+6-4*sin(4)
ans = 11.0272
If we assign the variable x the last statement

Title Page
MATLAB Basics
Plotting

Page 5 of 32

JJ

II

- ,
Full Screen

>> x = 2+6-4*sin(4)
x = 11.0272
ans disappeared

Search
Close
MATLAB
Workshop
Lecture 1

% sign is used to write a comment


% This is my first command
The last statement is totaly ignored by MATLAB
Title Page

; is used to suppress the result, useful when the


result is a huge matrix (image)
>>a=1;
To display the result
>> a or disp(a)

MATLAB Basics
Plotting

Page 6 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

MATLAB vectors
Row vector A = [1 0 1] is entered in MATLAB
as A=[1 0 -1]
spaces are used between the elements
commas can be used as well
>> A=[1 0 -1]
A = 1
0

-1

The size and data structure can be checked by


typing >> whose A

Title Page
MATLAB Basics
Plotting

Page 7 of 32

JJ

II

- ,
Full Screen

Name
A

Size
1x3

Bytes Class
24

double

Search
Close
MATLAB
Workshop
Lecture 1

Column vector A =
0
1
>> A=[1;0;-1]
A = 1
0
-1
To convert a row vector to a column vector, use
transpose operator
1
0
-1
>> B=A
B = 1

Title Page
MATLAB Basics
Plotting

Page 8 of 32

JJ

II

- ,
Full Screen

A =

Search
Close

-1

MATLAB
Workshop
Lecture 1

Matrices
Matrices are collection of column or row vectors
m n matrix, contains m rows and n columns
#
"
1
0
1
2
is entered as
The matrix A =
1 j 2
>> A=[1 1/2 0;-1 j 2]
A =
1.0000
0.5000
-1.0000
0 + 1.0000i

0
2.0000

Title Page
MATLAB Basics
Plotting

Page 9 of 32

JJ

II

- ,
Full Screen

Size of the matrix or vector can be returned as


Search

>> [m,n]=size(A)
m = 2
n = 3

Close
MATLAB
Workshop
Lecture 1

Accessing the elements of a matrix/vector


For a vector (column or row), elements can be accessed by specifying the number of the element
First element is a number 1 and not 0 in the array
A = [1
>> A(1)
ans = 1
>> A(2)
ans = 0

-1]

Title Page
MATLAB Basics
Plotting

Page 10 of 32

JJ

II

- ,
Full Screen

>> A(3)
ans = -1
>> A(end)
ans = -1

Search
Close
MATLAB
Workshop
Lecture 1

For matrix, elements are accessed by specifying their


locations in the row and column

1 1
5
A = j 0 2 + 3 j
1
2
7/2

Title Page
MATLAB Basics
Plotting

Page 11 of 32

>> A=[1 -1 5 ; -j 0 2+3*j ; 1 2 -7/2];

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

Elements of A are accessed as

Title Page
MATLAB Basics
Plotting

Page 12 of 32

JJ

II

- ,
Full Screen

Colon operator : is used to specify range of


columns and rows
1:3, produces [1 2 3]

Search
Close
MATLAB
Workshop
Lecture 1

Creating vectors automatically


It is much efficient to create matrices and vectors automatically
Functions used to create matrices can be used to create
vectors, opposite is not always possible.

Title Page
MATLAB Basics
Plotting

Page 13 of 32

For vectors:
x = linspace(StartVal , EndVal, N);
Creates a row vector of N linearly spaced numbers
between StartVal and EndVal
Increment = difference between any two consecal
utive numbers = EndV alStartV
N 1

JJ

II

- ,
Full Screen
Search

Creates 10 elements between 1 and 5


Close

>> x=linspace(-1,5,10);

MATLAB
Workshop
Lecture 1

The increment can be checked by subtracting any


two consecutive numbers in x
>> x(2)-x(1)
>> ans = 0.667
Exactly equals to

Title Page

5(1)
101

MATLAB Basics

= 0.667

x = StartVal : Increment : EndVal


Exactly the same as linspace
x=-1:0.667:5
Produces the same x produced previously by
linspace

Plotting

Page 14 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

Creating matrices automatically


Several functions
Function
eye(m,n)
zeros(m,n)
ones(m,n)
diag(m)
diag(m)
rand(m,n)
randn(m,n)
hilb(m)
magic(m)

Operation
m n identity matrix
m n matrix of zeros
m n matrix of ones
m m diagonal matrix
extract diagonals
m n randomly generated matrix
m n randomly generated matrix
m m Hilbert matrix
m m magic square

Title Page
MATLAB Basics
Plotting

Page 15 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

Matrix algebra
A B, A and B are vectors or matrices of the same
size

Title Page
MATLAB Basics

>> A=[1 0 -1; 1/2 j -3];


>> B=ones(2,3);
>> C=A-B;
A B, columns {A} = rows {B}
>> A=[1 0 -1; 1/2 j -3];
>> B=ones(2,3);
>> C=A*B;

Plotting

Page 16 of 32

JJ

II

- ,
Full Screen
Search

A. B, multiply the corresponding elements of A


and B, A and B must be of the same size

Close
MATLAB
Workshop
Lecture 1

>> A=[1 0 -1; 1/2 j -3];


>> B=ones(2,3);
>> C=A.*B
C =

Title Page
MATLAB Basics

1.0000
0.5000

0
0 + 1.0000i

-1.0000
-3.0000

A./B, divide the corresponding elements in A and


B, A and B must be of the same size
>> A=[1 0 -1; 1/2 j -3];
>> B=ones(2,3);
>> C=A./B
C =

Plotting

Page 17 of 32

JJ

II

- ,
Full Screen
Search
Close

1.0000
0
0.5000 0 + 1.0000i

-1.0000
-3.0000

MATLAB
Workshop
Lecture 1

Inverse of a matrix A, A is a square, full rank matrix


>> A=randn(3);
>> B=inv(A)
B =

Title Page
MATLAB Basics

0.1749
-0.2860
0.9737

-0.6997
0.1538
-0.2917

-0.7158
1.0568
-0.5160

Plotting

Page 18 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

Matrix functions

Title Page
MATLAB Basics
Plotting

Page 19 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

MATLAB built in variables


MATLAB has several built in variables
These variables name should not be used for user defined
variables

Title Page
MATLAB Basics
Plotting

Page 20 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

User defined variables


Should starts with characters a-z or A-Z
Can have any remaining characters

Title Page
MATLAB Basics
Plotting

Maximum length 31
Page 21 of 32

Should not have the name of a built in variable/function or a user defined function
A is different from a, case sensitive

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

Polynomials
Most engineering problems based on polynomial manipulations
Some functions

Title Page
MATLAB Basics
Plotting

Page 22 of 32

The polynomial f (x) = x2 + 2x + 1 is entered as


f=[1 2 1];
The polynomial g(x) = x2 + 1 is entered as
g=[1 0 1];
The value of g(0.5)
polyval(g,-0.5)

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

c(x) = f (x)g(x) is computed as


>> f=[1 2 1];
>> g=[1 0 1];
>> c=conv(f,g)

Title Page
MATLAB Basics
Plotting

c =

Page 23 of 32

The result is interpreted from right to left as


c(x) = 1 + 2x + 2x2 + 2x3 + x4

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

Vectorization
Transforming any problem to matrix-vector manipulation
Useful in engineering problems
In this case loops are avoided.
Consider the C-loop
pi=3.14;
N=5;
for (i=0; i < N ; i++)
{
x[i]=pi*i/4;
}
MATLAB version x=0:pi/4:pi
Uses

1
5

of the time required for the C-loop

Title Page
MATLAB Basics
Plotting

Page 24 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

Plotting
Plotting (x,y) data
Two dimensional plots are created with the plot
function.

Title Page
MATLAB Basics
Plotting

Page 25 of 32

plot(x,y)
plot(xdata,ydata,symbol)
plot(x1,y1,x2,y2,...)
plot(x1,y1,symbol1,x2,y2,symbol2,...)

x and y must have the same size, x1 and y1 must


have the same size, x2 and y2 must have the same
size, etc.

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

Different axis scaling


Name
loglog
plot
semilogx
semilogy

Axis scaling
log10 (y) versus log10 (x)
linear y versus x
linear y versus log10 (x)
log10 (y) versus linear x

Title Page
MATLAB Basics
Plotting

Page 26 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

Linear Scale

>>x=-10:0.1:10;
>>y=x.2;
>>plot(x,y)
>>grid

Title Page
MATLAB Basics
Plotting

Page 27 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

Log Scale

Title Page

>>x=linspace(0,1,10);
>>y=exp(2*x);
>>semilogy(x,y)

MATLAB Basics
Plotting

Page 28 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

Practice
1. Use colon operator to create a 100 elements vector
between 3 and 10 with uniform spacing between
elements.
2. For the vector created in (1), square all elements.
3. Create 4 4 normal random matrix, A. Find a new
matrix, B such that its columns are the diagonal of
the matrix A.

Title Page
MATLAB Basics
Plotting

Page 29 of 32

JJ

II

- ,
Ans. diag(A)*ones(1,4)

Full Screen
Search

4. Create a 3 3 uniform random matrix, X. Find


the eigen value decomposition of the matrix, X =
V V 1 .

Close
MATLAB
Workshop
Lecture 1

5. Create 1 100 random vector with zero mean and


unity variance. Check the values of the mean and
the variance using MATLAB commands.
Title Page
MATLAB Basics

6. You need to use the randn, mean, var functions.


To find help about these functions, at the command
prompt, type, help FunctionName.

7. Create the matrix

Plotting

Page 30 of 32

JJ

II

- ,

0.5

Write MATLAB commands to:

Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

Extract the elements of the second row


Interchange column 3 and column 1
Subtract row 1 from row 2 and store the result
in the second row
Multiply all elements in row 1 and all elements
in column 2 and store the result in row 1


1 3# 7 ,
8. Given the vectors x
=
"
3 1 6


2 4 2 , A =
y =
, and
5 2 7

1 4

B=
7 8 .
2 2
Determine which of the following statements will
correctly execute (and if not, state why). For the
correctly executed statements give the final result

Title Page
MATLAB Basics
Plotting

Page 31 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

using MATLAB.
1. x + y
5.x + A
9.A [x0 , y 0 ]

2.[x; y 0 ]
6.[x; y]
10.ones(1, 3) A

3.B A
7.B A
11.[x; y] + A

4.B./x
8.B./x

Title Page
MATLAB Basics
Plotting

Page 32 of 32

JJ

II

- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1

Potrebbero piacerti anche