1. Trang chủ
  2. » Giáo án - Bài giảng

Giáo trình MATLAB 54m

252 323 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 252
Dung lượng 3,58 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

x = 3 : assign the value 3 to the variable x x = x + 2 : add 2 to the current value of x  Workspace : the names and values of any variables in use in the current work session sessio

Trang 1

BEGINNING

Trang 3

CHAPTER 1

An Overview of MATLAB

Trang 4

References

1 Co So Matlab va Ung dung,

Author: Pham Thi Ngoi Yen

Publisher: Nha xuat ban Khoa hoc va Ky thuat, Ha Noi

Year: 2006

2 The MATLAB Help System

3 Essential MATLAB® for Engineers and Scientists

Author: Brian D Hahn and Daniel T Valentine

Publisher: Elsivier

Year: 2007

Trang 5

The default MATLAB Desktop

Trang 6

Operations (1/2)

Trang 7

Operations (2/2)

Calculation result is saved

6 is saved to ans by using ans one more time

1.5 is saved to the variable ‘a’ by

assign ‘a’ as the storing place

In the Matlab, ‘\’ = ‘\’

Trang 8

assignment , workspace, and command

The sign ‘=‘ is called the

assignment or replacement’

operator

x = 3 : assign the value 3 to the variable x

x = x + 2 : add 2 to the current value of x

Workspace : the names and values of

any variables in use in the

current work session

session

clc clears the Command window

memory

exist(‘name’) Determines if a file or variable

exists having the name ’name’

who Lists the variable currently in

memory

sizes, and indicates if they have imaginary parts

: Colon; generates and array

having regularly spaced elements

, Comma; separates elements of

an array

; Semicolon; suppresses screen

printing; also denotes a new row

in an array

… Ellipsis; continues a line

Trang 9

recall feature, and special variable

recall feature

up-arrow key( ↑) & down-arrow key(↓): move up and down through the

previously typed lines one line at a time

Tab key: automatically completes the name of a function, variable, or file if you

type the first few letters of the name and press the Tab key

i , j The imaginary unit

Trang 10

Complex Number Operations

‘;’ is a command which

leads to a new line, also omits calculation results

Trang 11

Formatting Commands

The Format command controls how numbers appear on the screen

format short Four decimal digits (the default); 13.6745

format long 16 digits; 17.27484029463547

format short e Five digits (four decimals) plus exponent; 6.3793e+03

format long e 16 digits (15 decimals) plus exponent; 6.379243784781294e-04

format bank Two decimal digits; 126.73

format + Positive, negative, or zero; +

format rat Rational approximation; 43/7

format compact Suppresses some line feeds

format loose Resets to less compact display mode

Trang 12

Array(1/2)

One of the strengths of MATLAB is its ability to handle collections of numbers, called arrays

A numerical array is an ordered collection of numbers

We can use square brackets

Trang 13

Array(2/2)

You can compute ‘w=5*sin (u)’ for ‘u=[0: 0.1: 10]’

>> u=[0: 0.1: 10];

>> w=5*sin(u);

 computed the formula ‘w=5*sin(u)’ 101 times

array index: points to a particular element in the array

Trang 14

◈The MATLAB trigonometric functions

use radian measure

x

e x x

ln

x

log1 0 x

cos x

sin x

Trang 15

Working with Files

M-file : MATLAB function files and program files are saved with the

extension m, and called M-files

MAT-file : save the names and values of variables

ASCII-file : files written in a specific format designed to make them usable to a wide variety of software

Trang 16

System, directory, and file commands

addpath dirname Adds the directory dirname to the search path

cd dirname Changes the current directory to dirname

dir dirname Lists all the files in the directory dirname

rmpath dirname Removes the directory dirname from the search path

what Lists the MATLAB-specific files found in the current working directory Most data

files and other non-MATLAB files are not listed Use dir to get a list of all files

what dirname Lists the MATLAB-specific files in directory dirname.

Trang 17

Plotting commands

[x , y] = ginput(n) Enables the mouse to get n points from a plot, and returns the x and

