Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
MATLAB Basics
Plotting
Page 2 of 32
It is an interpreted language
JJ
II
- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1
Title Page
MATLAB Basics
Plotting
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
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
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
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
>> [m,n]=size(A)
m = 2
n = 3
Close
MATLAB
Workshop
Lecture 1
-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
1 1
5
A = j 0 2 + 3 j
1
2
7/2
Title Page
MATLAB Basics
Plotting
Page 11 of 32
JJ
II
- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1
Title Page
MATLAB Basics
Plotting
Page 12 of 32
JJ
II
- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1
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
>> x=linspace(-1,5,10);
MATLAB
Workshop
Lecture 1
Title Page
5(1)
101
MATLAB Basics
= 0.667
Plotting
Page 14 of 32
JJ
II
- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1
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
Plotting
Page 16 of 32
JJ
II
- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1
Title Page
MATLAB Basics
1.0000
0.5000
0
0 + 1.0000i
-1.0000
-3.0000
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
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
Title Page
MATLAB Basics
Plotting
Page 20 of 32
JJ
II
- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1
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
JJ
II
- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1
Title Page
MATLAB Basics
Plotting
c =
Page 23 of 32
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
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,...)
JJ
II
- ,
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1
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
Close
MATLAB
Workshop
Lecture 1
Plotting
Page 30 of 32
JJ
II
- ,
0.5
Full Screen
Search
Close
MATLAB
Workshop
Lecture 1
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