0% found this document useful (0 votes)
19 views60 pages

ML Basic

The document provides an overview of MATLAB fundamentals and programming techniques, covering topics such as the user interface, variables, data visualization, and scripting. It outlines the capabilities of MATLAB in various fields including finance, engineering, and education, and includes a course outline with specific topics to be covered. Additionally, it discusses data handling, plotting, and basic programming commands within MATLAB.

Uploaded by

stu00701
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views60 pages

ML Basic

The document provides an overview of MATLAB fundamentals and programming techniques, covering topics such as the user interface, variables, data visualization, and scripting. It outlines the capabilities of MATLAB in various fields including finance, engineering, and education, and includes a course outline with specific topics to be covered. Additionally, it discusses data handling, plotting, and basic programming commands within MATLAB.

Uploaded by

stu00701
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MATLAB FUNDAMENTALS AND

PROGRAMMING TECHNIQUES
(BASIC)

Pony Lai
support@[Link]
MathWorks™ Product Overview
Simulink Product Family Application-Specific Products

MATLAB Product Family


Diverse Users
Utilities & energy

Signal processing
& communications

Biotech, medical,
& pharmaceutical

Instrumentation
Finance & economics

Automotive
Government
Aerospace
& defense
Education
What Can You Do with MATLAB® ?
5
5 Data
8-term Model
4.5
4.5 3

Retail consumption [10 kWh/day]


10

Retail consumption [10 kWh/day]


4
4 2
10

9
9
3.5
3.5 1
10
3

Power
3 0
10
2.5
-1
2.5 10
2
-2
2 10
01/95 01/00 01/05
Date 1.5

Interactively -3
01/95 01/00 01/05
10 Date
0 1
10 10
Period [years]

import, analyze,
and export data Perform calculations and
analysis on data sets

Create informative data visualizations Write programs


Mexico
to automate
complex tasks
2.5
0.1
O3 [ppm]

0.09
2

Mexico prices
0.08 Mexico
data?
1.5
0.07
36
Yes
34 -94
32 -96 1
1990 1992 1994 1996 1998 2000 2002 2004 2006 2008
-98 Year
30 -100
-102
Lat. Lon. No
??? Error using ==> gpif at 21
No data for Mexico
Technical Computing Workflow
Access Explore & Discover Share
Files Reporting and
Data Analysis Documentation
& Modeling

Algorithm
Software Outputs for Design
Development

Code & Applications


Application
Development
Hardware Deployment

Automate
Course Outline

● Working with the MATLAB User Interface

● Variables and Expressions

● 2-D Plot Introduction

● Automating Commands with Scripts


Course Outline

● Working with the MATLAB User Interface

● Variables and Expressions

● 2-D Plot Introduction

● Automating Commands with Scripts


Outline

• Reading data from file

• Saving and loading variables

• Plotting data

• Customizing plots

• Calculating statistics and best-fit line

• Exporting graphics for use


in other applications
The MATLAB® Desktop
Desktop controls

Current folder
Variables
(data) in
Commands entered memory
& results returned
Files in the
current folder Previously
entered
commands
Customizing the Desktop

Window
actions Resize &
reposition
Course Example: Gas Price Data
C:\class\coursefiles\mlbe\gasprices
Interactive Importing
Variables in the Base Workspace

1990 NaN 1.87 3.63 2.65 4.59 3.16 1 2.05 2.82 1.16
1991 1.96 1.92 3.45 2.9 4.5 3.46 1.3 2.49 3.01 1.14
1992 1.89 1.73 3.56 3.27 4.53 3.58 1.5 2.65 3.06 1.13
1993 1.73 1.57 3.41 3.07 3.68 4.16 1.56 2.88 2.84 1.11
1994 1.84 1.45 3.59 3.52 3.7 4.36 1.48 2.87 2.99 1.11
1995 1.95 1.53 4.26 3.96 4 4.43 1.11 2.94 3.21 1.15
1996 2.12 1.61 4.41 3.94 4.39 3.64 1.25 3.18 3.34 1.23
1997 2.05 1.62 4 3.53 4.07 3.26 1.47 3.34 3.83 1.23
1998 1.63 1.38 3.87 3.34 3.84 2.82 1.49 3.04 4.06 1.06

