MATLAB Crash Course

Post on 07-Mar-2015

47 views 6 download

Transcript of MATLAB Crash Course

!!" ""#$!!"

%&'$""

( % $ " !"& " !" ""$

!

"$ ! %

$ (") !) $

*+ * ! $ $! ,") *-.*/ "

"" $!"$ 0

!"""% *123+% !" 4

$$$#%

) "" $ !" 56

" $ 5 1$" 7 $ !"8

) "9$

!"$%$ "" &

"! """!" $50 "

%$ !$)""""$ )

$ "" ""!:

: "; "") ) & :

:"%&!"%)$"2"!"

"%

!!""" $,"$$

$ / !" " "

0

<"$! "%$ """ $"!$"!"%!%"!

; """ "$"$$

=

!!"#$"$"% "

4%! "

")9 1

1") ""!

%$"! !

!

)!%$ ""

"!&'(("$) (("$"

"!& $ "

%""+*>1$ !"

! )

((" $)

! (("

$""!$)!)

$

. 9

$ ! . !

?@@A

:

!*!*)

% ""%""$!)""

" )>+1

!$"%!(("

B*5!"$ !"$"""

!

9!""$5 !

""" !!,!!/$

"

*!!$"

!"!

+!"$"? A

&+#$",$-*

" " ! $ "

$"

?A%?A%?A% ?!A"π ?A?#A!"√,'/75 $,$/7!"& ""&!"""8

"!'""

C

! ,$"") "

!1 2 / ""!!")

" $

")

@@ & &

@@ . 2 ?."A)$?A""$#!

$

!

@@ ! ""$.D92+1"

@@ + ""$4>292+1"

@@ "! E"! """

"$

@@ E"!!" "!?!"A

; "" ! " " " """%!"; ""!

$#

F

!" &!" $

"" "

.&/0$(1**!*)$"

$!"" !!,@@ /

!*G *2

@@ =:H,CIJ/

;""" !

K

FL

$ "% " ?A

""

$ ",?&A/%!"$$!

@@ &K=:H,CIJ/

;""" !

&K

FL

" " "" $ " &!

""",""$!)"""/

,&/ ,&/ ,&/ ",&/ &!,&/

> ! 2+G1

" ,*-.*/ $(1 ) %$ !!"$

"&"

L

.&2+3*$%"$""1%(($"%$"%"*

, $ ! " 4>2 2G%.%1./%0"$ "&!

0"""?!A

$&

M"NK""!"&!

)$")!

&!% "" )"% ""

)!"

") " !

, "/ ""

""

@@&K

&K

@@&KI&7= !"

&K # " $% &

!&!" ,!/

"" $""""""

$!"" ""

&!"

@@K=O !'( $ $)&& &@@K: *'(

K

:

@@.K,'/H:P'IP

+

.K

L

J

.&4+*!*)$"$"%+$%",*

;"",?&A%?$A?<A/

@@ &K:FLO

@@ $KL=:CO @@ <K'=CO

@@

G" ",O/ ,O/

!$!, !%$$!

" "8/

@@&K:FL

&K

:FLPP

@@$KL=:C

$K

FL=C

@@<K'=C

<K

'=CPP

@@

E ,!/""G!! $&

E

@@&K:FLO

@@$KL=:CO

@@<K'=CO

@@!K<I,&I,$//Q=

!K

'=JHPP:

@@

*(")- *(")1)$"1""&"!" $ !

4&!"%$&RS

P

RSKR='=S

$ "!"$*.E***G $=%

E % ! % RS RS% "!"$RS$RS%RS$

RS%%

1!"$$% I6

9 ""$")

AAA

AAA A

232221

131211

=

BBB

BBB B

232221

131211

=

IK BABABA

BABABA

232322222121

131312121111

K /BA/BA/BA

/BA/BA/BA

232322222121

131312121111

( ) ( ) ( )( ) ( ) ( )

AAAAAA

n ^ A. n23

n22

n21

n13

n12

n11

=

$ "

""",

