You are on page 1of 6

QQ993878382
Email993878382@qq.com
http://top99.blog.hexun.com/46036333_d.html

QQ

Matlab

Matlab

Matlab
Current
Directory
Workspace

Command History

Command Window

1
1.1 Matlab
MATLABMathWorks

MATLABMatrix Laboratory
MATLAB 5.XRelease 111997
MATLAB 6. Release 12, 2000
MATLAB 7.0Release 14, 2004
MATLAB
MATLAB
MATLAB

Matlab

Matlab

2 MATLAB

1.2 Matlab
1
C

2forwhileif
switch
3

5MATLAB

Matlab

x1+2x2+4x3 = 2.1

Current
Directory
Workspace

Command History

Command Window

Matlab

3x1+ x2 - x3 = 3.6

Ax=b
x=A-1b

-x1+4x2+5x3 = -1.4

20x6sin(2x)
sinx2sin2x
>>x=linspace(0,6,100);
>>y1=sin(2*x),y2=sin(x.^2),y3=(sin(x)).^2;
>>plot(x,y1,x, y2,x, y3)

## >>A=[3 1 -1;1 2 4;-1 4 5]; b=[3.6;2.1;-1.4];

>>x=A\b
x=
1.4818
-0.4606
0.3848

Matlab

3 z = sin( x + y ) / x
(x,y) (-8,8).
2

+y

Matlab

[
X,Y]=meshgrid(-8:0.5:8);
R=sqrt(X.^2+Y.^2)+eps;
Z=sin(R)./R;
surf(X,Y,Z);
colormap(cool);
xlabel('x');
ylabel('y');
zlabel('z');

4 MATLAB
4.1 Help
4.2 MATLAB

doc

help
M
helpwin
lookfor
M

Matlab

5 MATLAB
MATLAB

5.1 MATLAB
MATLAB

5.1.1
MATLAB
1
23131
MATLAB
3

Matlab

pi
eps
inf
NaN
realmax
i, j

5.1.2

>>s=matrix laboratory;
s=
matrix laboratory
>> size(s)
% size
ans=
1 17

5.2
5.2.1
1

>>a=1:0.5:4
a=
Columns through 7
1
1.5
2
2.5
3
3.5
4

>> a=1+2*3
a=
7

>> 1+2*3
ans=
7

Matlab

5.1.3

Matlab

## >>A=[1 2 3;4 5 6;7 8 9]

A=
1 2
3
4 5
6
7 8
9
3
zeros(m)
eye(m)
ones(m)
rand(m)
randn(m)

2

data1.txt
1 1 1
1 2 3
1 3 6

m0
m
m1
m
m

Matlab

Matlab

5.2.2

det(a) a

1
2

6.1

eig(a) a

3
>> a=[2 1 -3 -1; 3 1 0 7; -1 2 4 -2];
>> a1=det(a);
>> a2=det(inv(a));
>> a1*a2

## >> y=[0 0.58 0.70 0.95 0.83 0.25];

>> plot(y)
>> x=linspace(0, 2*pi, 30);
>> y=sin(x);
>> plot(x,y)

inv(a) a
rank(a) a
trace(a) a

0.9
0.8

5.2.3

0.7

0.6

0.8
0.6

0.5

.*./.^

0.4

0.4

0.2

0.3

0.2

-0.2

0.1
0

-0.4

1.5

2.5

3.5

4.5

5.5

-0.6

-0.8
-1

Matlab

6.2

## x=linspace(0,2*pi,30); y=sin(x); z=cos(x);

u=2*sin(x).*cos(x); v=sin(x)./cos(x);
subplot(2,2,1),plot(x,y),axis([0 2*pi -1 1]),title('sin(x)')
subplot(2,2,2),plot(x,z),axis([0 2*pi -1 1]),title('cos(x)')
subplot(2,2,3),plot(x,u),axis([0 2*pi -1 1]),title('2sin(x)cos(x)')
subplot(2,2,4),plot(x,v),axis([0 2*pi -20 20]),title('sin(x)/cos(x)')

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

s in(x)
0

cos (x)

0.5

0.5

- : -. - -
. + * x x o
yrg bwkmc.

-0.5

-0.5

-1

-1

0.5

10

-0.5

-10

s in(x)/cos (x)
20

Matlab

6.4

2s in(x)cos (x)
1

-1

3 z = sin( x 2 + y 2 ) /
(-8,8).

Matlab

0.8

-1

6.3

>> x=0:pi/15:2*pi;
>> y1=sin(x)
>> y2=cos(x)
>> plot(x, y1,'b:+', x, y2, 'g-.*')

-20

Matlab

6.5
x 2 + y 2

[
X,Y]=meshgrid(-8:0.5:8);
R=sqrt(X.^2+Y.^2)+eps;
Z=sin(R)./R;
surf(X,Y,Z);
colormap(cool);
xlabel('x');
ylabel('y');
zlabel('z');

## x=sint, y=cost, z=t

>> t=0:pi/50:10*pi;
>> plot3(sin(t),cos(t),t)
40

30

20

10

0
1
0.5

1
0.5

0
0

-0.5

-0.5
-1

-1

Matlab

7. MATLAB
7.1
7.1.1 for ... end
>> mysum=0; for i=1:1:100, mysum=mysum+i; end; mysum
7.1.2 while ... end
>>mysum = 0; i=1; while (i<=100), mysum=mysum+i; i=i+1; end
7.1.3 (ifelseifelseend)
7.1.4