19 1999
2000
1.72
1.94
1.52
1.86
3.85
3.8
3.42
3.45
3.87
3.77
3.27
3.65
1.79
2.01
3.8
4.18
4.29
4.58
1.17
1.51
2001 1.71 1.72 3.51 3.4 3.57 3.27 2.2 3.76 4.13 1.46
2002 1.76 1.69 3.62 3.67 3.74 3.15 2.24 3.84 4.16 1.36
2003
2004
2.19
2.72
1.99
2.37
4.35
4.99
4.59
5.24
4.53
5.29
3.47
3.93
2.04
2.03
4.11
4.51
4.7
5.56
1.59
1.88
numeric data → “double precision”
2005 3.23 2.89 5.46 5.66 5.74 4.28 2.22 5.28 5.97 2.3
2006 3.54 3.26 5.88 6.03 6.1 4.47 2.31 5.92 6.36 2.59
2007 3.85 3.59 6.6 6.88 6.73 4.49 2.4 6.21 7.13 2.8
2008 4.45 4.08 7.51 7.75 7.63 5.74 2.45 5.83 7.42 3.27

11
The Variable Editor
New Variables
Saving and Loading Variables
Course Outline

● Working with the MATLAB User Interface

● Variables and Expressions

● 2-D Plot Introduction

● Automating Commands with Scripts


Outline

• Entering commands

• Creating variables & Data Type 1 2 3


4 5 6
• Getting help
7 8 9
• Accessing and modifying values in
variables

• Creating character variables


MATLAB® Commands
Saving and Loading MAT-Files
save

load
Assignment

>> no_of_penguins = x*sin(pi*t);

1. evaluate right-hand side

2. assign resulting value to


variable on left-hand side

create new variable or


overwrite old one,
as appropriate
MATLAB® Data Types
Array
[full or sparse] Scalar

user function
logical char numeric cell structure class handle

abc

int8, uint8, single double


int16, uint16,
int32, uint32,
int64, uint64
Boolean Operating and Indexing
Boolean Operators >> Mass = [-2 10 NaN 30 -11 Inf 31];
= = equal to >> each_pos = Mass>=0
each_pos =
> greater than
0 1 0 1 0 1 1
< less than >> all_pos = all(Mass>=0)
~= not all_pos =
0
& and
>> pos_fin = (Mass>=0)&(isfinite(Mass))
| or pos_fin =
isempty() 0 1 0 1 0 0 1
>> good_mass = Mass(pos_fin)
isfinite(), etc. . . .
good_mass =
any() 10 30 31
all()
1 = TRUE
0 = FALSE

>> bool_ops
Integer Arrays
How many colors of truck
could you possibly need?

uint8
double

256*256*256
= 16.8 million
Creating Vectors
Arbitrary data

>> x = [2,3,5,7,11,13];

Equally-spaced values
n subdivision points
x
a b
dx

>> x = a:dx:b;

>> x = linspace(a,b,n);
Creating Matrices
>> A = [1,2,3; 4,5,6; 7,8,9];
or
>> A = [1 2 3; 4 5 6; 7 8 9];
or

>> A = [1 2 3 1 2 3
data entry
4 5 6 mode 4 5 6
7 8 9]
7 8 9
Matrix Creation Functions compan
eye
gallery
hadamard
hankel
hilb
invhilb
magic
ones
pascal
rand
randi
randn
rosser
toeplitz
vander
wilkinson
zeros
Characters and Strings
>> y = x variable
>> y = 'x' character

>> MarkA = 'Friends, Romans, countrymen, lend me your ears';

1-by-46 char array

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

'F' 'r' 'i' 'e' 'n' 'd' 's' ',' ' ' 'R' 'o' 'm' 'a' 'n' 's' ',' ' ' 'c'

>> FriendNationality = MarkA(10:15)


Help and Documentation
search

browse

help
doc
docsearch
Row, Column Indexing
>> gasprices(1,2) >> gasprices(3,6) >> gasprices(2,end)

>> gasprices(end,2) >> gasprices(end,end)


Multiple Row, Column Indices
>> Year = gasprices(:,1) >> gasprices([3,4],6:9)

>> gp08 = gasprices(end,2:end)


Indexed Assignment

>> x = gasprices(1,2)
reference