y coordinates in the vectors x and y, which have a length n

grid puts grid lines on the plot

gtext (‘text’) Enables placement of text with the mouse

plot (x , y) Generates a plot of the array y versus the array x on rectilinear axes

title (‘text’) Puts text in a title at the top of the plot

xlabel (‘text’) Adds a text label to the horizontal axis (the abscissa)

ylabel (‘text’) Adds a text label to the vertical axis (the ordinate)

Trang 18

Plotting with MATLAB

Variable pairs must be written (x,y) and (x,z) are pairs,

so express ‘plot(x,y,x,z)’

Trang 19

Linear Algebra Equations

the left division operator(\)

 e.g

64 9z -

8y

2x

5 3z 2y

- 7x

70 4z

12y 6x

Trang 20

Statistics, Calculus, and Processing

Statistics

 perform statistical calculations and other types of data

manipulation

Numerical Calculus, Differential Equations, and Simulink

 MATLAB can numerically compute the derivative and

the integral

Symbolic Processing

 obtain the derivative and the integral in

symbolic form (a formula instead of as a set of numerical

values)

Trang 21

Script Files and the Editor/Debugger(1/2)

: directly enter the commands in the Command window

: store the commands in script files

Trang 22

Script Files and the Editor/Debugger(2/2)

Trang 23

Input/output commands

disp (A) Displays the contents, but not the name, of the array A

disp (‘text’) Displays the text string enclosed within single quotes

format Controls the screen’s output display format

fprintf Performs formatted writes to the screen or to a file

x = input (‘text’) Displays the text in quotes, waits for user input from the

keyboard, and stores the value in x

x = input (‘text’ , ’s’) Displays the text in quotes, waits for user input from the

keyboard, and stores the input as a string in x

k = menu

(‘title’,’option1’,’option2’,…)

Displays a menu whose title is in the string variable ‘title’, and whose choices are ‘option1’,’option2’, and so on

Trang 24

The MATLAB Help System(1/4)

Help Browser

 Graphical user interface

 find information, view online documentation

Help Functions

 help, lookfor, doc

 display syntax information for specified function

Other Resources

 run demos, contact technical support, participate in a newsgroup

 The MathWorks Website

 the home of MATLAB

 http://www.mathworks.com

Trang 25

The MATLAB Help System(2/4)

- Help Browser

The MATLAB Help Browser

Contents: a contents listing tab

Index: a global index tab

Search: a search tab having a find function and full text search features

Demos: a bookmaking tab to start built-in

demonstrations

Trang 26

The MATLAB Help System(3/4)

doc toolbox/function Displays the documentation for the specified toolbox function

doc toolbox Displays the documentation road map page for the specified toolbox help Displays a list all the function directories, with a description of the

function category each represents

help function Displays in the Command window a description of the specified

function function

helpwin topic Displays the help text for the specified topic inside the desktop Help

Browser window

lookfor topic Displays in the Command window a brief description for all functions

whose description includes the specified keyword topic

Trang 27

- Help Functions

Examples

Trang 28

28

CHAPTER 2

Numeric, Cell, and Structure Arrays

Trang 29

29

Arrays

 the basic building block in MATLAB

 addition, subtraction, multiplication, division, and exponentiation

 polynomial algebra and root

Available classes of arrays in MATLAB 7

numeric arrays : contains only numeric values

cell arrays : access data by its location

structure arrays : access data by name

Array

Trang 30

in MATLAB: write in a specific order,

separate with a space,

identify the group with brackets

 [5 7 2]

row vector [5 7 2]

column vector

Trang 31

31

Arrays

Creating Vectors in MATLAB

 MATLAB displays row vectors horizontally and column vectors vertically

 Create a row vector : space or commas separate elements

Trang 32

32

Arrays

