برنامه نویسی به زبان matlab

130
ان ب ه ز ب ی س ی و ن ه م ا رب بMatlab 1

Upload: kali

Post on 24-Feb-2016

69 views

Category:

Documents


0 download

DESCRIPTION

برنامه نویسی به زبان Matlab. فهرست مراجع. آشنایی با Matlab. - PowerPoint PPT Presentation

TRANSCRIPT

Matlab

Matlab1 2 Matlab . . Matlab . . . . . . . 3 Matlab (MATLAB) . Matlab . . Matlab ( ) . Matlab MATrix LABoratory . . Matlab .

4 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) ... .

5 Matlab Matlab c (java) . Matlab Matlab ( ) . Matlab . - .

6 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(help only) - help(documentation and product)8 Matlab

9 Matlab

10 Matlab

11 Matlab

12 Matlab

13 Matlab

14 Matlab

15

Matlab16

Matlab17

Matlab18

Matlab19

Matlab20

Matlab21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43 ans . :a=2b= 2*3 + 5^2>> b= 31 31 . (_) .Matlab . ; . .

44 :

clear . who whose . . S1= 1 +2i> S2=3 1.5j>> S1+S2 abs angle .>> abs (s1)>> angle (s1)

forendIffunctionwhileswitchcasepersistentbreaktryelseelseifreturnglobalbreakcatchotherwise45 pi . real , imag .>> Real (1+2i)>> imag (3-1.5j)sqrt(x)inf , NaN , realmin , realmax epsFormat

46 format

47 format

48 format

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

