You are on page 1of 29

MATLAB

MATLAB .
.
MATLAB :






MATLAB interactive
.
.
noninteractive FORTRAN C Pascal.

MATLAB MATrix LABoratory MATLAB .


MATLAB .

. MATLAB.
MATLAB / .

Object oriented . MATLAB
MATLAB . C FORTRAN .
MATLAB .
MATLAB
.
MATLAB
. MATLAB.


MATLAB
. :
4*25+6*22+2*99
= ans
430


: .
.
.

5+3

23-12

MATLAB

3.14 * 0.85

/ \

56/8=8\56

5^2

MATLAB

MATLAB
. . who
.
. .
Save Workspace As... File
. LOAD Worksapce... File
.



load . :

save

save

matlab.mat .:
data

save

data.mat .:
data A B C

save

A B C data.mat .
save data A B C -ascii
data ascii .
extension mat .
.


MATLAB .
:
a=8/3
= a
2.6667

long

format :
= a
2.66666666666667

format long
format short e
format long e
format hex
format bank
format +
format rat
format short


2.66666666666667

2.6667e+000
2.666666666666666
e+000
4005555555555555

2.67
+
8/3
2.6667

MATLAB


MATLAB .
.

MATLAB

MATLAB .

Items, items, itEms, ITEMS

MATLAB

howaboutthisvariablename


_ .

how_about_this_variable_name
X51483
a_b_c_d_e


ans

pi

= /

eps

flops

inf

1/0

NaN

" " 0/0

j i

nargin

nargout

realmin

realmax

:
clear

.
.


