Tải bản đầy đủ (.pdf) (61 trang)

Matlab training course_ basic

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.64 MB, 61 trang )

Examples and excises codes are free for education goals, contact me via
facebook: roland.nam.5

MATLAB FUNDAMENTALS AND
PROGRAMMING TECHNIQUES
(BASIC)

Claire Chuang



Course Outline


Working with the MATLAB User Interface



Variables and Commands



Analysis and Visualization with Vectors and
Matrices



Automating Commands with Scripts




Appendix: Data Types


How to Use This Manual


Code font is used for code, function names and URLs. It also
occurs on the slides in one of the lower corners as reference to
relevant files or commands for the example.
>> command_line_code
>>[a,b,c] = command_in_file(d,f);



At times, code may run off the line. The line continuation
character (…) is used to show this. These are valid MATLAB
statements if typed as shown, including carriage returns.
>> [CFlowAmounts, CFlowDates, TFactors] = ...
cfamounts(couponRate, settle, maturity);



Menu items, options and key names are highlighted in bold in
the notes sections.
Use Ctrl+C to break out of execution.
Click on File, Set Path ... to open the Path Browser.

>> try this at the prompt



Course Outline


Working with the MATLAB User Interface



Variables and Commands



Analysis and Visualization with Vectors and
Matrices



Automating Commands with Scripts



Appendix: Data Types


The MATLAB® Desktop

Current folder

Command Window
Commands entered & results returned


Current Folder
Files in the current folder

Workspace
Variables(data) in memory


Customizing the Desktop

Window
actions
Window
actions

Resize &
reposition


Variables in the Base Workspace

19

1990
1991
1992
1993
1994
1995
1996
1997

1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008

NaN
1.96
1.89
1.73
1.84
1.95
2.12
2.05
1.63
1.72
1.94
1.71
1.76
2.19
2.72
3.23
3.54
3.85

4.45

1.87
1.92
1.73
1.57
1.45
1.53
1.61
1.62
1.38
1.52
1.86
1.72
1.69
1.99
2.37
2.89
3.26
3.59
4.08

3.63
3.45
3.56
3.41
3.59
4.26
4.41
4

3.87
3.85
3.8
3.51
3.62
4.35
4.99
5.46
5.88
6.6
7.51

2.65
2.9
3.27
3.07
3.52
3.96
3.94
3.53
3.34
3.42
3.45
3.4
3.67
4.59
5.24
5.66
6.03
6.88

7.75

4.59
4.5
4.53
3.68
3.7
4
4.39
4.07
3.84
3.87
3.77
3.57
3.74
4.53
5.29
5.74
6.1
6.73
7.63

11

3.16
3.46
3.58
4.16
4.36
4.43

3.64
3.26
2.82
3.27
3.65
3.27
3.15
3.47
3.93
4.28
4.47
4.49
5.74

1
1.3
1.5
1.56
1.48
1.11
1.25
1.47
1.49
1.79
2.01
2.2
2.24
2.04
2.03
2.22

2.31
2.4
2.45

2.05
2.49
2.65
2.88
2.87
2.94
3.18
3.34
3.04
3.8
4.18
3.76
3.84
4.11
4.51
5.28
5.92
6.21
5.83

2.82
3.01
3.06
2.84
2.99
3.21

3.34
3.83
4.06
4.29
4.58
4.13
4.16
4.7
5.56
5.97
6.36
7.13
7.42

1.16
1.14
1.13
1.11
1.11
1.15
1.23
1.23
1.06
1.17
1.51
1.46
1.36
1.59
1.88
2.3

2.59
2.8
3.27

numeric data → “double precision”


The Variable Editor


Navigating the Help Browser


Help and Documentation
search
browse


Chapter 1 Test Your Knowledge
1. Where does MATLAB display a listing of the variables
currently in memory and their associated attributes?
A. Command Window
B. Workspace browser
C. Current Directory browser
D. Command History

2. T/F: The MATLAB desktop is customizable.

3. The default MATLAB variable type for numeric data is:
A. Single

B. Double

C. Cell


Course Outline


Working with the MATLAB User Interface



Variables and Commands



Analysis and Visualization with Vectors and
Matrices



Automating Commands with Scripts



Appendix: Data Types


MATLAB® Commands



Assigning Values to Variables

Data Type

>> x = 6*7;
Assignment

1) Evaluate right-hand side
2) Assign to variable
on left-hand side

x
1-by-1

42


Chapter 2 Test Your Knowledge
1. (Select all that apply) Which of the following will create a matrix with
three rows?
A.
B.
C.
D.

A = [zeros(2,4);ones(1,4)];
A = [1;2;3,4;5;6];
A = [1,2;3,4;5,6]';
A = rand(3);


2. Given a 5-by-5 matrix A, A(4:end,3:4) will produce a matrix of what size?
A.
B.
C.
D.
3.

1-by-2
2-by-2
2-by-3
3-by-2

(Select all that apply) Which commands are equivalent to the command
>>x = 1.4:2:6.8;?
A. X = [1.4 2 6.8];
B. X = [1.4 6.8];
C. X = [1.4 3.4 5.4];
D. X = [1.4 3.4 5.4 6.8];
E. X = [3.4 5.4];


Course Outline


Working with the MATLAB User Interface



Variables and Commands




Analysis and Visualization with Vectors and
Matrices



Automating Commands with Scripts



Appendix: Data Types


