You are on page 1of 27

MATLAB

Lecture 02

Advanced Electrical Control Lab. Department of Electrical Engine


Engineering
ering National Chung-Hsing
Chung-Hsing University


MATLAB
Script file (M-file)
MATLAB

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P2/27



MATLAB



MATLAB
Advanced Electrical Control Lab.
Department of Electrical Engineering National Chung Hsing University

P3/27


MATLAB

[ ]
,
10[2 3 5 7][2,3,5,7]


;
1~9 [1 2 3;4 5 6;7 8 9]

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P4/27

MATLAB (1)
MATLAB


()

()
sin(5)acos(0.5)abs(-12.5)
Advanced Electrical Control Lab.
Department of Electrical Engineering National Chung Hsing University

P5/27

MATLAB (2)

sumresansa1

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P6/27


//

a=3, b=11,


V=[1 3 5 7],

A=[1 0;
0 1]


sinabslength

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P7/27

syms


()

conv(p1,p2)

deconv(p1,p2)

poly(A)

roots(p)

residue(p)

pretty(p)

collect(p)

simplify(p)

simple(p)

solve(f)

dsolve(f)

diff(f)

int(f)

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P8/27



MATLAB
C
( ) ,
MATLAB

a=max(13,5) a=13
Advanced Electrical Control Lab.
Department of Electrical Engineering National Chung Hsing University

P9/27

help
(average/mean)
help average help mean

MATLABhelp
Demo

MATLAB
Advanced Electrical Control Lab.
Department of Electrical Engineering National Chung Hsing University

P10/27


MATLABM-file
function
M-file

function =(1,2,)
();
;
end


()
Advanced Electrical Control Lab.
Department of Electrical Engineering National Chung Hsing University

P11/27

M-file

MATLABC


If else
Switch case
For / While

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P12/27

IF - ELSE

if 1
1;
elseif 2
2;
else
3;
end

==

>=

<=

~=

(AND)

&&

(OR)

||


elseifelse

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P13/27

1
2,34,5
6

dice=1+round(5*rand()) %
if dice==1
result='You Lose!!'
elseif dice==2 || dice==3
result='Small!!'
elseif dice==4 || dice==5
result='Big!!'
elseif dice==6
result='You Win!!'
else
ex01.m
result='You Cheat!!'
end

1~6
rand()

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P14/27

SWITCH - CASE

Switch
if-else


C
default
otherwise
case
break

switch ()
case 1
1();
case 2
2();
case 3
3();
otherwise
();
end

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P15/27

1
2,34,5
6

dice=1+round(5*rand()) %
switch dice
case 1
result='You Lose!!'
case {2,3}
result='Small!!'
case {4,5}
result='Big!!'
case 6
result='You Win!!'
otherwise
result='You Cheat!!'
ex02.m
end

ex01
switchif-else

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P16/27

WHILE / FOR

for =::
();
end
while
();
end

for

while
Advanced Electrical Control Lab.
Department of Electrical Engineering National Chung Hsing University

P17/27



result10

result(i) result
i

while
6

for i=1:1:20
%
dice=1+round(5*rand());
result(i)=dice;
end
ex03.m
result

dice=0;
count=0;
while dice~=6
dice=1+round(5*rand())
count=count+1;
end
count

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

ex04.m
P18/27


t=0:0.1:10;
a=5;
y1=sin(a*t);
plot(t,y1)
grid on
hold on
box on
y2=cos(a*t);
plot(t,y2,'--r')
title('Title text');
xlabel('Xlabel text');
ylabel('Ylabel text');
legend('legend text1','legend text2');
axis([0 pi -1 1]);
ex05.m

MATLAB

plot

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P19/27

- plot
plot

plot(x,y,:ro,
<=
LineWidth,1,
<=
MarkerEdgeColor,k, <=
MarkerFaceColor,w, <=
MarkerSize,6)
<=
plot

plot

--

-.

plot

(Blue)

RGB(0,0,1)

>

(Cyan)

RGB(0,1,1)

<

(Green)

RGB(0,1,0)

square

(Black)

RGB(0,0,0)

diamond

(Magenta) RGB(1,0,1)

pentagram

hexagram

(Red)

RGB(1,0,0)

(White)

RGB(1,1,1)

(Yellow)

RGB(1,1,0)

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P20/27

- axis
axis([xmin xmax ymin ymax]);
or
axis ();
axis

square

equal

on

off

subplot
axis
Advanced Electrical Control Lab.
Department of Electrical Engineering National Chung Hsing University

P21/27

clear all
clc
t=-pi:2*pi/100:pi;
y=sin(t);
subplot(2,2,1)
plot(t,y)
title (')
axis([-pi pi -1 1]);
subplot(2,2,2)
plot(t,y)
title (')
axis ('square');
subplot(2,2,3)
plot(t,y)
title (')
axis ('equal');
subplot(2,2,4)
plot(t,y)
title (')
axis ('off');

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P22/27

sin / sind

(/)

sinh

cos /cosd

(/)

cosh

tan / tand

(/)

tanh

cot /cotd

(/)

coth

sec / secd

(/)

sech

csc /cscd

(/)

csch

asin / asind

(/)

asinh

acos / acosd

(/)

acosh

atan / atand

(/)

atanh

cot /cotd

(/)

acoth

sec / secd

(/)

asech

csc /cscd

(/)

acsch

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P23/27

exp

ceil

log

fix

()

log2

floor

log10

10

round

()

sqrt

abs

pow(a,b)

ab

rem(a,b)

a/b

pow2

2x

mod(a,b)

mod(a,b) = rem(a,b)+b ()

sign

>0:1, =0:0, <0:-1

isreal

sort(v)

angle

min(v)

conj

max(v)

complex

mean(v)

real

length(v)

()

imag

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P24/27

plot

loglog

title

title

semilogx

(X)

xlabel

semilogy

(Y)

ylabel

plot3

legend

line

axis

fill

grid ( on / off )

text

()

hold ( on / off )

gtext

()

box ( on / off )

polar

subplot

bar

figure

barh

comet

stairs

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P25/27


[] MATLAB 7.0 ()

[] Matlab7.0

http://www.cs.nthu.edu.tw/~jang

MATLAB

http://bime-matlab.blogspot.com/
Advanced Electrical Control Lab.
Department of Electrical Engineering National Chung Hsing University

P26/27


1. MATLAB1~50
2. function
[1~
]
calprime.medge
[Hint]1

Advanced Electrical Control Lab.


Department of Electrical Engineering National Chung Hsing University

P27/27

You might also like