>> x = 1.96;
>> gasprices(1,2) = x
assignment
Concatenation
>> C = [A,B]
>> A C =
A = 1 2 3 1 0 0
1 2 3
4 5 6 0 1 0
4 5 6
7 8 9 7 8 9 0 0 1

>> B >> D = [A;B] []


B =
D =
1 0 0
0 1 0 1 2 3
0 0 1 4 5 6
7 8 9
1 0 0
0 1 0
0 0 1
Course Outline

● Working with the MATLAB User Interface

● Variables and Expressions

● 2-D Plot Introduction

● Automating Commands with Scripts


基本的繪圖指令
⚫ 最基本的繪圖指令:plot
⚫ 對 x 座標及相對應的 y 座標進行描點作圖

x = linspace(0, 2*pi); % 在 0 到 2*pi 間,等分取 100 個點


y = sin(x); % 計算 x 的正弦函數值
plot(x, y); % 進行二維平面描點作圖
Plot基本繪圖-1

1
⚫ 說明
0.8
◆ linspace(0, 2*pi) 產生從
0.6
0 到 2*pi且長度為 100
0.4 (預設值)的向量 x
0.2
◆ y 是對應的 y 座標
0

-0.2 ⚫ 若只給定一個向量
-0.4
◆ 則plot會進行該向量則對
-0.6
其索引值(Index)作圖
➔plot(y)和
-0.8

-1
0 1 2 3 4 5 6 7 plot(1:length(y), y)會得
到相同的結果
Plot基本繪圖-2 (I)
⚫ 一次畫出多條曲線
◆ 將 x 及 y 座標依次送入plot 指令

x = linspace(0, 2*pi); % 在 0 到 2*pi 間,等分取 100 個點


plot(x, sin(x), x, cos(x), x, sin(x)+cos(x)); % 進行多條曲線描點作圖
Plot基本繪圖-2 (II)

Plot(x,sin(x), x, cos(x), x, sin(x)+cos(x));

1.5 ▪ 畫出多條曲線
時,會自動輪
1
換曲線顏色
0.5

-0.5

-1

-1.5
0 1 2 3 4 5 6 7
Plot基本繪圖-3 (I)
⚫ 可以使用不同的線標(Marker)來作圖

x = linspace(0, 2*pi); % 在 0 到 2*pi 間,等分取 100 個點


plot(x, sin(x), 'o', x, cos(x), 'x', x, sin(x)+cos(x), '*');
Plot基本繪圖-3 (II)

1.5

0.5

-0.5

-1

-1.5
0 1 2 3 4 5 6 7
Plot基本繪圖-4 (I)
⚫ plotyy 指令
◆ 畫出兩個刻度不同的 y 軸

x = linspace(0, 2*pi); % 在 0 到 2*pi 間,等分取 100 個點


y1 = sin(x);
y2 = exp(-x);
plotyy(x, y1, x, y2); % 畫出兩個刻度不同的 y 軸,分別是 y1, y2
Plot基本繪圖-4 (II)

1 1

0 0.5

y1的刻度 y2的刻度

-1 0
0 1 2 3 4 5 6 7

◆ y1 的刻度是在左手邊
◆ y2 的刻度是在右手邊
◆ 兩邊的刻度不同
圖形控制範例-(I)
◆ 用黑色點線畫出正弦波
◆ 每一資料點畫上一個小菱形

x = 0:0.5:4*pi; % x 向量的起始與結束元素為 0 及 4*pi,


% 0.5為各元素相差值
y = sin(x);
plot(x, y, ‘k:diamond’) % 其中「k」代表黑色,「:」代表點
% 線,而「diamond 」則指定菱形為曲
% 線的線標
圖形控制範例-(II)

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 2 4 6 8 10 12 14
Handle Graphics
◆ 事實上,我們可將畫出來的曲線看成一個物件,並進而改變
物件的性質。

x=0:0.5:4*pi;
h=plot(x, sin(x)); % Plot a sin curve
set(h, 'marker', 'o'); % Set marker to 'o'
set(h, 'markerSize', 15); % Set marker size to 15
set(h, 'lineWidth', 5); % Set line width to 5
set(h, 'lineStyle', ':'); % Set line style to dot
set(h, 'markerEdgeColor', 'g'); % Set marker edge color to green
set(h, 'markerFaceColor', 'y'); % Set marker face color to yellow
Handle Graphics