Array Operations
>> GMSum = Germany + Mexico
3.65
4.20
4.77
4.63
5.00
5.07
5.19
5.00
4.83
5.21
5.46
5.60
5.91

6.63
7.27
7.88
8.34
9.28
10.20
>> load ./gasprices/gasprices.mat

2.65
2.90
3.27
3.07
3.52
3.96
3.94
3.53
3.34
3.42
3.45
3.40
3.67
4.59
5.24
5.66
6.03
6.88
7.75

1.00
1.30

1.50
1.56
1.48
1.11
1.25
1.47
1.49
1.79
2.01
2.20
2.24
2.04
2.03
2.22
2.31
2.40
2.45


Matrix Operations
Inner dimensions must be equal!!

WAvgPrices

Prices
1.96
1.96
1.89
1.73
1.84

1.95
2.12
2.05
1.63
1.72
1.94
1.71
1.76
2.19
2.72
3.23
3.54
3.85
4.45

1.87
1.92
1.73
1.57
1.45
1.53
1.61
1.62
1.38
1.52
1.86
1.72
1.69
1.99
2.37

2.89
3.26
3.59
4.08

3.63
3.45
3.56
3.41
3.59
4.26
4.41
4.00
3.87
3.85
3.80
3.51
3.62
4.35
4.99
5.46
5.88
6.60
7.51

2.65
2.90
3.27
3.07
3.52

3.96
3.94
3.53
3.34
3.42
3.45
3.40
3.67
4.59
5.24
5.66
6.03
6.88
7.75

4.59
4.50
4.53
3.68
3.70
4.00
4.39
4.07
3.84
3.87
3.77
3.57
3.74
4.53
5.29

5.74
6.10
6.73
7.63

3.16
3.46
3.58
4.16
4.36
4.43
3.64
3.26
2.82
3.27
3.65
3.27
3.15
3.47
3.93
4.28
4.47
4.49
5.74

1.00
1.30
1.50
1.56
1.48

1.11
1.25
1.47
1.49
1.79
2.01
2.20
2.24
2.04
2.03
2.22
2.31
2.40
2.45

19-by-10

2.05
2.49
2.65
2.88
2.87
2.94
3.18
3.34
3.04
3.80
4.18
3.76
3.84

4.11
4.51
5.28
5.92
6.21
5.83

2.82
3.01
3.06
2.84
2.99
3.21
3.34
3.83
4.06
4.29
4.58
4.13
4.16
4.70
5.56
5.97
6.36
7.13
7.42

1.16
1.14
1.13

1.11
1.11
1.15
1.23
1.23
1.06
1.17
1.51
1.46
1.36
1.59
1.88
2.30
2.59
2.80
3.27

rpop/100
0.0210
0.0370
0.0701
0.0900
0.0660
0.1390
0.1180
0.0530
0.0670
0.3360




2.1547
2.2662
2.3495
2.3339
2.4143
2.5245
2.5206
2.4468
2.2712
2.4766
2.7190
2.5711
2.5750
2.9306
3.3489
3.7602
4.0618
4.4006
4.9705

10-by-1

2.72*0.0210 + 2.37*0.0370 + 4.99*0.0701 + 5.24*0.0900 + ... + 5.56*0.0670 + 1.88*0.3360 = 3.3489

19-by-1


Array Operations
Matrices must have the same dimensions!!


Prices

19-by-10

.*

cx

19-by-10

19-by-10


Data in the MATLAB® Environment

Observations
or cases

Variables

1990
1991
1992
1993
1994
1995
1996
1997


Australia Canada
1.96
1.87
1.96
1.92
1.89
1.73
1.73
1.57
1.84
1.45
1.95
1.53
2.12
1.61
2.05
1.62

France Germany ...
3.63
2.65
3.45
2.90
3.56
3.27
3.41
3.07
3.59
3.52
4.26

3.96
4.41
3.94
4.00
3.53

Independent columns

mean


Statistical Operations
1.96
1.96
1.89
1.73
1.84
1.95
2.12
2.05

1.87
1.92
1.73
1.57
1.45
1.53
1.61
1.62


3.63
3.45
3.56
3.41
3.59
4.26
4.41
4.00

2.65
2.90
3.27
3.07
3.52
3.96
3.94
3.53

4.4079

4.2247

mean

2.3284

2.0868

max
min

mean
median
std
sum
prod
diff
gradient
cumsum
cumprod
corrcoef
cov


Example: Array Operations


In most languages - use loops:
>> tic; for I = 1:10000

Density(I) = Mass(I)/(Length(I)*Width(I)*Height(I));
end; toc
elapsed_time =
4.7260



Use TIC and TOC to
measure elapsed time

In MATLAB - use array operations:

>> tic; Density = Mass./(Length.*Width.*Height); toc
elapsed_time =

0

>> array_examp

Vectorized code is
much faster than loops


Chapter 3-1 Test Your Knowledge
1. (Select all that apply) Given two matrices, A and B, where A
is a 2-by-3 matrix and B is a 3-by-2 matrix, which of the
following operations are valid?
A.
B.
C.
D.

A+B
A.+B
A*B
A.*B

2. Given a vector x, what is the command to add 3 to each
element, double that value, then sum all the resulting values?
A.
B.
C.


sum(2*x+3)
sum[2*x+3]
sum(2*(x+3))


Plotting the Data

Automatic code


Property Inspector

>>propedit


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×