Creating Vectors in MATLAB

 Generate a large vector of regularly spaced elements using colon operator (:)

 x = [m:q:n]

 m : the first value

 q : the increment (default = 1)

 n : the last value <= n

 x = [0:2:8] = [0, 2, 4, 6, 8] x = [0:2:7] = [0, 2, 4, 6]

 y = [-3:2] = [-3, -2, -1, 0, 1, 2]

 u = [10:-2:4] = [10, 8, 6, 4]

 The linspace command, logspace command

 linspace(x1, x2, n) : n is number of points between x1 and x2

 linspace(5,8,31) = [5:0.1:8]

 logspace(a, b, n) : n is number of points between 10a and 10b

 logspace(-1,1,4) = [0.1000, 0.4642, 2.1544, 10.000]

Trang 33

Space or commas separate elements in different columns

Semicolons separate elements in different rows

You can also create a matrix from row or column vectors

10 4

7

5 3 1

 1 3 5 7 9 11 

Trang 34

34

Arrays

Array Addressing

The row number is always listed first

The colon operator selects individual elements, rows, columns,

or ”subarrays” of arrays

v( : ) represents all the row or column elements of the vector v

v(2 : 5) represents the second through fifth elements

A( : , 3) denotes all the elements in the third column of the matrix A

8

7 3

12 3

25 9

4 8

18 7

3 16

13 10

4 2

B

The general indexes of two dimensional array

Trang 35

A(3, :) = [ ] deletes the third row in A

A(:, 2:4) = [ ] deletes the second through fourth columns In A

A([1 4], : ) = [ ] deletes the first and fourth rows of A

1

4 9

7 5

1

3 0

4 9

7 0

0

6 9

4 0

3

B

Trang 36

36

Arrays

Command Description

cat(n, A, B, C, …) Creates a new array by concatenating the arrays A , B , C , and so on along the dimension n

find(x) Computes an array containing the indices of the nonzero elements of the aray x

[u, v, w] = find(A) Computes the arrays u and v, containing the row and column indices of the nonzero elements of the

matrix A, and the array w, containing the values of the nonzero elements The array w may be omitted

length(A) Computes either the number of elements of A if A is a vector or the largest value of m or n if A is an m

× n matrix

linspace(a,b,n) Creates a row vector of n regularly spaced values between a and b

logspace(a,b,n) Creates a row vector of n logarithmically spaced values between a and b

max(A) Returns the algebraically largest element in A if A is a vector Returns a row vector containing the

largest elements in each column if A is a matrix If any of the elements are complex, max(A) returns the elements that have the largest magnitudes

[x, k] = max(A) Similar to max(A) but stores the maximum values in the row vector x and their indices in the row vector

k

min(A) Same as max(A) but returns minimum values

[x, k] = min(A) Same as [x, k] =max(A) but returns minimum values

size(A) Returns a row vector [m n] containing the sizes of the m × n array A

sort(A) Sorts each column of the array A in ascending order and returns an array the same size as A

sum(A) Sums the elements in each column of the array A and returns a row vector containing the sums

Trang 37

37

Arrays

absolute values of the elements of x

<example>

x = [2, -4, 5]

length = 3, magnitude = 6.7082, absolute value = [2, 4, 5]

want to open

2 2

2 2

Trang 38

38

Multidimensional Arrays

MATLAB supports multidimensional arrays

The first two dimensions are the row and column, as with a matrix

The higher dimensions are called pages

<example>

Want to create a three dimensional array whose first page is

and whose second page is

0 8 5

1 6 4

1 3 0

9 2 6

Trang 40

2 ^ [3, 5] = [2^3, 2^5]

[3, 5] ^ [2, 4] = [3^2, 5^4]

Trang 41

>>y=sin(x.^2);

>>plot(x,y);

Trang 42

eye(n) Creates an n×n identity matrix

eye(size(A)) Creates an identity matrix the same size as the matrix A

ones(n) Creates an n×n matrix of ones

ones(m,n) Creates an m×n array of ones

ones(size(A)) Creates an array of ones the same size as the array A

zeros(n) Creates an n×n matrix of zeros

zeros(m,n) Creates an m×n array of zeros

zeros(size(A)) Creates an array of zeros the same size as the array A

Trang 43

43

Matrix Operations

Matrix Division

 Right and left operators, / and \

 Chapter 6 covers matrix division and matrix inverse