switch input_num
case 0
disp('zero');
case 1
disp('positive one');
otherwise
disp('other value');
end

Matlab

8. M
MATLABMM (Script Mfiles )M (function M-files),
ASCII
M
function [] = ()
%

circ.m
>> [x,y]=meshgrid(-100:1:100);
>> z=circ(x,y,50);
>> imshow(z);

Matlab

Matlab

circM

RectM

function z = circ(x,y,d)
% CIRC(X,Y) generates samples of a continuous, aperiodic,
% unity-height cylinder at the points specified in array (X,Y)
% CIRC(X,Y) is a cylinder of height 1 and diameter 1
% CIRC(X,Y,d) is a cylinder of diameter d
%
error(nargchk(2,3,nargin)); nargin
if nargin<3, d=1; end

unction y = rect(t,Tw)
%RECT Sampled aperiodic rectangle generator.
% RECT(T) generates samples of a continuous, aperiodic,
% unity-height rectangle at the points specified in array T, centered
% about T=0. By default, the rectangle has width 1.
% RECT(T,W) generates a rectangle of width W.
%
error(nargchk(1,2,nargin));
if nargin<2, Tw=1; end % Returns unity in interval [-Tw/2,+Tw/2)

r = sqrt(x.*x+y.*y);
z = r<d/2;
z =double(z);
z(find(r==d/2))=0.5;

t = abs(t);
y = double(t<Tw/2);
y(find(t==Tw/2)) = 0.5;

Matlab

M
% circ_Fourier.m
% Calculate the Fourier transform of a circle function
% Author: Chengshan Guo
% Date:March.14,2002
%
M=256;N=256; dx=10.0;dy=10.0; Diameter=20; % Parameters
[x,y]=meshgrid(-M/2:1:M/2-1,-N/2:1:N/2-1);
DScreen=circ(x,y,Diameter);
figure(1); colormap(gray); imshow(DScreen);
DScreen_F=fftshift(fft2(fftshift(DScreen)));
Amplitude=abs(DScreen_F)/max(max(abs(DScreen_F)));
figure(2);
imshow(Amplitude,[0 0.5]); % Amplitude distribution
figure(3);
imshow(angle(DScreen_F)+pi,[0 0.5]); % Phase distribution

Matlab

9. MATLAB
9.1

tic, s=0;
for i=1:1000000, s=s+(1/2^i+1/3^i); end; s,toc
s=
1.5000
elapsed_time =
16.003
tic, i=1:1000000; s=sum(1./2.^i+1./3.^i), toc
s=
1.5000
elapsed_time =
2.954

Matlab

Matlab

9.2 .
h_{i,j}=1/(i+j-1)
1

1
tic
for i=1:5
for j=1:10000
H(i,j)=1/(i+j-1);
end
end
Toc

tic,
for j=1:10000
for i=1:5
J(i,j)=1/(i+j-1);
end
end
Toc

elapsed_time=2.7240

elapsed_time=23.1230

tic
for i=1:5
for j=1:10000
H(i,j)=1/(i+j-1);
end
end
toc
elapsed_time =
2.7640

9.4

Matlab

2Matlab

1Matlab(DFT)

## g ( x, y ) exp [ j 2 ( x + y)] dxdy

G ( , ) exp [ j 2 ( x + y)] d d

M 1 N 1

F ( p, q ) = f (m, n)e j (2 / M ) pm e j (2 / N ) qn ,
m=0 n=0

f (m, n) =

1
MN

M 1 N 1

F ( p , q )e

j (2 / M ) pm

e j (2 / N ) qn ,

p =0 q =0

elapsed_time =
0.0300

9.5 Mex

10 Matlab

g ( x, y ) =

tic
H=zeros(5,10000);
for i=1:5
H(i,:)=1./[i:i+9999];
end
toc

9.3

Matlab

G ( , ) =

p = 0,1,..., M 1
q = 0,1,..., N 1
m = 0,1,..., M 1
n = 0,1,..., N 1

1
1
, =
M x
N y

Syntax
Y = fft2(X)
Y = fft2(X,m,n)

Syntax
Y = fftshift(X)
Y = fftshift(X,dim)

f = zeros(30,30);
f(5:24,13:17) = 1;
imshow(f,'notruesize')
F = fft2(f,256,256);
F2 = fftshift(F);
F2_I=abs(F2)/max(abs(F2(:)));
figure(2);
imshow(F2_I,[0 0.1],'notruesize');
colormap(gray); colorbar

f(x,y)
fft2{f(x,y)}
fftshift{fft2{f}}

Matlab

3
M=64;N=64; dx=10.0;dy=10.0; Diameter=20;
[x,y]=meshgrid(-M/2:1:M/2-1,-N/2:1:N/2-1);
DScreen=circ(x,y,Diameter);

tic
%
for nf=-M/2:M/2-1
for mf=-N/2:N/2-1
DS_F(nf+M/2+1,mf+N/2+1)=sum(sum(Dscreen
.*exp(-i*2.0*pi*(x*nf/M+y*mf/N))));
end
elapsed_time = 11.0470
end
toc
%
tic
DS_F2=fftshift(fft2(fftshift(DScreen)));
toc
elapsed_time = 0.1100