"!"(G*11

" &0"

&/

$ " "

"

>$ ""!"% "$&$"!

!08

=

!$1)#$*1!"! $

&

,

$$

(?2"A

4)$

$$!".9

"1)!(("#(!$1)#$*

M"!!

M"!"

M".9

:

M"!".9

!%"&!"

$"1)""&!"

")5

-,K!,B !"$

5/

!"!!

G"%$" .9 M$.9

$$ ","0)%$! $

0)/

G , "" "% !

)/"!

,K!,B!!5%55/

B!!5 !"!!$!.9

B5 ""

$ & $! 9 E>( " 0%

"

)1)!(("#(!$1)#$* "" ""$ !"$

& !"$!!$"

$1!,"/

!,B&5/

$1 " "

!"$"!"$&

>"$"$!$$1 "

! % " , "

!"$/!"$

!"$"%&!"%"$

" "" ! $ ("$ $

""!

G

!,B5/

!!$"

C

#1$")#

4!! % #1$")# "" $" $! , /

" ""!"$

")56!,B&!T'="&5%"N/

"'

'=

9 ' K ",!"/ K 4" ,/

1!!"$

= K ,=/

1!!"$"

!

K .,0/

1! !"$

1

*&!"" ,FPJPJLHPP/

* *&!"!!*,FPJPJL*HPP/

4 4&,FPJPJPL/

U *

,"!/

The % sign marks the spot where the number is inserted within the text

Formatting elements (defines the format of the number

The name of the variable whose value is to be displayed

F

" !" " & $ $! T , T "" $ $ "/

!"

$& "")

!,B&6T&T&T65%.

!./

L

& &U!" !"$ ! "

" $ !" !"%

$ "!"

U!$

!",&%$/

")*,)*$") 1

!""$&

!",&%$%B"!5%B!$G5%5!$M"/

9& $"""$!)

/$)"%<)%

")5"""/- "/$

J

$"1!$#$

!!"

$") * 1!$#$

1","/ '

+ ''

+

+' '

$"* 1!$#$

2

U

"

$

;"" $

") )

9

7 1 1!$#$

" H

."

) I

10

+

4'!

!

1&'!

G!

• !" !"• 9!$!$• !!" % """

*&!"

!",&%$/ """! )

!",&%$%55/ ""!

!",&%$%5''$5/ $"" "!

!",&%$%5I5/ !) ,I/7" !

!",&%$%55/ "!)

)

&1) ("&1) *

=P

4!!!""

!$G +! "!$

M"

9,

" /

1!

"

!,"P/

)1<,)</

1!<)

!

)*.",)"/

1!")%

""

"")

."!"%$!

)4.",

)"/

1!"

""""

)

."!

"%$!

-*" 1

5*-*" *-*

"!"&& &""$""

&"",B&5/

$"",B&5/

)$)*!((""!"

",B&5/

)5)!(("

&!"!!

',&%$/&

&,&%$%5&5/

!& !$!$

&,B&5/

9 &% 4 9 !

!!&!"

=

())$"%))5)

& ," !/ !" &!" ""

E"!9 ? & &!A

$ % $!"!$G!$M"

""

$#$ ##!)

V "

V "$"

V G"

VWX 1!

V<W<X 1!<

U) " $ $! 0 4

" "% "" " . 14

!!%"""!!*&!"

!)

$")$"%

7

*))

V"! αV βV γV θV! π

V σV ΦV+" ∆VU ΓV ΛV> ΩV1 Σ

4 & !"$ $ &""% $""% "% &

" $ !" !$G