Matrix Exponentiation

 must be a square matrix

 to find A2, type A^2

Special products

cross(A, B) Computes a 3×n array whose columns are the cross

products of the corresponding columns in the 3×n arrays A and B Returns a three-element cross-

product vector if A and B are three-element vectors

dot(A, B) Computes a row vector of length n whose elements

are the dot products of the corresponding columns

of the m×n arrays A and B

Trang 44

44

Polynomial Operations Using Arrays

Type help polyfun for more information

2 3

1 2 1

)

(  nn  n   n  nn

a x a x a x

a x

a x a x

7 3 5 9 ) (xx3 x2 x

f g(x)  6x2x 2 h(x) f(x)g(x)

9 2 9

) (xx3x2 x

h

Trang 45

45

r

poly(r) Computes the coefficients of the polynimial whose roots are specfied by the vector r

polyval(a, x) Evaluates a polynomial at specified values of its independent variable x, which can be a matrix

or a vector The polynomial’s coefficients of descending powers are stored in the array a The result is the same size as x

roots(a) Computes the roots of a polynomial specified by the coefficient array a The result is a column

vector that contains the polynomial’s roots

14 29

41 39

54

) 2 6

)(

7 3 5 9 ( ) ( ) (

2 3

4 5

2 2

x x

x x x

x x x

g x f

5833 0 5 1 2

6

7 3 5 9 ) (

) (

2

2 3

x x x x

g

x f

>>f = [9, -5, 3, 7];

>>g = [6, -1, 2];

>>product = conv(f, g) product =

54 -39 41 29 -1 14

>>quotient, remainder] = deconv(f,g) quotient =

1.5 -0.5833 remainder =

0 0 -0.5833 8.1667

Trang 46

9 )

-200 0 200 400 600 800 1000 1200

x

Trang 47

9 6

386

842

J

Trang 48

48

Cell Arrays

Function Description

C = cell(n) Creates an n×n cell array C of empty matrices

C = cell(n,m) Creates an n×m cell array C of empty matrices

celldisp(C) Display the contents of cell array C

cellplot(C) Displays a graphical representation of the cell array C

c = num2cell(A) Converts a numeric array A into a cell array C

[X, Y, …] = deal(A,B, …) Matches up the input and output lists Equivalent to X = A, Y = B, …

[X, Y, …] = deal(A) Matches up the input and output lists Equivalent to X = A, Y = A, …

iscell(C) Returns a 1 if C is a cell array; otherwise, returns a 0

Trang 49

49

Cell Arrays

Trang 50

50

Structure Arrays

Structure arrays : composed of structures

Creating Structures

using assignment statements

using the struct function

dot notation (.) : to specify and to access the fields

<example>

student.name = ‘John Smith’;

student.SSN = ‘392-77-1786’;

student.email = ‘smithj@myschool.edu’; student.tests = [67, 75, 84];

student(2).name = ‘Mary Jones’;

student(2).SSN = ‘431-56-9832’;

student(2).email = ‘jonsm@myschool.edu’; student(2).tests = [84, 78, 93];

Structure array “student”

Trang 51

51

Structure Arrays

Function Description

names = fieldnames(S) Returns the field names associated with the structure array S as

names , a cell array of strings

F = getfield(S, ‘field’) Returns the contents of the field ‘ field ’ in the structure array S

Equivalent to F = S.field isfield(S, ‘field’) Returns 1 if ‘field’ is the name of a field in the structure array S , and

0 otherwise

isstruct(S) Returns 1 if the array S is a structure array, and 0 otherwise

S = rmfield(S, ‘field’) Removes the field ‘ field ’ from the structure array S

S = setfield(S, ‘field’, V) Sets the contents of the field ‘ field ’ to the value V in the structure

array S

S = struct(‘f1’, ‘v1’, ‘f2’, ‘v2’, …) Creates a structure array with the fields ‘f1’, ‘f2, … having the

values ‘v1’, ‘v2’, …

Ngày đăng: 17/09/2016, 10:24

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w