50imag : () () Xs:step:XendLinspace(x1,x2,n) , logspace(x1,x2,n) .`

51

ones(3,2) , ones(3)zeros(3,2) , zero(3)eg: k=pi ; A=ones(2,3)*keye(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)

52 m

53

54 m Set pathRun ,f5 (save name var_name) (load name ) workspace file save workspace as.name)) workspace (load name)Input() , disp() (\n , s')55 m (function) Function output=function_name (input) : % H - (input ,.) - (disp, plot,) - ; -

56 if end . 1: a=input('please enter a number :');if a>5 b=a^2; disp('a^2 = ');disp(b);End== , ~= , >= , >, < ,=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 1 eps = eps/2;endeps = eps*2 2:s=0n=0while s> 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')65 .66r=input('please input the radius : \n');if r==0 break;endangle=linspace(0,2*pi,360);x=r*sin(angle);y=r*cos(angle);plot(x,y)xlabel('x')ylabel('y')title('circle')axis('equal')grid on 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 on67 polar ( r = f()) . r .>> theta = linspace(0,2*pi ) ;>> r = 2 * cos (2 * theta ) ;>> polar (theta , r)

68 - . . -ob --sr :dg .- +w*k x c vm ^y > > 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,..)

70 .

Linwidth MarkerEdgColor MarkerSize MarkerFaceColor 1 :x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); plot(x,y,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',... 'MarkerSize',10) 71 2:>> x = linspace(0,2*pi,31)>> plot(x,sin(x),-bs, 'LineWidth',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'})72 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 on73 .>> 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 )

74 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')

75

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

77 1:axis autoxlim ([0 2*pi])title( xlim( [0 2*pi] ) ') 2 : x=0:pi/100:2*pi;y = sin(x);plot(x,y)axis tightaxis equalaxis autoaxis squareaxis normalaxis normal tight offaxis on

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

79

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

81 3: subplot(m,n,p) .(m n p)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])

82 x=linspace(0,2*pi,41);y=sin(x).^2.*exp(-0.2*x);plot(x,y), hold onstem(x,y,.)83

>> x=linspace(0,2*pi,9);>> subplot(2,2,1)>> bar(x,x.^2+1)>> subplot(2,2,2)>> barh(x,x.^2+1)>> subplot(2,2,3)>> bar3(x,x.^2+1)>> subplot(2,2,4)>> bar3h(x,x.^2+1)84

- Matlab . .

plot3 ezplot3 comet3 stem3 scatter385 t = 0:pi/50:10*pi;plot3(sin(t),cos(t),t)grid onaxis square

86 plot3 plot :plot3(X1,Y1,Z1,...)plot3(X1,Y1,Z1,LineSpec,...)- x,y,z .close all,clc ,cleart = 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 onstem3(x,y,z,'*r') % hold offgrid onaxis equalxlabel('x')ylabel('y')zlabel('z')

87 88

88 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 equalxlabel('x'),ylabel('y') ,zlabel('z')

89 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:closeti = 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)

90 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)

91 meshgrid . . .[x,y] = meshgrid (x) [x,y,z] = meshgrid(x,y,z)Peaks matlab .[x,y,z] = peaks(n) 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')92

93 [x,y,z] = peaks(40);colormap(hot)surf(x,y,z)xlabel('x'),ylabel('y')zlabel('z'), title ('surf')

- surf RGB . .- colorbar 94 [x,y,z] = peaks(50);surf(x,y,z)colorbarxlabel('x'),ylabel('y')zlabel('z'), title ('surf')

95 contour : ( ) z=f(x,y) . . :[x,y] = meshgrid(-3:0.1:3);z = x.^2 + y.^2;contour(x,y,z,15)colorbartitle('contour ploting for z=x2+y2 ')

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

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

97 [x,y] = meshgrid(-3:1/8:3);z = peaks(x,y);surfl(x,y,z);shading interpcolormap(winter);axis([-3 3 -3 3 -8 8])

- Shading interp .Shading flat .Shading faceted . shading interp .

98 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 flatsubplot(1,2,2)surf(x,y,z), axis equal ,title('surf2')shading interp

99 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 equalshading interpcolormap copperxlabel('x'),ylabel('y')zlabel('z'), title ('surfl')

100 4:[x,y] = meshgrid(-3:1/8:3);z = peaks(x,y);surfl(x,y,z);shading interpcolormap(gray);axis([-3 3 -3 3 -8 8])

101 5:view([10 10])grid onhold onsurfl(peaks)shading interpcolormap copperhold off

view .View(az,el)defaut :Az=-37.5 , el=30o102 6: view [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)')

103 View : .[x,y,z]= peaks(30);mesh(x,y,z)view([4,4,4])

view(az,el)view([x,y,z]) view(2) az = 0, el = 90view(3) az = 37.5, el = 30az = 180; el = 90; View (az, el); 104 :ezplot('x','sin(x)')ezplot3('cos(x)','sin(x),sqrt(x))ezmesh((x^2+y^2))ezsurf('(x^2+y^2))

105 - cylinder [X,Y,Z] = cylinder[X,Y,Z] = cylinder(r)[X,Y,Z] = cylinder(r,n)cylinder(axes_handle,...)cylinder(...) 1:cylinderaxis squareh = 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 square106 sphere sphere sphere(n) [X,Y,Z] = sphere(n) ribbon ribbon(Y)ribbon(X,Y)ribbon(X,Y,width)ribbon(axes_handle,...)h = ribbon(...)rotaterotate(h,direction,alpha) rotate(...,origin)Moviegetframe107 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

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

109 danc_ribbon :function danc_ribbonj=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);

110 :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',[i-100,i,i+100],'color',[0.025*i,0.036*i,0.012*i]); F(i)=getframe;end;movie(F,20)111 . . :

fix floor ceil round mod rem 112

primes(n)n factor(n) n factorial(n) n gcd(A,B) A , B lcm(A,B)A , B Sqrt(n) n Sqrtm(a) aNthroot(m,n) n mPower(m,n)M n Pow2(n)2 n Exp(x) xLog ,log2,log10 logm(a) a- 113 SYMS Short-cut for constructing symbolic objects. SYMS arg1 arg2 ... is short-hand notation for arg1 = sym('arg1'); arg2 = sym('arg2'); ...syms x