!$M"""

&$&

&,&%$%B&5%!$G%!$M"/

!$G!M"

$

==

!$G $! % !$M" $!

!!$"!!$" "

!$G +! "!$

M"

2 1!

&

1",/

+"P

4" 1!"

"$"

G"%"

+""

4G 1!

$

""$

41< 1!< 1",!/

+"P

49 1!

%"%"

+""

." 1!"

&

."!

,=/

)'" 1!

)"

,"/

."!

,=/

*." 1!"

"&

&

."!

,=/

9 1!

"&

&

1",!/+"P

5

9 !",&%$/ &% & "

&""&

$ &!!

&

&,R&%&&%$%$&S/ 1"&$&

R&%&&%$%$&S

&0" 1"&

&0 1&0

& 1&"

$

"!"

=

2"

1!$*!$")$#$!%1

$!!"0!"!"

& " , & "/% '" ,

&"%"/%&!"!"

1"$,&%$/ "$& ",P/"$&

"&&

1"&,&%$/ "$& ",P/"&&

"$&

",&%$/ "&$ ",P/&

",%%B"!5/

"!"!,/,

/

*)$1*1*)

1"!!"$!$!

!!"

!",&%$%%%%%/

4&!"%

!",&%$%B'5%%%B''5%%%B5/

"!

>,&$/!" """ !",/!"

"!",/!"

"

=:

6")!)$"$"$#"!&#$"$-*

!' " ;$ $ "$) ",5 $$

$/

((" !$1)$" 5(1*

"1 &!"

@@K

:C

! π,:J=CLJFJ6/ @@!

K:C

! ""<"

@@!

K===P:'PC

(!$ @@K

+0,'/

1 −

,/"$"

@@

KPHPPPP

# 1"?A+0,'/

1 −

G!?A

@@#K

PHPPPP

GG GG

( ""

@@PP

9+$<

K

GG

" 2"""$ @@"@@

."&$< 2"$"&$<$

+!"$"""""$

$

+!"$"""$$< $"

=

$)()$!1)$"$)!*, ""/

""""%

$&, $ /

!$1)$" 5(1*

H H

' 1 '

I "!" I

,/+

Q 2! Q,K=/

0,&/ 10 @@0,L/

K

J

&!,&/ *&!",&/ @@&!,/

K

:L:=

,&/ "","/

@@,'=:/K

=:

",&/ G"""

"

@@",PPP/K

CJPFL

"P,&/ P"

"P

@@"P,PPP/

K

",&/ "&8K,&/,&'/,&'=/6

,&!/

@@",/K

=P

=C

$%"()$!*#"!)$"

!$1)$" 5(1*

! "π :J=CLJFJ

,&/ 1"&

,&/

@@,!C/

KPPPP

,&/ ."&,&/

@@,!C/K

PLCCP

,&/ "&,&/

@@,!C/K

PFF:

,&/ ,!"/

"&,&/

@@,!C/

K=PPPP

,&/ 1,!"/"&

,&/

@@,!C/K

:F

,&/ .,!"

/"&,&/

@@,!C/

KF=

,&/ , "

&Y/

2+G1

@@,P/

KP=C

,&/ ,

"&Y/

2+G1

@@,P/

KP:F=

,&/ ,

"&Y/ 2+G1

@@,P/

KP:CC

=F

1-*$!)$%"()$!*#"!)$"

!$1)$" 5(1*

,&/ E$!",""$

/

2e - e

(x) sinh-xx

=

@@,/

K

F=

,&/ E$!",""$ /

2e e

(x) cosh-xx +=

@@,/K

:@@

,&/ E$!",""$

/

x-x

-xx

e ee e

(x) tanh+−=

@@,/

KPFCC

"$!" %

$&,&/%,&/,&/

U!$!""

=L

()#"!)$"

!$1)$" 5(1*

,&/ 2

@@,F/

K

&,&/ 2 <,

/

@@&,/

K

=

",&/ 2 $,!/

@@",/K

",&/ 2

$,

/

@@",/

K

=@@",'J:/

K'

,&%$/ 2&

$$

@@,%/K

,&/ 12?A&@P

2?'A&ZP

2?PA&KP

@@,/K

@@,P/

KP

@@,'CLJP/

K'

$1* #()

