You are on page 1of 36

MATLAB

MATLAB
.
,
,
.
MATLAB-

.


.

The MATLAB Desktop

Command Window:

MATLAB- command prompt(>>)- .
.
Workspace:
,
.

Command history: MATLAB- prompt


.
Current directory :
.
.

Editor window: -
.
,
,
M-File
Command Window
.
Figure window:

.


who- .
whos-
.
what- M, Mat, Mex .
clear- .
clear x y z- x,y,z
.
clear all- ,
.
mlock fun-fun . clear
.
munlock fun-fun .
lc - .
home -clc- .
lf - .


computer
lock
date
ore
ver


.
.
, , .

.
MATLAB- .

quit

MATLAB- .

xit

MATLAB- .


+
*
/
^
.*
./
.^

addition
subtraction
multiplication
division
exponentiation
term-by-term multiplication
term-by-term division
term-by-term exponentiation

>> x=2+2
x=
4
>> y=2^2+log(pi)*sin(x)
y=
3.1337

>> format long


>> y=2^2+log(pi)*sin(x)
y=
3.13366556593561


>> x=[1 2 3]
x=
1 2 3

>> y=[2; 1; 5]
y=
2
1
5
>> z=[2 1 0];
>> a=x+z
a=
3 3 3

>> a=x.*z

a=
2

>> b=2*a
b=

>> x=linspace(0,10,5)
[0:2.5:10]

x=
0

2.5000

5.0000

7.5000 10.0000

0.5985 -0.9589

0.9380 -0.5440

>> y=sin(x)
y=
0

>> z=sqrt(x).*y
z=
0

0.9463 -2.1442

2.5688 -1.7203

Matrices
>> A=[5 6 4; 12 7 8; 9 10
11]
A=
5 6 4
12 7 8
9 10 11
>> B=[1 3 4; 5 6 7;2 8 9]
B=
1 3 4
5 6 7
2 8 9

>> C=A+B
C=
6 9
17 13
11 18
>> D=A-B
D=
4 3
7 1
7 2

8
15
20

0
1
2

>> u= [3; 1; 4]
u=
3
1
4
>> v = [2 0 -1];
>> x = v*u
x=
2

B = magic(3)
B=
8 1 6
3 5 7
4 9 2

>> B*v
>> v*B

Transpose:
B=AT , that is, bij=aji
>> A=[2 3;6 7];
>> B=A'
B=
2 6
3 7
>> u=[3 2 1; 5 6 7; 8 9 10];
>> u'
ans =
3 5 8
2 6 9
1 7 10

>> a=[3 1 -1; 2 2 -1; -2 1 4]


a=
3 1 -1
2 2 -1
-2 1 4
>> det(a)
ans =
15
>> b=inv(a)
b=
0.6000 -0.3333 0.0667
-0.4000 0.6667 0.0667
0.4000 -0.3333 0.2667

>> A = [1 1 1;1 2 3;1 3


6]
A=
1
1
1

1
2
3

1
3
6

>> X=A^2
X=
3 6
6 14
10 25
>> X=A.^2
X=
1 1
1 4
1 9

10
25
46

1
9
36

Indexing (or subscripting)


>> A=[1 2 3;4 5 6;7 8 9];
>> A(2,3)
A(ij) i-,
ans =
j-
6
>> A(3,:)
whole row
ans =
7 8 9
>> A(:,2)
whole column
ans =
2
5
8

eye(m,n)
>> eye(3)
ans =
1 0 0
0 1 0
0 0 1
zeros(m,n)
ones(m,n)

eye(m,n)
1 (m,n)

rand(m,n)

1
(m,n)

>> rand(2,3)
ans =
0.6458 0.6649 0.0099
0.9669 0.8704 0.1370
diag(a)
a
>> a=[1 2 3 4]
a=
1 2 3 4
>> A=diag(a)
A=
1 0 0 0
0 2 0 0
0 0 3 0
0 0 0 4

diag(A)

>> diag(A)'
ans =
1 2 3 4
diag(A,1)
>> diag(A,1)'
ans =
0 0 0

A 1

MATLAB functions

pow2(x)
log(x)
log10(x)
exp(x)
sqrt(x)
abs(x)
cos(x)
acos(x)
sin(x)
asin(x)
tan(x)
atan(x)
cot(x)
acot(x)

