Você está na página 1de 21

1.

Introduction to Matlab
Matlab: Matrix Laboratory Numerical Computations with matrices Every number can be represented as matrix

Why Matlab? User Friendly (GUI) Easy to work with Powerful tools for complex mathematics.
C.B. Pham 1

Matlab desktop
When you start MATLAB, MATLAB desktop appears, containing tools (graphical user interfaces) for managing files, variables, and applications associated with MATLAB.

Command window

C.B. Pham

Help in Matlab
>> help <function_name> Shows help document for a give function Eg. help sqrt

>> lookfor <keyword> Searches all the help documents for a given keyword Eg. lookfor sqrt

>> demo
C.B. Pham 3

2. Matrix operations
Addition >> c = a + b Multiplication >> c = a*b >> c = a.*b Substraction >> c = a - b

% matrix multiplication % element wise multiplication

Division (left, right) >> c = a/b >> c = a./b >> c = a\b >> c = a.\b
C.B. Pham

% a * b-1 % element wise division % a-1 * b % element wise division


4

Variable: constant, vector, matrix


Real >> x = 5 x= 5 Row vector >> x = [1, 2, 3] x= 1 2 3 % [1 2 3] Complex >> x = 5 + 10*i x= 5.0000 + 10.0000i Column vector >> x = [1; 2; 3] x= 1 2 3

Matrix >> x = [1 2 3; 4 5 6] x= 1 2 3 Note: Variable Names are case sensitive 4 5 6 C.B. Pham

Vectors
>> [0 : 0.2 : 1] % 0 to 1 in increments of 0.2 ans = 0.0 0.2 0.4 0.6 0.8 1.0 >> linspace(0, 1, 6) % 6 points from 0 to 1 on a linear scl ans = 0.0 0.2 0.4 0.6 0.8 1.0 >> logspace(0, 1, 6) % 6 points from 100 to 101 on a log scl ans = 1.0000 1.5849 2.5119 3.9811 6.3096 10.0000
C.B. Pham 6

Matrix
>> ones(2, 3) ans = 1 1 1 1 1 1 >> zeros(2, 3) ans = 0 0 0 0 0 0 % generates an all one 2 x 3 matrix

% generates an all zero 2 x 3 matrix

>> [1 2 3; 4 5 6] ans = 1 2 3 4 5 6
7

C.B. Pham

Matrix
>> eye(2) ans = 1 0 0 1 % generates the 2 x 2 identity matrix

>> rand(2, 3) % generates a random 2 x 3 matrix ans = 0.9501 0.6068 0.8913 >> [1 2 3; 4 5 6] 0.2311 0.4860 0.7621 ans = 1 2 3 4 5 6
C.B. Pham 8

Accessing matrix elements


>> M = [ 1 2 M= 1 2 3 5 6 7 9 10 11 3 4; 5 6 7 8; 9 10 11 12] 4 8 12

>> x = M(2, 3) % element at row 2 & column 3 of M x = >> y = M(2, :) % select the 2nd row M 7 y= >> z = M(2:3, 2:3) % select sub-matrix of M 5 6= 8 z 7 6 7 10 11
C.B. Pham 9

Concatenating, Appending,
>> A = [ 1 2 3 ] ; >> B = [ 4 5 6 ] ; >> R = [ A B ] >> S = [ A; B ] R= 1 2 3 4 5 6 S= 1 2 3 4 5 6

>> S(3, 3) = 7 S= 1 2 3 4 5 6 0 0 7

Note: if you store a value in an element outside of the matrix, the size increases to accommodate the newcomer.
C.B. Pham 10

Complex number operations


>> abs(x) ans = 5 % Absolute value >> angle(x) ans = 0.9273

>> x = 3 + 4*i;

% Phase angle (in radians)

>> conj(x) % Complex conjugate >> imag(x) % Complex imaginary part ans = ans 3.0000 4.0000i = 4 >> real(x) ans = C.B. Pham 3 % Complex real part
11

Some Useful Functions


Some useful math functions: sin(x), cos(x), tan(x), atan(x), exp(x), log(x), log10(x), sqrt(x), >> t = [0 : 0.1 : 10] ; >> x = sin(2 * pi * t) ;

Some useful matrix and vector functions:

>> length(x) ans = 101


C.B. Pham

>> size(x) ans = 1 101


12

More Operators and Functions


>> sum(M) ans = 6 8 >> sum(ans) ans = 14 >> M ans = 1 2 % equivalent to sum(sum(M))

>> M = [ 1 2; 5 6 ] M= 1 2 5 6

% equivalent to transpose(M) 5 6 % diagonal elements

>> diag(M) ans = 1 6


C.B. Pham

13

More Operators and Functions


Find the roots of the polynomial >> f = [ 1 2 3 4 ] ; >> roots(f) ans = -1.6506 -0.1747 + 1.5469i -0.1747 - 1.5469i Partial Fraction Expansion >> [R, P, K] = residue([ 5 3 ] , [ 1 3 0 -4 ]) R= P= K= -0.8889 -2.0000 [] 2.3333 -2.0000 0.8889 1.0000
C.B. Pham

% create f = x3 + 2x2 + 3x + 4

5x 3 x 3 3x 2 4

9 3 9 x 2 ( x 2) 2 x 1
14

3. Plots in Matlab

C.B. Pham

15

Continuous time plots - plot


The plot function has different forms, depending on the input arguments. >> x = [ 0 : 0.1 : 2*pi ] ; >> y = sin(x) ; >> plot(y) >> plot(x, y)

C.B. Pham

16

Discrete time plots - stem


Stem function is very similar to plot. It is used to plot discrete time sequences.

>> x = [ 0 : 0.5 : 10 ] ; >> y = x.^2 ; >> stem(x, y) >> grid on >> xlabel(Time, t) >> ylabel(Speed, s) >> title(Graph s = t^2)

C.B. Pham

17

Multiple Data Sets in One Graph


>> t = [ 0 : 0.001 : 2*pi ] ; >> x = sin(t); >> y = sin(t - 0.5); >> z = sin(t - 1); Approach 1 >> plot(t, x, t, y, t, z) Approach 2 >> plot(t, x, t, y, t, z) >> plot(t, x, b) >> hold on >> plot(t, y, g) >> plot(t, z, r) >> hold off
C.B. Pham

>> grid on >> legend(sin(t), sin(t 0.5), sin(t - 1))


18

Plot with two different y-axes


>> t = [ 0 : 0.001 : 2*pi ] ; >> p = sin(2*t); >> v = 2*cos(2*t);

>> plot(t, p) >> plot(t, v)

>> plotyy(t, p, t, v)

C.B. Pham

19

4. Function in Matlab
Syntax function [out1, out2, ] = function_name (in1, in2, ) .. .. % commands .. Description: function [out1, out2, ...] = function_name (in1, in2, ...) defines function function_name that accepts inputs in1, in2, etc. and returns outputs out1, out2, etc. Note: When you create a function in a Matlab .m file and you want to call it from the workspace, make sure the name of the .m file is the same as the name of the function itself (good programming practice).
C.B. Pham 20

Example: make function of area


File saved as dien_tich.m function a = dien_tich(l1, l2, d) if (d == 1) a = l1 * l2 ; elseif (d == 2) a = l1 * l2 / 2 ; else a = -1 ; end % rectangle % triangle % others >> dien_tich(2, 3, 1) ans = 6 >> dien_tich(2, 3, 2) ans = 3 >> dien_tich(2, 3, 3) ans = -1

A = a.b

h A = a.h / 2 a
21

C.B. Pham

Você também pode gostar