2 Intro2Matlab
2 Intro2Matlab
Contents
• What is Matlab
• Matlab basic
• Advanced usage of Matlab
- plots
- loops
- working with m files
- advanced functions
Contents
• What is Matlab
• Matlab basic
• Advanced usage of Matlab
What is Matlab
• Matrix laboratory
• easy to learn
• robust
• powerful functions
Contents
• What is Matlab
• Matlab basic
• Advanced usage of Matlab
Get Help
• Help
- help
- help function name (help abs)
Basic Math Operations
• To compute a math expression composed of numbers
and mathematics operators
• + addition
• - subtraction
• * multiplication
• / division
• ^ power
• Compute (2+3-9)*7^2/4 = -49
General Math Expressions
• To compute a math expression composed of
numbers, variables and mathematics operators
• Define all variables
• The others are the same
• E.g. x=3;y=6;x+y=? It is equivalent to 3+6
Exercise in Class (1)
• x=123;y=234;z=345
• Write the command expression and get the result:
1) the sum of x and y
2) subtract y from z
3) the multiplication of x and z
• Answer
1) x+y=357;
2) z-y=111;
3) x*z=42435
Vectors and Matrices
• Arrays are represented by some elements
within brackets
• The elements of each row is separated by at
least one space
• The separation of rows is by semicolons
• E.g. [2 3;1 2]
How to Retrieve Elements of Matrix 1
1 2 3 4
5 6 7 8
x
9 10 11 12
13 14 15 16
x=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]
What is the difference between x(2), x(1,2),
x(:,2), x(2,:), x(1:2,2)?
How to Retrieve Elements of Matrix 2
• Index begins from 1, not 0
• For matrix x:
• x(i) is the ith element, counted along the column
• x(i,j) is the element at the ith row and jth column
• x(:,j) is all the elements at the jth column
• x(i,:) is all the elements at the ith row
• x(i1:i2,j) is those elements at the jth column, begins from
index i1 to i2
Vector and Matrix Operations
• +, -, *, / are used in operations between
matrices
• .*, ./, .^ are used in operations between matrix
elements
• Pay attention to the dimensions of vectors and
matrices
Exercise in Class (2)
• x=[1 2;3 4];y=[5 6;7 8];
• z1=[1 2 3];z2=[4 5 6];
1) Sum of x and y
2) Product of x and y
3) Bitwise product of x and y
4) Bitwise division of z1 over z2
5) Bitwise division of z1 over z2, but for the first two elements
only (both the expression and result)
Answer:
1) [6 8;10 12];
2) [19 22;43 50];
3) [5 12;21 32];
4) [0.25 0.4 0.5];
5) z1(1:2)./z2(1:2)=[0.25 0.4]
Matrix Transpose
• More operators other than +, -, *, /, ^
• Matrix transpose: ’(apostrophe)
Contents
• What is Matlab
• Matlab basic
• Advanced usage of Matlab
Plot
• plot(y) plots the columns of y versus their index
• plot(x,y) plots vector y versus vector x
• plot(x,y,s) plots vector y versus vector x, and s is a
character string which states the line properties
• plot(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the
plots defined by the (X,Y,S) triples, where the X's
and Y's are vectors or matrices and the S's are strings.
Color Options
- Yellow - ‘y’
– Magenta - ‘m’
– Cyan - ‘c’
– Red - ‘r’
– Green - ‘g’
– Blue - ‘b’
– Black - ‘k’
Line Styles
-- dashed line
: dotted line
-. dash-dot line
Line Markings
• + - plus sign
• o - circle
• * - asterisk
• . - Point
• x - cross
• s - square
• d - diamond
• ^ - upward pointing triangle
• v - downward pointing triangle
• > - right pointing triangle
• < - left pointing triangle
• p - five-pointed star (pentagram)
• h - six-pointed star (hexagram)
t=0:0.1:4*pi;
x=sin(t);
y=cos(t);
plot(t,x,'r+-',t,y,'bo-.');
grid on;
xlabel('The Time');
ylabel('The Amplitude');
title('Sin Wave and Cos Wave');
legend('Sin','Cos','Location','Best');
axis([0,4*pi,-2,2]);
grid on;
text(3/4*pi,sin(3/4*pi),'\leftarrow Sin');
text(9/4*pi,cos(9/4*pi),'\leftarrow Cos');
Basic Control Statements-for
for, end
for variable=expression
statement;
end
expression: initial value: increment: end value
for i=1:5
for j=1:4
A(i,j)=1/(i+j-1);
end
end;
Exercise in Class 6
Write a for program to compute
0.5+1+1.5+2+…+50
x=0;
for i=0.5:0.5:50
x=x+i;
end
Basic Control Statements-while
while, end
while expression
statement;
end
expression: a logical statement which is connected by
==,<,<=,>,>=,~=.
x=1;
while x<5
x=x+1;
end
Exercise in Class 7
Write a while program to compute
0.5+1+1.5+2+…+50
i=0.5;x=0;
while i<=50
x=x+i;
i=i+0.5;
end
Basic Control Statements-if
if, elseif, end
IF expression
statements
ELSEIF expression
statements
ELSE
statements
END
M Files
• Put a bunch of commands in an .m file.
• Run the file under the command window.
• The using of ‘;’.
• Write a function as an .m file.
- file name is the function name
- the first line defines the function
function myresutl=myfunction(a, b);
- the comment lines (start with %) follow the first line
is shown when asking for help