You are on page 1of 79

MATLAB

The Language of Technical Computing


Command Window
<< 5*4
ANS=20
ANS ,
.
<<80/4
ANS=20
<<80\4
ANS=0.0500

: .

5+3

A+B

23-12

A-B

3*23

A*B

24/8 OR 24\8

\ /
OR

5^2

A/B OR B\A

AB

;>>A=15
B,A
;<<B=5
<<C=(((A+B)*3-50)/2)^2
C =25
))<<C=(((A+B)*B)/(B^2
C =4

MATLAB -
:
=Pi =3.1415
3.1416
3.14159265358979
3.1416e+000
3.141592653589793e+000

:
:
:

>>clc
>> who
>>clear


19 ,
.
.

ans

pi

eps

inf

0/0 :

NaN

j = i = 1

i,j


:
eps=7
clear eps

1*M

- () , M .
.
-first .
-Increment .
( )1
-last .

= first : last
= first : increment : last
x=2 3 4 5 6
x=2 2.5 3 3.5 4

-first .
-last .
-n .
(
x=2 3 4 5 6
)100
x=1 10 100 1000 10000

>>x=2:6
>>x=2:0.5:4

) = linspace(first,last,n
) = logspace(first,last,n
)>>x=linespace(2,6,5
)>>x=logspace(0,4,5

"
[,,,,] =
]>>x=[23,4,66,12,56
x=23 4 66 12 56

" , .
;>>x=0:0.05:4*pi
;)>> y=sin(x
)>> plot(x,y

" , .
;)>> x=linspace(0,10,11
;>> y=2*x
)>> plot(x,y


.j -i


Re al number a
Im aginary number b
angle
abs of a and b M
>>A=1+2i
>>M=abs(A)
>>teta=angle(A)
>>a=real(A)
>>b=imag(A)

A=1.0000+2.0000i
M=2.2361
teta=1.1071
a=1
b=2

M M e j a jb
M a2 b2
a M cos

b
tan 1
a
b M sin


)conj(A

)[teta,r]=cart2pol(x,y

)[x,y]=pol2cart(teta,r


.
MATLAB .
" " " " ,
.
?
[ >>x=[Re_1+j*IM_1 , Re_2+j*IM_2 , Re_3-jIM_3 ,..,
:

]>>x=[3+j*2, 5-j , 7+2*j , 4+0*j

7.0000 + 2.0000i 4.0000

x = 3.0000 + 2.0000i 5.0000 - 1.0000i

( )
: "
X= 0 1 2 3 4 5

.
.
.
.
, .
.
.

X=0.5 1.5 2.5 3.5 4.5 5.5

>> X=0.5+X

X= 1

>>X= 2*X

X= 2

>>X= X+X

36 100 196 324 484

X=4

>>X=X.*X

81 121

49

25

X=1

>>X=X/4

X=1

)>>X=sqrt(X

125 343 729 1331

27

11

10 14 18 22

11

X=1

>>X=X.^3


:
A=1 2 3 4
B=5 6 7 8

21

.
]>>C=[A , B
C=1 2 3 4 5 6 7 8
.
)>>D=C(4
D=4
.
)>>E=C(7:-2:2
E=7 5 3
.
<<F=E
F=7
5
.
3
.
>>Scalar_Number=E*F Scalar_number = 83
' '1 '.'0
35

Matrix_3_3 = 49
21

>>Matrix_3_3=F*E
15 25 5
9 15

G=1+5i 2+6i 3+7i 4+8i


ans_1=1 1 1
ans_0=0 0 0

>>G=A+B*j
)>>ans_1=ones(1,3
)>>ans_0=zeros(1,3

N*1

:
= first : last
= first : increment : last
'[.] =

" ' ".


: " ' ",
,
,
.

"
,
" " ; ".

>>x=2:0.5:4 x=2 2.5 3 3.5 4


<<x=x.
x=2
2.5
3
3.5
4
[; =[num1;num2;num3;num4
x=23
4
66
12
56

]>>x=[23;4;66;12;56

" "
:
Ans_0=0 0 0

;]>> y=[Ans_0 1 Ans_0 1 Ans_0 1


;>> x=1:16
)>>plot(x,y

:
; <<A=1 2 3
; <<B=2 1 0
; ]>>y=[A B A B A B A
; <<x=0:20
)<<plot(x,y


x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the Sine
Function')

-
t = 0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
subplot(2,2,1)
plot(t,y1)
subplot(2,2,2)
plot(t,y2)


linear plot
plot
discrete plot
stem
add grid lines
Grid
add X-axis label
xlabel
add Y-axis label
ylabel
add graph title
title
divide figure window subplot
create new figure window
figure
wait for user response pause


: A
;]))A=[A A(length(A

: A
;)A=A(1:length(A)-1


?
for t=-pi:pi/6:pi
X(t)=sin(t);
end


..

;t=-pi:pi/6:pi
)for n=1:length(t
;))X(n)=sin(t(n
end

for t=-pi:pi/6:pi
;)X(floor(t*6/pi +7))=sin(t
end
t*6/pi +7
double integer floor
2.000 2

N*M

N , M.

( .) N=1
, .
,
, 3
. , :
,X ,Y .Z
. :,
, , .

.

N*M
num1,n

num2,n
num3, n

num4, n

num5,n
numm ,n

num1,3

num1, 2

num2,3

num2, 2

num3,3

num3, 2

numm , 4 numm ,5

numm ,3

numm , 2

num1,1

num2,1
num
3,1

.
num
m ,1

N , M.
.
" " . DC .AC


, ,
, " ; ".
, :
]<<A=[1 2 3 ; 4 5 6 ; 7 8 9
=A
1 2 3
4 5 6
7 8 9

, :
;>>line_1=1:3
;<<line_2=4:6
;<<line_3=7:9
]<<A=[line_1;line_2;line_3
=A
1 2 3
4 5 6
7 8 9


>> a=[1 2];
>> b=[3 4];
>> A=[a;b]
A=
1 2
3 4

<<a=linspace(1,3,3);
>>b=linspace(4,6,3);
>>c=linspace(7,9,3);
>>A=[a;b;c]
A=
1 2 3
4 5 6
7 8 9

2 * 2

3 * 3

" : , .
: :
=A
3
6
9

2
5
8

1
4
7

? 8
;ans=8

)>>A(3,2

? 3
ans=7 8 9

)>>A(3,:

? 1
ans=1
4
7

)>>A(:,1


]B=[b1,b2,..,bN
C=Scalar

]A=[a1,a2,..,aN

]C+A=[C+a1,C+a2,,C+aN

]C*A=[C*a1,C*a2,C*aN

]A+B=[a1+b1,a2+b2,,aN+bN

]A. *B=[a1*b1 , a2*b2 , . . . , aN*bN

]A. /B=[a1/b1 , a2/b2 , . . . , aN/bN

]A. \B=[a1\b1 , a2\b2 , . . . , aN\bN

] A. ^B=[a1^b1 , a2^b2 , . . . , aN^bN

4 * 4

=A
3
6
9
A=1 2 3
7 8 9
A=2 3
8 9
3
9
3
9

2
8
2
8

3
9
3
9

A= 2
8
2
8

2
5
8

1
4
7
][=)>>A(2,:

][=)>>A(:,1

]A=[A A ; A A


3
9
3
9

2
8
2
8

3
9
3
9

A= 2
8
2
8

.
N=4, M=4 ,9 .13
A= 2 3 2 3
8 9 8 9
2 3 2 3

]>>A(4,4)=[13

8 9 8 13
, .2,3
5 .8,9
A= 2 3 2 3
8 9 8 9
2 3 2 3
8 5 5 13

]>>A(4,2:3)=[5

' .'1 ?
A= 2 1 2 3
8 1 8 9
2 1 2 3

]>>A(:,2)=[1

8 1 5 13

?
A= 0 0 0 0
0 0 0 0
0 0 0 0

]>>A(:,:)=[0

0 0 0 0

' ,'1 ,2,3 2,3


A= 0 0 0 0
0 1 1 0
0 1 1 0

0 0 0 0

]>>A(2:3,2:3)=[1


)A(r,c

,A r
.c

)A(r,:

,A
.r

)A(:,c

,A
.c

)A(:

,A
.
.

)A(i

i A .

A1

) flipud(A
) fliplr(A .
) reshape(A,m,n .A
) rot90(A 90
.
) inv(A ,A
-
) det(A
0-

A=0 0 0
0 0 0

A=1 1 1
1 1 1
1 1 1
A=1 0 0
0 1 0
0 0 1
A=0.2343 0.2556 0.4288
0.5273 0.7723 0.3327

)>>A=zeros(2,3

)>>A=ones(3

)>>A=eye(3

)>>A=rand(2,3

-
:

-
size
:

length
:

-
) -sum(A :A

-
-A

-
,diag ,
:

-
-find
:
:
:0

-
"
:

-
det(A) :
:

-
:
i=1 6 nn

-
:

n 10

]k [1..30

1 n
) (1
n

-
:

3X2 :
]>>A=[1 -1 2;0 1 1
=A
1
0

-1
1

2
1

)>>pinv(A
= ans

-0.0909 0.1818
0.6364 -0.2727
0.3636 0.2727

2X3 :
]>>B=[1 -1;2 0;3 3
=B
1
2
3

-1
0
3

)>>pinv(B
= ans
0.2632 0.2368
-2105 -0.2895

0.0789
0.2368



, :
[1 -2 3] * [2 -1 0[ = ]1 -2 3] * 2 = 4
-1
0
:
1 2 * 1 = 19
-3 0
9
-3

a1x+b1y=c1
a2x+b2y=c2
" :
x = a1 b1 -1 * c1
y = a2 b2
c2
[>> [x y]=inv([a1 b1;a2 b2])*[c1 c2

-
:for
for s:d:f
statements

end

-
:continue

.
:break .
,
.


:
)f(t)=n=1 10 anSin (nt

0<t<2, an=()n

- for
clear all;
close all;
N=10;
% Number of coefficients
a=0.5.^(1:N); % The Coefficients
t=0:pi/20:pi; % Time axis to be monitored
for L=1:length(t) % For every value of t
for k=1:N % All the coefficients
g(k)=a(k)*sin(k*t(L)); % The scalar product
end
f(L)=sum(g); % Applying Sigma
end
stem)t,f,r(
Xlabel)t(;
Ylabel)f)t((;

matlab
:
function [output_vars]=function_name)input_vars(

-output_vars
. -function_name
.
-input_vars

-matlab
:
. .
help
".
lookup
.

-matlab
:2 :calc_area_perimeter.m

-matlab
:

-
n!

function x=Atsrt(n)
x=1;
for k=1:n
x=x*k;
end
>>atsrt(5)

ans =
120

)function u=step_fn(t
;)u=0.5*(sign(t+eps)+1
end

clear all;
close all;
t=-10:0.005:10;
x=step_fn(t-2);
plot(t,x)
axis([-10 10 0 1.5]);
xlabel('t');
ylable('u(t-2)');

-

function imp=impls_fn(t,delta)
imp=0.5*( step_fn(t+delta/2)-step_fn(t-delta/2) );
end

plot(t,40*impls_fn(t+3.5,0.05));
axis([-10 10 0 25]);
xlabel('t');
ylabel('delta(t+3)');

function y=pls_fn(t)
y=step_fn(t+0.5)-step_fn(t-0.5-eps);
end

)function r=rmp_fn(t
;)r=0.5*t.*(sign(t)+1
end


matlab
conv(x,y) : z
Length(x)+Length(y)-1
matlab
. fft(x) :


Matlab
trapz

.
;t=0:0.01:pi
;)y=sin(t
)s=trapz(t,y
>>area

=s
2.0000


roots
:
F(s)=aosn+a1sn-1+a2sn-2+an
roots([a0 a1 a2 [( :
)]>> roots([-1 2 1

= ans
-0.4142
2.4142


) D=eig(A A
AX=X :
)]>>d=eig([1 -9;5 1
=d

i6.7082 + 1.0000
i6.7082 - 1.0000



) zplane(z,p z
p .
)>> zplane(0.5+0.25i,0.5-0.25i


1
0.8
0.6

Imaginary Part

0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
-1

-0.5

0
Real Part

0.5

Extra Functions :

mesh(x,y,z)
. z - x,y
:
x=-1:0.1:1;
y=-1:0.1:1;
for m=1:length(x)
for n=1:length(y)
z(m,n)=x(m)^2*y(n)^2;
end
end
mesh(x,y,z)


meshgrid
.
,
.
f(x,y)=x+y
]]/x , /y[ = ]2x+y,2y+x


[x,y]=meshgrid(-2:0.1:2);
z=x.^2+y.^2;
[px,py] = gradient(z,0.1,0.1);
contour(x,y,z)
hold on;
quiver(x,y,px,py)


:
R=f)t(+g)t(+h)t( plot3
;>>t=-3*pi:0.1:3*pi
))>>plot3(t,cos(t),sin(t

You might also like