2x
-
- 10
ex

x-

fix(x)



0-

A=
2.6000 7.8000 9.9000 1.2000 3.3000

>> fix(A)
ans =
2 7 9

floor(x)

>> a = [-1.9, -0.2, 3.4, 5.6, 7.0, 2.4+3.6i]


a=
-1.9000
-0.2000
3.4000
7.0000
2.4000 + 3.6000i

>> floor(a)
ans =
-2.0000
7.0000

-1.0000
3.0000
2.0000 + 3.0000i

5.6000

5.0000

ceil(x)


>> A=[2.6,7.8,9.9,1.2,3.3]
A=
2.6 7.8 9.9 1.2 3.3
>> ceil(A)
ans =
3 8 10

round(x)

x-

>> A=[2.6,7.8,9.9,1.2,3.3]
A=
2.6000 7.8000 9.9000 1.2000 3.3000
>> round(A)
ans =
3 8 10

magic(n)
M=magic(3)
M=
8 1 6
3 5 7
4 9 2

n>=3
>> sum(M)
ans =
15 15 15
>> sum(M')'
ans =
15
15
15

mod(x,y)

>> mod(13,5)
ans =
3
>> mod([1:5],3)
ans =
1 2 0 1

>> mod(magic(3),3)
ans =
2 1 0
0 2 1
1 0 2

length(x)

>> x=ones(1,8)
x=
1 1 1 1
>> n=length(x)
n=
8


size
.

mean(x)
>> A=[5 6 7 4 9]
A=
5 6 7 4 9
>> mean(A)
ans =
6.2000
>> a=[1 2 3;4 5 6;7 8 9];
>> mean(a)
ans =
4 5 6


mean


.

max(x)

>> A=[5 6 7 4 9]
A=
5 6 7 4 9
>> max(A)
ans =
9
>> B=[5 6 7; 78 9 4; 41 4 2];
>> max(B)
ans =
78 9 7


max


.

min(x)

>> A=[5 6 7 4 9]
A=
5 6 7 4 9
>> min(A)
ans =
4
>> C=[3 4 5;2 6 5; 8 9 1];
>> min(C)
ans =
2 4 1


min



.

Operators (relational, logical)


<
<=
>
>=
==
~=
&
|
~
xor

less than
less than or equal
greater than
greater than or equal
equal
not equal
logical AND
logical OR
logical complement (NOT)
exlusive OR

f(x) a n x n a n -1x n -1 ..... a 0


1. r=roots(p)

s 3 6s 2 72s 2

>> p=[1 -6 -72 -27];


>> r=roots(p)
r=
12.1229
-5.7345
-0.3884

2. p = poly(r)

>> poly2str(p,'x')
ans =
x^3 - 6 x^2 - 72 x - 27

r=
12.1229
-5.7345
-0.3884
>> p = poly(r)
p=
1.0000 -6.0000 -72.0000 -27.0000

3.

c=conv(a,b)

a( x) x 2 2 x 3; b( x) 4 x 2 5 x 6
c ( x 2 2 x 3) (4 x 2 5 x 6)
>> a=[1 2 3];
>> b=[4 5 6];
>> c=conv(a,b)
c=
4 13 28 27 18
>> poly2str(c,'x')
ans =
4 x^4 + 13 x^3 + 28 x^2 + 27 x + 18

4. a=deconv(c,b)

c( x) 4 x 4 13x 3 28 x 2 27 x 18; b( x) 4 x 2 5 x 6
c( x)
a ( x)
b( x )
>> c=[4 13 28 27 18];
>> b=[4 5 6];
>> a=deconv(c,b)
a=
1 2 3
>> poly2str(a,'x')
ans =
x^2 + 2 x + 3

5. k = polyder(p)
k = polyder(a,b)

(3x 2 6 x 9) ( x 2 2 x)
>> a = [3 6 9];
b = [1 2 0];
k = polyder(a,b)
k=
12 36 42 18
>> poly2str(k,'x')
ans =
12 x^3 + 36 x^2 + 42 x + 18
6. y = polyval(p,x)

p ( x) 3 x 2 2 x 1
x 5;7;9

>> p = [3 2 1];
polyval(p,[5 7 9])
ans =
86 162 262

You might also like