%
. :
diametre=4
% diameter of circle.
= diameter
4

) ; ( :
; diametre=4
% diameter of circle.

:
; diameter=4; A=0.45

) ( :
area=pi * ...
(diameter/2)^2
=

area

12.5664

Ctrl-C .


MATLAB .
MATLAB :
c1=1-2i
= c1
1.000-2.000i

MATLAB

c2=6+sin(.5)*j
6.0000+0.4794i


MATLAB
. .

) abs(x
) acos(x
) acosh(x
) angle(x
) asin(x
) asinh(x
) atan(x
)atan2(x,y

) atanh(x
) ceil(x
) conj(x
) cos(x
) cosh(x
)exp(x
)fix(x
)floor(x
)gcd(x,y
)imag(x
)lcm(x,y

e x




)log(x

) log10(x
) real(x
) rem(x,y
) round(x
) sign(x
) sin(x
) sinh(x
) sqrt(x ) (
) tan(x
) tanh(x
Script

.
.
Script .
.m MATLAB .
Script M .
example1.m .
% exapmle1 script file
;a=4
;b=5
c=a^2+b

example1 :
= c
21

Script echo on
. echo off .
Extension

MATLAB

M
.
M .

) disp(ans
echo
input
keyboard
pause
) pause(n n
waitforbuttonpress


MATLAB .
.

cd
p=cd p
cd path path
chdir
chdir path

cd
cd dir

delete test M test.m


dir
ls

dir

matlabroot MATLAB

path MATLAB
pwd

cd

type test test.m


what M MAT
which test test.m

clc
diary
home
more


MATLAB : help
lookfor .MATLAB
help .
help :
help sqrt
SQRT
Square root.
SQRT(X) is the square root of the elements of X. Complex
results are produced if X is not positive.
See also SQRTM.

help

MATLAB

MATLAB

lookfor M
:
lookfor complex
Complex conjugate.
Complex imaginary part.
Complex real part.
Complex diagonal form to real block
diagonal form.
Real block diagonal form to complex
diagonal form.
Sort numbers into complex conjugate
pairs.

CONJ
IMAG
REAL
CDF2RDF
RSF2CSF
CPLXPAIR

complex .
help
.


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

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

:
A=[ 1 2 3
4 5 6
]7 8 9

.
) sqrt(3.


:
10

B=0:1:10
=B
0 1 2

:
10

C=0:2:10
=c
0 2 4

] x=[ 2 2*pi sqrt(2) 2-3j


x=first:last x first
last
x=first:increment:last x
increment last
first

) x=linspace(first,last,n x first last


n
)x=logspace(first,last,n

x 10first
10last n

.
:
] C=[1;2;3;4;5
= c
1
2
3
4
5

MATLAB



.
.
* .
.
* . .
/ \ A B A

A\B

B/A

).B*inv(A

inv(A)*B

a=[a1 a2 ... an], b=[b1 b2 ...


>bn], c=<a scalar
]a+c=[a1+c a2+c ... an+c

] a*c=[a1*c a2*c ... an*c


] 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 ... a3\b3
] a.^c=[a1^c a2^c ... an^c
] c.^a=[c^a1 c^a1 ... c^an
]a.^b=[a1^b1 a2^b2 ... an^bn

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

= A

3
6
9

2
5
8

1
4
7

A(3,3)=0 % set element in 3rd row, 3rd column to zero

= A
3
6
0

2
5
8

1
4
7

A(2,6)=1 %set element in 2nd row, 6th column to one

= A
0
1
0

0
0
0

0
0
0

3
6
9

2
5
8

1
4
7

A=[1 2 3;4 5 6;7 8 9]; %restore original data


)B=A(3:-1:1,1:3

= B
9
6
3

8
5
2

7
4
1

][=)B(:,2

= B
9
6
3

7
4
1

][ .
find .
:
x=-3:3
3

-1

-2

-3

= x

)k=find(abs(x)>1
7

= k

find x
.
. .

MATLAB

) i=find(x x
) [r,c]=find(x x

whos .
length size
.

MATLAB

)balance(A

)cdf2rdf(A

)chol(A

)cond(A

)condest(A
)d=eig(A),[V,D]=eig(A
)det(A
)expm(A

1-norm

)expm1(A

)expm2(A

)expm3(A

)funm(A,fun

expm

fun

)hess(A

)inv(A

)logm(A
)lscov(A,b,V
)lu(A

)nnls(A

)norm(A

)norm(A,1

)norm(A,2

)norm(A,inf
)norm(A,p
)norm(A,fro

)null(A

)orth(A

)pinv(A

)poly(A

)polyvalm(A

)qr(A

)qrdelete(Q,R,j

)qrinsert(Q,R,j,x

qr

)qz(A,B

)rank(A

)rcond(A

)rref(A

)rsf2csf(U,T

schur

)schur(A

)sqrtm(A

schur

qr

echelon

schur

MATLAB

)svd(A
)trace(A

][
compan

companion

eye
gallery
hadamard
hankel
hilb
invhilb

Hadamard
Hankel
Hilbert
Hilbert

magic
ones
pascal
rand
randn

rosser
toeplitz
vander
wilkinson

Toeplitz
Vandermonde
Wilkinson

zeros

.
.
.
.

<
=<
>
=>
==
=~

:
A=1:9

= A
9

tf=A>4
0

= tf
0


.
.
A

&
|
~

)tf=~(A>4
0

= tf
1

MATLAB
)tf=(A>2)&(A<6
0

= tf
0

) xor(x,y x y
.
) any(x x .
) all(x x .

MATLAB
. .

finite .
isempty .
isglobal
.
ishold
.
isieee
IEEE .
isinf .
isletter .
isnan .
isreal .
isspace

whitespace

characters.

issparse
matrix.
isstr
string.

sparse

character

isstudent
.
isunix UNIX.
isvms VMS.



.:

MATLAB

?t= How about this character string

= t
?How about this character string

t :
)u=abs(t

104

116

32

116

117

111

114

101

116

99

97

114

63

103

= u
Columns 1 through 12
72 111 119 32
97 98
Columns 13 through 24
105 115 32 99 104 97
Columns 25 through 32
32 115 116 114 105 110

:
)v=setstr(u

= v
?How about this character string

abs

ASCII

MATLAB

dec2hex
fprintf
hex2dec
hex2num

IEEE

int2str
lower
num2str
setstr ASCII
sprintf
sscanf
str2mat
str2num
upper


MATLAB
. : for while
if-Else-End .
for :
for x = array
}{commands
end

:
n=1:10

for

;)x(n)=sin(n*pi/10
end
x

0.8090

0.9511

1.0000

0.9511

= x
Columns 1 through 7
0.3090 0.5878 0.8090
Columns 8 through 10

0.0000

0.3090

0.5878


. :
; n=1:10
;) x=sin(n(pi/10
while :
while expression
}{commands
end

if-else-end :
expression
}{commands
end

if

:
if
expression
}{commands evaluated if True
else
}{commands evalusted if False
end

MATLAB MATLAB
MATLAB . extension .m .
help
. :
)function r = eq2deg(x
% This m-file calculates roots of a second degree eq.
% x is a row matrix which contains decending
% coefficients % of equation.
% r is a row matrix which contains the roots of the
% equation.
;)r=zeros(1,2
;)r(1)=(-x(2)+sqrt(x(2)^2-4*x(1)*x(2))) / (2*x(1
;)r(2)=(-x(2)-sqrt(x(2)^2-4*x(1)*x(2))) / (2*x(1

MATLAB

. eq2deg.m .
MATLAB . MATLAB help

eq2deg .
r .
:
)function r = eq2deg(x
% This m-file calculates roots of a second degree eq.
%
x
is
a
row
matrix
which
contains
decending
coefficients % of equation.
% r is a row matrix which contains the roots of the
% equation.
if length(x) ~= 3
;)fprintf(No. of coefficients is not suitable
else
;)r=zeros(1,2
;)r(1)=(-x(2)+sqrt(x(2)^2-4*x(1)*x(2))) / (2*x(1
;)r(2)=(-x(2)-sqrt(x(2)^2-4*x(1)*x(2))) / (2*x(1
end

MATLAB .
.
global . global
.
global .
M MATLAB .
return .


plot .:
)plot(x,y

;)y=sin(x

;)x=linspace(0,2*pi,30

.:

;)plot(x,y,x,z

;)z=cos(x

y z :
w=[y;z]; % create a matrix of the sin and cosine
plot(x,w) % plot the columns of w vs. x

bar
compass
errorbar
feather
fill
hist
image
loglog
plot
polar
rose
semilogx
semilogy
stairs
stem

MATLAB

-.
--

axis

clf
close
figure
grid
gtext
hold
subplot
text
title
xlabel
ylabel
zoom

)corrcoef(x

correlation

)cov(x

covariance

) cplxpair(x
) cross(x,y
) cumprod(x
) cumsum(x
) del2(A
) diff(x
) dot(x
) gradient(x x
) histogram(x
) max(x), max(x,y
) mean(x
) median(x
) min(x), min(x,y
) prod(x
) rand(x
) randn(x
) sort(x
) std(x
) subspace(x
) sum(x

MATLAB

) conv(a,b
) [q,r]=deconv(a,b
) poly(r
) polyder(a
) polyfit(x,y,n
) polyval(p,x
) [r,p,k]=residue(a,b
) [a,b]=residue(r,p,k
) roots(a

) polyfit(x,y,n
) interp1(x,y,xo
) interp1(x,y,xo,spline spline
) interp1(x,y,xo,'cubic
) interp2(x,y,z,xi,yi
) interp2(x,y,z,xi,yi,cubic
) interp2(x,y,z,xi,yi,neares t

MATLAB
.
MATLAB .
fplot


. humps M
:
)function y = humps(x
% HUMPS is a function with strong maxima near x = 0.3
and
% x = 0.9.
;y = 1 ./ ((x-0.3).^2+0.01) +1 ./ ((x-0.9).^2 +0.04) - 6

:
)]fplot(humps,[0 2
)title(FPLOT of hunps

100
80
60
40
20
0
2

1.5

0.5

-20

humps


fmin fmins .:
fmin

MATLAB

fzero .:
)fzero(humps,1.2
= ans

1.3200


quad trapz quad8 :
)quad(humps,-1,2
ans = 26.3450

You might also like