◆ 結果如下:

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 2 4 6 8 10 12 14
plot 指令的曲線顏色

Plot指令的曲線顏色字串 曲線顏色 RGB值


b 藍色(Blue) (0,0,1)
c 青藍色(Cyan) (0,1,1)
g 綠色(Green) (0,1,0)
k 黑色(Black) (0,0,0)
m 洋紅色(Magenta) (1,0,1)
r 紅色(Red) (1,0,0)
w 白色 (1,1,1)
y 黃色(Yellow) (1,1,0)
plot 指令的曲線格式

plot 指令的曲線樣式字串 曲線樣式


- 實線(預設值)
-- 虛線
: 點線
-. 點虛線
plot 指令的曲線線標 (I)

plot 指令的曲線線標字串 線標說明


O 圓形
+ 加號
X 叉號
* 星號
. 點號
^ 朝上三角形
V 朝下三角形
plot 指令的曲線線標 (II)

plot 指令的曲線線標字串 線標說明


> 朝右三角形
< 朝左三角形
square 方形
diamond 菱形
pentagram 五角星形
hexagram 六角星形
None 無符號(預設值)
圖軸控制範例(I)
⚫ 同時畫出四個圖於一個視窗中

x = 0:0.1:4*pi;
subplot(2, 2, 1); plot(x, sin(x)); % 此為左上角圖形
subplot(2, 2, 2); plot(x, cos(x)); % 此為右上角圖形
subplot(2, 2, 3); plot(x, sin(x).*exp(-x/5)); % 此為左下角圖形
subplot(2, 2, 4); plot(x, x.^2); % 此為右下角圖形
圖軸控制範例(II)

1 1

0.5 0.5

subplot(2,2,1) 0 0 subplot(2,2,2)
-0.5 -0.5

-1 -1
0 5 10 15 0 5 10 15

1 200

150
0.5

subplot(2,2,3) subplot(2,2,4)
100
0
50

-0.5 0
0 5 10 15 0 5 10 15
獨立顯示各軸的格線
⚫ 我們可以獨立顯示雙 ⚫ Result
軸的格線

subplot(2,2,1); plot(humps); 100 100

80 80

subplot(2,2,2); plot(humps); 60 60

40 40

set(gca, 'xgrid', 'on'); 20 20

subplot(2,2,3); plot(humps);
0 0
0 10 20 30 0 10 20 30

set(gca, 'ygrid', 'on'); 100 100

80 80

subplot(2,2,4); plot(humps); 60 60

40 40
grid on; 20 20

0 0
0 10 20 30 0 10 20 30
Course Outline

● Working with the MATLAB User Interface

● MATLAB Plot Tools introduction

● Variables and Expressions

● Automating Commands with Scripts


Case : Time and Frequency Data

.* =
Create a signal Create a mask A new signal

+ =
A new signal noise Signal + noise

>> addwhistlenssilent
Example: Creating a Train Whistle
⚫ The sound of a train whistle may sound like one note,
but it is usually the combination of three or more
tones.
fs = 8000;
t = 0:1/fs:2;
f1 = 280; a1 = .7;
f2 = 330; a2 = .7;
f3 = 380; a3 = .6;
f4 = 440; a4 = .9;
f5 = 535; a5 = 1;
s1 = a1*sin(2*pi*f1*t);
...
whistle = s1 + s2 + s3 + s4 + s5;

⚫ Listen to the train whistle.


soundsc(whistle,fs)

>> createwhistlesilent
Example: Create a mask for the whistle
A 1-second burst, 0.5-second silence, a 0.5-
second burst.
mask=ones(size(whistle));

mask(1*fs:1.5*fs) = 0;

wburst = whistle .* mask;


soundsc(wburst,fs)

>> maskwhistlesilent
Random Number Generation
⚫ The functions rand and randn are pseudo random
number generators.
doc rand
doc randn

n = .2 * randn(size(wburst));
noisyw = wburst + n;
soundsc(noisyw,fs)

>> addwhistlenssilent
Running a Script
Code Sections

Create the time


base for the signal

Set the fundamental


%%
frequency of the call

Create the
harmonics

Create the envelope

Create the call

Plot the model call


and listen to it

You might also like