((" !$1)$" 5(1*

4&! : @@=JPF

=J

"!"

PP ≤ ≤PPP> !"$

K

::=LC

" 4&! :

"!"

PP ≤ ≤PP> !"$

@@=JPF

K

::=LF:=LF:

1 :"!"

@@=JPFK

::=JHPP

" 1 "!"

@@=JPFK

::=LF:=LF:HPP

'&

"!

@@=JPF

K::=J

" '&"!

@@=JPFK

::=LF:=LF:

") " @@=JPF

K::

! *"!$"

"" "

!"$

" !$"

,>!!&&/

P

$*)$"#"!)$"#"*$"%

$"'"$

!$1)$" 5(1*

",/ 2

"

@@KRJ=:SO

@@",/

K:

<,/ 2 RS%

<$

@@KRCOLP=S

KC

LP=

@@<,/K=

!,%%/

2&RS "

"

3*

@@KRCOLP=SK

CLP=

@@!,%%=/K

PLC

=

,/ 9%

0& """

@@KRF:=SO

@@K,/KFPP

P:PPP=

,/ 9RS&%

""RS

@@KR=O:COFLJS

K=:C

FLJ

@@K,/

K

J

.&& )$")%")"1*)$",81*)$) clc; N = 1024; n = 0:1:N-1; a = 1; f = 1; y = a*sin(2*pi*f*n/N); figure(1); subplot(2,1,1); plot(n,y); ylim([(min(y)+min(y)*0.1) (max(y)+max(y)*0.1)]); xlim([0 N]); xlabel('sample (n)'); ylabel('amplitude x(n)'); subplot(2,1,2); z = abs(fft(y,N))*2/N; stem(n,z); xlim([0 (N/16)-1]); ylim([-max(z)*0.05 (max(z)+max(z)*0.1)]); xlabel('frequency(Hz)'); ylabel('magnitude');

0 100 200 300 400 500 600 700 800 900 1000-1

-0.5

0

0.5

1

sample (n)

ampl

itude

x(t)

0 10 20 30 40 50 600

0.2

0.4

0.6

0.8

1

frequency(Hz)

mag

nitu

de

=

0 100 200 300 400 500 600 700 800 900 1000-0.2

-0.1

0

0.1

0.2

sample (n)

ampl

itude

x(n

)

0 10 20 30 40 50 600

0.05

0.1

0.15

0.2

frequency(Hz)

mag

nitu

de

)$")1("$!" ")$3, clc; N = 1024; n = 0:1:N-1; Amax = 1; A = Amax; F = 3; fmax = 512; signal = zeros(1,N); for f = F:2*F:fmax A = (Amax*2)/(f*pi); signal = signal + A*sin(2*pi*f*n/N); end y = signal; fig=figure(1); set(fig,'Position',[100 100 800 600]); set(fig,'Color',[1 1 1]); subplot(2,1,1); plot(n,y); ylim([(min(y)+min(y)*0.1) (max(y)+max(y)*0.1)]); xlim([0 N]); xlabel('sample (n)'); ylabel('amplitude x(n)'); subplot(2,1,2); z = abs(fft(y,N))*2/N; stem(n,z); xlim([0 (N/16)-1]); ylim([-max(z)*0.05 (max(z)+max(z)*0.1)]); xlabel('frequency(Hz)'); ylabel('magnitude');

$(1*"")$!$)1"&*))

!"! "" 2. !

""$ $%&!"!

! 75"$8

% *************************************** L = 47e-3; %Use milli Henry range C = 0.1e-6; %Use sub micro Farad range R = 100; %Can be 100 Ohms or more V = 5; % *************************************** clc; fr = 1/(2*pi*sqrt(L*C)); f = 0:10:fr*3; xl = 2*pi*f*L; xc = 1./(2*pi*f*C); q = (1/R)*sqrt(L/C); fr = 1/(2*pi*sqrt(L*C)); z = sqrt((R^2)+ (xl - xc).^2); i = V./z; ph = atan((xl-xc)/R); figure(1); clf; hold off plot(f, R*i,'color','blue'); %VR hold on plot(f,xl.*i,'color','red'); %VL plot(f,xc.*i,'color','green'); %VC text(3*fr/4,max(xc.*i)+ max(xc.*i)*0.1,['fr = ' num2str(fr)]); text(3*2*fr/4,max(xc.*i)+ max(xc.*i)*0.1,['i(max) = ' num2str(max(i))]); text(3*3*fr/4,max(xc.*i)+ max(xc.*i)*0.1,['Q = ' num2str(q)]); xlim([0 fr*3]); ylim([0 max(xc.*i)+ max(xc.*i)*0.2]); xlabel('Frequency (Hz)'); ylabel('Volts'); figure(2) plot(f,ph*180/pi,'color','cyan'); %phase xlim([0 fr*3]); "!"

0 1000 2000 3000 4000 5000 60000

5

10

15

20

25

30

35

40fr = 2321.5134 i(max) = 0.049998 Q = 6.8557

Frequency (Hz)

Vol

ts

0 1000 2000 3000 4000 5000 6000-100

-80

-60

-40

-20

0

20

40

60

80

100

:

*!*)$"%&%(

!!"& ""B"5 )

!"+4 B"5 %Discrete Fourier Transform Routine. clf format long figure(1); %------------------------------- N = 32; n = 0:1:N; Sig_Freq = 3; %-x(n) Signal frequency-- signal=sin(2*pi*n*Sig_Freq/N)+0.2*sin(2*pi*n*Sig_Freq*4/N); %-plot Signal x(n)-------------- subplot(2,1,1); plot (signal,'-g'); hold on; stem (signal); xlim([1 32]); %------------------------------- m=0; for I = 1:N+1 %-Cos+jSin---------------------- real = cos(2*pi*n*m/N); imaj = sin(2*pi*n*m/N); sig_real=signal.*real; sig_imaj=signal.*imaj; %-normalise signals------------- sig1 =sum(sig_real)*2/N; sig2 =sum(sig_imaj)*2/N; %-square signals---------------- sigdft =(sig1)*(sig1); sigdft2=(sig2)*(sig2); %-square root sum of signals---- sig3 = (sigdft+sigdft2)^0.5; %-plot magnitude of harmonics--- subplot (2,1,2); hold on; stem (m,sig3);

m = m+1; end; xlim([0 33]);

5 10 15 20 25 30-1.5

-1

-0.5

0

0.5

1

1.5

0 5 10 15 20 25 300

0.5

1

1.5

0 100 200 300 400 500 600 700 800 900 1000-1

-0.5

0

0.5

1

time (t)

ampl

itude

x(t)

0 100 200 300 400 500 600 700 800 900 1000-1

-0.5

0

0.5

1

time (t)

ampl

itude

x(t)

0 5 10 15 20 25 300

0.5

1

frequency(Hz)

mag

nitu

de

3, ")$)$"

!$0

E%!"& !!"

clc; clear all; N = 1024; n = 0:1:N-1; fmax = 1; nH = 14; fig = figure(1); clf; set(fig,'Position',[100 100 800 600]); set(fig,'Color',[1 1 1]); subplot(3,1,1); hold on; y=0; for f = 1:2:nH; plot(sin(2*pi*f*n/N)/f, 'color', [1-f/nH 0.5 f/nH]); y = y + (sin(2*pi*f*n/N)/f); end ylim([-1.1 1.1]); xlim([0 N]); xlabel('time (t)'); ylabel('amplitude x(t)'); Box on; subplot(3,1,2); plot(n,y); ylim([(min(y)+min(y)*0.1) (max(y)+max(y)*0.1)]); xlim([0 N]); xlabel('time (t)'); ylabel('amplitude x(t)'); subplot(3,1,3); z = abs(fft(y,N))*2/N; stem(n,z,'-r'); ylim([-max(z)*0.05 (max(z)+max(z)*0.1)]); xlim([0 (N/32)-1]); xlabel('frequency(Hz)'); ylabel('magnitude');