You are on page 1of 121

Matlab

:
s_sanati@yahoo.com


Matlab
:
Matlab 7
:
Matlab

Matlab

.
.
Matlab
.
.

........ .
.

Matlab
( )MATLAB .
Matlab


. Matlab ( )
Matlab
MATrix LABoratory .
.

Matlab .

Matlab
Matlab .
:
x=0:0.01:10
( y=sin(2*pi*10*x
(plot(x,y

Graphical User Interfaces


C Matlab .
Matlab .
Matlab ( )The MathWorks
... .

Matlab
Matlab c
) (java .
Matlab ( Matlab )
. Matlab

-

Matlab
:
Matlab
Matlab
Matlab
Matlab
Matlab
Matlab
Matlab
Matlab
Matlab
Matlab
Matlab
Matlab
7

Matlab

cd Matlab .
java virtual machine
.
PLP
cd .
:
)(product only )help only( help - )documentation and product( help

Matlab

Matlab

10

Matlab

11

Matlab

12

Matlab

13

Matlab

14

Matlab

15

Matlab

16

Matlab

17

Matlab

18

Matlab

19

Matlab

20

Matlab

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46



ans .

:
a=2
b= 2*3 + 5^2
=>> b
31
31 .

(_)
Matlab
; .

47


:
break

case

switch

while

function

If

end

for

persistent

catch otherwise

break

global

return

elseif

else

try

clear .
whose who

<< S1= 1 +2i


>> S2=3 1.5j
>> S1+S2
abs angle

)>> abs (s1


)>> angle (s1
48

Pi .
real , image
.
)>> Real (1+2i
)>> image(3-1.5j
)sqrt(x
eps inf , NaN , realmin , realmax
Format

49

format

50

format

51

format

52


\ [] , () ; : + - * /
^ .* , `, a.*a ,
`./ ,.\ ,.^,.

53


:



()
()

Xs:step:Xend
Linspace(x1,x2,n) , logspace(x1,x2,n)
`.
54


ones(3,2) , ones(3)

zeros(3,2) , zero(3)

eg: k=pi ; A=ones(2,3)*k


eye(n) , eye(n,m)
s=[1 2 3 4] ; a=diag(s) ;ss=diag(a)
magic (n)
rand(n,m) , randn(n,m)
Randperm(n)

55

56

57

m

Set path
Run ,f5

)save name var_name(
)load name (
file save workspace workspace
)as.name)
)load name( workspace
)\n , s'( Input() , disp()
58

m
()function
)Function output=function_name (input

o
o
o

:
% H

()input ,. ()disp, plot, ; -

59



if end
.
:1
;)'a=input('please enter a number :
if a>5
;b=a^2
;)disp('a^2 = ');disp(b
End

(&&, ||, ~) :

== , ~= , >= , >, < ,<= , logical operators


if
elseif ........
else
end

60


:2
a=input('please enter a number : a = ');
if a>=5
b=a^2;
disp('a^2 = ');
disp(b);
else
disp('too little');
End

:3
a=input('please enter a number : a = ');
if a>=5 && a<=100
b=a^2;
disp('a^2 = ');
disp(b);
elseif a==0
disp('wrong number');
else
disp('invalid number');
end

61


: whileend

while expression
statements
End

: switch....caseend
SWITCH switch_expr
CASE test1,
(commands1)
CASE {test2_1, test2_2, test2_3,...}
(commands1)
...
OTHERWISE,
(commands)
END

62


:1
eps = 1;

while (1+eps) > 1


eps = eps/2;
end
eps = eps*2

:2
s=0
n=0
while s<1000
n=n+1;
s=s+n^2;
end
out=[n-1 s-n^2]
% .....sum((1:13).^2) ,n=13

63


:3
number = input ('please input the number :','s')

switch lower(number)
case 'one'
disp('number is 1')
case 'two'
disp('number is 2')

case 'three'
disp('number is 3')
case 'zero'
break;
otherwise

disp('Unknown number')
end

64


: Forend

:While..end

return , continue , break


:1
;)'N = input('input a number : \n
for R = 1:N
for C = 1:N
;)A(R,C) = 1/(R+C-1
end
end
;)disp(A

65


:2
k= input('enter a number: ');
for m = 1:k
for n = 1:k
if m == n
a(m,n) = 2;
elseif abs(m-n) == 2
a(m,n) = 1;
else
a(m,n) = 0;
end
end
end
disp(a);
66


Matlab
.


plot

plotyy
semilogx
semilogy
loglog
polar

67


plot .)>> Plot(x,sin(x),x,cos(x),.
plot .)]>> Plot([0 5 6 -1 0],[-1 0 3 2 -1
grid on ))>> plot (x,x.*sin(x
>> grid on
semilogx .;)>> x=linspace (1,100,8
;>> y=x/2
)'>> semilogx (x,y,'-o
68


semilogy ;)>> x = linspace (1,5
;)>> y = 2*exp(x
>> semilogy (x,y) , grid on
loglog .;)>> logspace (0,4
;)>> exp (-0.3*x
>> loglog (x,y) , grid on
plotyy y
;)>> x = linspace (0,2*pi
>> plot (x , sin(x) ,x, exp(x)) , grid on
>>
>> plotyy (x , sin(x) ,x, exp(x)) , grid on

69


polar () ) r = f( . r .

; ) >> theta = linspace(0,2*pi


; ) >> r = 2 * cos (2 * theta
)>> polar (theta , r

70

.
.

--

-.

>

<

71


:1
;)>> x=linspace (0,2*pi,31
;)>> y = sin(x
)>> plot (x,y, -*r, x ,cos(x) ,ob--
:2

)>> plot (x , sin(x) , dk



.

)>> plot(x ,y ,string, property1 ,value1,..

72

: 1

Linwidth

MarkerEdgColo
r

MarkerSize

MarkerFaceCol
or

;x = -pi:pi/10:pi
;))y = tan(sin(x)) - sin(tan(x
plot(x,y,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',...
)'MarkerFaceColor','g',... 'MarkerSize',10
73


:2
>> x = linspace(0,2*pi,31)
>> plot(x,sin(x),-bs,linwidth ,2)

:3
x = -pi:.1:pi;
y = sin(x);
plot(x,y)
set(gca,'XTick',-pi:pi/2:pi)
set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'})

74


hold .
)>> x= linspace(0,2*pi,31
)>> plot (x,sin(x) ,-bs
>> hold on
)>> plot(x ,cos (x), :ro
>> hold off

.
;>> cx = (0:pi/4:pi)' + rand(5,1)*I
;)>> cy = sin(cx
>> plot(cx, cy, --^) ,grid on

75

;)>> t = linspace (0,2*pi,9


;)>> y = cos(t) + i*sin(2*t
)>> plot (y ,b-o
,xlabel, title , legend , ylabel
.
;)>> t = linspace (0,5
;>> h = 30*t 0.5*9.81*t.^2
>> plot (t ,h ,b-o) ,grid on
))>> xlabel (time (s
))>> ylabel (height (m
) >> title ( h(t) = v0t - g t^2 , v0 = 30 m/s
76


:1
x=0:pi/100:2*pi;
y1 = sin(x);
y2 = sin(x - 0.25);
y3 = sin(x - 0.5);
plot(x,y1,x,y2,x,y3)
legend('sin(x)' , 'sin(x-0.25)' ,'sin(x-0.5)')

: 2
plot (x,y1, 'c--*', x ,y2,'r-d', x,y3, 'k-.p')

77

78


axis .
.
; x = 0:.025:pi/2
)' plot(x,tan(x),'-ro
)] axis([0 pi/2 0 5

79


:1
axis auto
xlim ([0 2*pi])
title( xlim( [0 2*pi] ) ')

: 2
x=0:pi/100:2*pi;
y = sin(x);
plot(x,y)
axis tight
axis equal
axis auto
axis square
axis normal
axis normal tight off
axis on
80


.
:
area , bar , barh ,bar3 ,bar3h ,pie ,stemscatter
,..
: 1
;] Y = [1, 5, 3;3, 2, 7; 1, 5, 3; 2, 6, 1
) area(Y
grid on
colormap summer
)' set (gca,'Layer','top
' title 'Stacked Area Plot
81

82


:2
t=linspace(0,2*pi,31);
subplot(2, 1,1)
area(t,sin(t),'facecolor','g'),axis tight
subplot(2,1,2)
area(t',cos(t),'linewidth',2), axis tight

83


subplot(m,n,p) :3
)p n m (.

84

p = [5.2 3.6 1 9];


subplot(2,2,1), pie(p)
subplot(2,2,2), pie(p,p==max(p))
subplot(2,2,3), pie3(p)
subplot(2,2,4), pie3(p,[1 0 0 0])

Matlab .
.

plot3

ezplot3

comet3

stem3

scatter3

85


t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t)
grid on
axis square

86


: plot plot3

plot3(X1,Y1,Z1,...)
plot3(X1,Y1,Z1,LineSpec,...)
. x,y,z close all,clc ,clear
t = linspace(0,2*pi,41);
x = -3*sin(t).*exp(-0.05*t);
y = 3*cos(t).*exp(-0.05*t);
z = 0.5*t;
plot3(x,y,z,'linewidth',2)
hold on
stem3(x,y,z,'*r')
%
hold off
grid on
axis equal
xlabel('x')
ylabel('y')
zlabel('z')

87


ezplot3 m .
close
;')xstr = '-3*sin(u)*exp(-0.1*u
;')ystr = '2*cos(u)*exp(-0.1*u
'zstr = '0.3*u
)]ezplot3(xstr,ystr,zstr,[0 4*pi
axis equal
)'xlabel('x'),ylabel('y') ,zlabel('z

88


. comet3 comet3(z)

comet3(x,y,z)
comet3(x,y,z,p)
comet3(axes_handle,...)

:1
t = -10*pi:pi/250:10*pi;

comet3((cos(2*t).^2).*sin(t),(sin(2*t).^2).*cos(t),t);

:2
close
ti = linspace(0,6*pi,5000);

xi = -3*sin(ti).*exp(-0.05*ti);
yi = 3*cos(ti).*exp(-0.05*ti);
zi = 0.3*ti;
comet3(xi,yi,zi)
89


Matlab z . mesh , surf .
;) [x,y] = meshgrid (-8:0.5:8
; r = sqrt (x.^2 + y.^2) + eps
; z = sin(r)./r
) mesh(x,y,z
) xlabel('x'),ylabel('y
)' zlabel('z'),title('mesh

.
) surf(x,y,z

90


meshgrid . . .
)[x,y] = meshgrid (x
)[x,y,z] = meshgrid(x,y,z
Peaks matlab . n
)[x,y,z] = peaks(n
:1
;)[x,y] = meshgrid(-3:0.1:3
;z = x.^2 + y.^2
)mesh(x,y,z
)colormap(jet
)'xlabel('x'),ylabel('y
)'zlabel('z'), title('mesh plot for z = x^2 + y^2

91

92


;) [x,y,z] = peaks(40
) colormap(hot

) surf(x,y,z
)' xlabel('x'),ylabel('y
)' zlabel('z'), title ('surf

surf RGB .
.
colorbar

93


[x,y,z] = peaks(50);
surf(x,y,z)

colorbar
xlabel('x'),ylabel('y')
zlabel('z'), title ('surf')

94


:contour ( ) ) z=f(x,y
. .
:
;)[x,y] = meshgrid(-3:0.1:3
;z = x.^2 + y.^2

)contour(x,y,z,15
colorbar
)' title('contour ploting for z=x2+y2

95


;)[X,Y,Z] = peaks(30
)surfc(X,Y,Z
colormap hsv
)]axis([-3 3 -3 3 -10 5

4
.

) z=f(x,y .
Surfc () .. - meshc .

- surfl .

96


;)[x,y] = meshgrid(-3:1/8:3
;)z = peaks(x,y
;)surfl(x,y,z
shading interp
;)colormap(winter
)]axis([-3 3 -3 3 -8 8
Shading interp .
Shading flat .
Shading faceted .
shading interp
.
97


:1
[x,y] = meshgrid(-pi:0.1*pi:pi);
z = x.*cos(y)/2;
surf(x,y,z), axis equal ,title('surf')
xlabel('x'),ylabel('y'),zlabel('z')
:2
[x,y] = meshgrid(-pi:0.1*pi:pi);
z = x.*cos(y)/2;
subplot(1,2,1)
surf(x,y,z), axis equal ,title('surf1')
shading flat
subplot(1,2,2)
surf(x,y,z), axis equal ,title('surf2')
shading interp
98


:3
[x,y]=meshgrid(-10:0.1:10);

r = sqrt(x.^2 + y.^2)+eps;
z = 4*sin(r)./r;
surfl(x,y,z), axis equal
shading interp

colormap copper
xlabel('x'),ylabel('y')
zlabel('z'), title ('surfl')

99


:4
[x,y] = meshgrid(-3:1/8:3);

z = peaks(x,y);
surfl(x,y,z);
shading interp
colormap(gray);
axis([-3

100

-3

-8

8])


:5
view([10 10])
grid on
hold on
surfl(peaks)
shading interp
colormap copper
hold off
view
.
View(az,el)
defaut :Az=-37.5
101

, el=30o


view :6
[x,y,z] = peaks(30);
subplot(2,2,1)
mesh(x,y,z,'edgecolor','k')
axis tight, xlabel('x'),ylabel('y'),zlabel('z')
title(Default')
subplot(2,2,2)
mesh(x,y,z,'edgecolor','k')
axis tight, xlabel('x'),ylabel('y'),zlabel('z')
view(20,60)
title('view(20,60)')
subplot(2,2,3)
mesh(x,y,z)
axis tight, xlabel('x'),ylabel('y'),zlabel('z')
view(210,20)
title('view(210,20)')
subplot(2,2,4)
mesh(x,y,z)
axis tight, xlabel('x'),ylabel('y'),zlabel('z')
view(0,90)
title('view(0,90)')

102


. : View
[x,y,z]= peaks(30);

mesh(x,y,z)
view([4,4,4])

view(az,el)
view([x,y,z])
view(2)
view(3)
az = 180;
el = 90;
View (az, el);

103

az = 0, el = 90
az = 37.5, el = 30


:
ezplot('x','sin(x)')

ezplot3('cos(x)','sin(x),sqrt(x))
ezmesh((x^2+y^2))
ezsurf('(x^2+y^2))

104


cylinder - -

[X,Y,Z] = cylinder
[X,Y,Z] = cylinder(r)
[X,Y,Z] = cylinder(r,n)
cylinder(axes_handle,...)
cylinder(...)
:1

cylinder
axis square
h = findobj('Type','surface');
set(h,'CData',rand(size(get(h,'CData'))))
:2

t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(2+cos(t));
surf(X,Y,Z)
axis square
105


sphere

sphere
sphere(n)
[X,Y,Z] = sphere(n)
ribbon ribbon(Y)
ribbon(X,Y)
ribbon(X,Y,width)
ribbon(axes_handle,...)
h = ribbon(...)
rotate rotate(h,direction,alpha)
rotate(...,origin)
Movie getframe 106


:1
h = surf(peaks(20));
rotate(h,[1 0 0],180)
:2
h = surf(peaks(20));
zdir = [0 0 1];
center = [10 10 0];
rotate(h,zdir,45,center)
: 3
[x,y] = meshgrid(-3:.5:3,-3:.1:3);
z = peaks(x,y);
ribbon(y,z)
xlabel('X')
ylabel('Y')
zlabel('Z')
colormap hsv
107


:3
Z = peaks; surf(Z);

axis tight
% Record the movie
for j = 1:20
surf(sin(2*pi*j/20)*Z,Z)
F(j) = getframe;
end
% Play the movie ten times
movie(F,10)

108


:danc_ribbon
function danc_ribbon
j=1;
for i=1:20
[x,y]=meshgrid(-3:0.5:3,-3:0.1:3);
z=peaks(x,y);
ribbon(y,z)
light('Position',[i-10,i,i+10],'Style','infinite');
axis off;
F(j)=getframe;
rotate(ribbon(y,z),[1,0,0],180);
light('Position',[i+10,i,i-10],'Style','infinite');
axis off;
F(j+1)=getframe;
j=j+2;
end;
movie(F,10);
109


:sphere_light_rotate
function sphere_light_rotate()

[x,y,z]=sphere(50);
for i=1:250
h=surf(x,y,z);
axis off

rotate(h,[1,0,0],i+45);
light('Position',[i100,i,i+100],'color',[0.025*i,0.036*i,0.012*i]);
F(i)=getframe;
end;
movie(F,20)

110


. . :

fix

floor

ceil

round

mod

rem

111

)primes(n

)factor(n

)factorial(n

A , B

A,B

)gcd(A,B
)lcm(A,B


n
a
n m

)Sqrt(n
)Sqrtm(a
)Nthroot(m,n

M n

)Power(m,n

2 n

)Pow2(n

)Exp(x
Log
,log2,log10
)logm(a

112


p(x) = x3 -2x -5 .
:

p(x) =1x3 -0x2 -2x -5


] P = [1 0 -2 -5

p
.
) r = roots(p

= r
2.0946
-1.0473 + 1.1359i
-1.0473 - 1.1359i

roots .
poly .

-5.0000

-2.0000

)>> pp = poly(r
= pp
1.0000
-0.0000

113

poly

poly

.
:
;] A = [1.2 3 -0.9;5 1.75 6 ;9 0 1
) s = poly(A
= s
1.0000
-3.9500
-1.8500 -163.2750
S | S()= |A - I . s S .
A :
S()= 3 3.95 2 1.85 163.2750
)B = roots (s
= b
7.2826
-1.6663 + 4.4321i
-1.6663 - 4.4321i
114


polyval x .
] P = [1 0 -2 -5
) Pm = polyval (P, 5

Pm = (5)3 -2(5) -5 = 110


polyvalm .
.
P(x)= x3 -2x -5

]P = [1 0 -2 -5

;]X = [2 4 5;-1 0 3; 7 1 5

)Y = polyvalm (P,X

= y

439

179

377

136

81

111

639

253

490

115


conv
.
;] a = [1 2 3
;] b = [4 5 6
) c = conv(a,b

= c

4
13
28
27
18
C = (x2+2x+3)(4x2+5x+6)=4x4+13x3+28x2+27x+18

deconv
) [q,r] = deconv (c,a
= q

= r
0

116


polyder
polyder(p)

ans =
3
0
-2
(x3-2x-5)=3x2-2
a = [1 3 5];
b = [2 4 6];

c = polyder(a,b)

c =
8

30

56

38

[q,d] = polyder(a,b)

q =
-2
d =
4
117

-8

-2

16

40

48

36



polyint(a)

ans =
0.3333

1.5000

5.0000

polyint(a,4)

ans =
0.3333

1.5000

5.0000

4.0000

b = [-4 8]; a = [1 6 8];


[r,p,k] = residue(b,a)

r=
-12
8
118

p=
-4
-2

k=
[]


polyfit x ,y n .
) P = polyfit (x,y,n

; ] X = [1 2 3 4 5
] Y = [5.5 43.1 128 290.7 498.4
) P = Polyfit (X,Y,3
=P
-0.1917 31.5821 -60.3262 35.3400

119

120

121

You might also like