Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 41

Matlab

1.1

Matlab 0
''c ' 'java
0

) (if 'c' 0
' 'c
) (command window 0

0
)0(GUI
1-1-1
)(

Matlab icon


)(
) (
-1 )(command window
-2 )(work space
-3 )(current directory
-4 )(command history
1-1-2 )(command window

0 )
(
<<( )
a=3
= a
3


0
;a=3

0
- 0
) (
) (editor m-file 0
0
1-1-3 )(work space

m-file 0
0 save
Matlab
filename.mat 0 Matlab
load filename 0
0
) Matlab ( m.file work space
clear 0
1-1-4 )(current directory
Matlab 0

1-1-5 )(command history



0
1-1-6 )(help
) ) help 0
0
0 PDF files
0 0
) (help ) (Demo
Demo
Demo

0 0
) (Matlab
) (tool box )00 (simulink 0 )
(Demo )(Matlab ,
) ) mathematics )00 (graphics 0
, ) ( basic matrix operation
0
(run this Demo 22 ,
) (of 22 4 , 0

1.2

1-2-1
Matlab 0 ball, BALL
0
) (- ) (19 0

)(default

inf

)(not a number
i, j

ans
pi
infinity
nan
)i=j=sqrt (-1

1-2-2 )(arithmatic operation



;a=3; b=4
a+b
= ans
7
a*b
= ans
12
a/b
= ans
0.7500
a-b
= ans
-1
a^b
= ans
81
)sqrt(b
= ans
2

)(relation operation
1-2-3
or 0 1
> =< ,=> ,=~ ,== ,< , | , OR & AND

;a=9;b=3
tf1=a<4
= tf1

tf2=a>5
= tf2
1
tf3=a>5&b>4
= tf3

tf4=a>5&b<4
= tf4
1

tf1,tf2,tf3 and tf4 or 1 0


0
1-2-4

;c=1-2i;d=1+2i
e=c*d
= e
5
f=c+d
= f

2
fc=c-d
= fc
0 - 4.0000i

;a=1; b=4; c=13


)X1= (-b+sqrt (b^2-4*a*c))/ (2*a

= X1
-2.0000 + 3.0000i


)magX1=abs(X1
= magX1
3.6056


)angleX1=angle(X1
= angleX1
2.1588


degX1= angleX1*180/pi
= degX1
123.6901

)realX1=real(X1
= realX1
-2


)imagX1=imag(X1
= imagX1
3

1-2-5
1-2-5-1 )sin(x) , cos(x) , tan(x) , sec(x) , cot(x
x

;x=30 *pi/180
)sin(x
= ans
0.5000

(sin (x))^2+(cos(x))^2
= ans


)asin(.5
= ans
0.5236

angle =(asin(.5))*180/pi
= angle
30.0000

1-2-5-2



)ceil(x
)fix(x
)floor(x
)round(x

)x=3.45; round(x
= ans

1-2-6


]a=[1,2 ,3
= a
3

1
]b=[3,4,2
3

= b


]c=[1 3 5;4 6 2
= c
5
2

3
6

1
4


)d=c(1,:
= d
5

1
)e=c(2,:
4

= e

c
]c=[d;e
= c
5
2

3
6

1
4



)f=c(:,1
= f
1
4


)x=(0:0.1:1

0.6000

0.5000

0.4000

0.3000

= x
Columns 1 through 9
0
0.1000
0.2000
0.7000
0.8000
Columns 10 through 11
0.9000
1.0000

1.8850

1.5708

x=(0:.1:1)*pi

1.2566

0.9425

= x
Columns 1 through 9
0
0.3142
0.6283
2.1991
2.5133
Columns 10 through 11
2.8274
3.1416

)(matrix transpose
'g=a
= g
1
2
3


3.0000 + 3.0000i

2.0000 + 2.0000i

]h=[1+1i,2+2i,3+3i
= h

1.0000 + 1.0000i

'k=h.
= k
1.0000 + 1.0000i
2.0000 + 2.0000i
3.0000 + 3.0000i
'm=h
1.0000 - 1.0000i
2.0000 - 2.0000i

= m

3.0000 - 3.0000i

0
conjugate
1-2-7
B X=A

]A=[1 1 0;2 3 1; 4 2 1
0
1
1

1
3
2

= A

1
2
4

]B=[1 2 1
= B
1
'X=inv(A)*B
0
1
-1

= X

) inv(A A )(A^-1
1-2-8

)zeros(3
= ans
0
0
0

0
0
0

0
0
0
)ones(2,4

1
1

3.1416
3.1416
3.1416

1
1

3.1416
3.1416
3.1416

= ans

1
1
1
1
ones(3)*pi
= ans
3.1416
3.1416
3.1416


)eye(3

0
1
0

0
0
1

= ans

1
0
0


)rand(3,1
= ans
0.9501
0.2311
0.6068

1-2-9
C
]x=[1 3 5 7
)for k=1:length(x
y(k)=2*x(k)+1
end
= x
7

1
3

15

11

11

= y
= y
= y
= y

) length(x x

]data=[ 3 9 45 6;7 16 -1 5
for n=data
)x=n(1)-n(2
end
6
5

45
-1

9
16

= data
3
7
= x
-4
= x
-7
= x
46
= x
1


for n=1:5

for m=5:-1:1
A(n,m)=n^2+m^2;% equation to determine the value of A
end
)disp(n
end
1
2
3
4
5
A
= A
26
29
34
41
50

17
20
25
32
41

10
13
18
25
34

5
8
13
20
29

2
5
10
17
26


%
for
n=1:10
)x=sin(n*pi/10
10
0.8090

0.9511

1.0000

6
0.9511

5
0.8090

= n

1
2
3
= x
Columns 1 through 9
0.3090
0.5878
0.5878
0.3090
Column 10
0.0000

x n x
1-2-10 if
if
1-if logical expression
statements
end
2- if logical expression
statement group 1
else
statement group 2
end


;]z=[4 -9 25
)for k=1:length(z
)x=z(k
if x>=0
)y=sqrt(x
else
)'disp('some of the elements of x are negative
end
end
= x
4
2

= y
= x

-9
some of the elements of x are negative
= x
25
= y
5

x=-9 y
1.3

Matlab figure
plot
]y=[1 2 3 4 5 6 5 4 3 2 1
)plot(y
1

= y

6
5.5
5
4.5
4
3.5
3
2.5
2
1.5
11

10

y
0
;)x=linspace(0,2*pi,30
;)y=sin(x
)plot(x,y
grid
)'xlabel('independent variable x
)'ylabel('independent variable y
)'title('sin curve

grid xlabel ,
ylabel title

sin curve

1
0.8
0.6

0.2
0
-0.2
-0.4

independent variable y

0.4

-0.6
-0.8
7

3
4
independent variable x

-1


;)x=linspace(0,2*pi,30
;)y=sin(x
;)z=cos(x
)'plot(x,y,'*',x,z,'-
grid
)'xlabel('independent variable x
)'ylabel('independent variable y
)'title('sin&cos curves
)')text(3,.2,'sin(x
)'gtext('cos x

y,z text
gtext 0
plot3
)plot3(x,y,z

sin&cos curves

1
0.8

independent variable y

0.6
0.4
0.2

sin(x)

cos x

0
-0.2
-0.4
-0.6
-0.8
-1

3
4
independent variable x

1
0.5
0
-0.5
-1
1
0.5
0
-0.5
-1


0
hold on
hold off plot0
)subplot(m,n,p
m
n
p ) (

;)x=linspace(0,2*pi,30
;)y1=sin(x
;)y2=cos(x
;)y3=sin(2*x
;)y4=cos(2*x
)subplot(2,2,1
;)plot(x,y1
; )'title('y1
;)subplot(2,2,2
;)plot(x,y2
;)'title('y2
;)subplot(2,2,3
;)plot(x,y3
;)'title('y3
;)subplot(2,2,4
;)plot(x,y4
;)'title('y4

y1

1
0.5

0.5

-0.5

-0.5

-1

y3

-1

0.5

-0.5

-0.5
0


x=linspace(0,2*pi,30);
y1=sin(x);
y2=cos(x);
y3=sin(2*x);
y4=cos(2*x);
subplot(2,2,1)
stairs(x,y1);
title('y1') ;
subplot(2,2,2);
stairs(x,y2);
title('y2');
subplot(2,2,3);
stairs(x,y3);
title('y3');
subplot(2,2,4);
stairs(x,y4);
title('y4');

-1

y4

0.5

-1

y2

stairs

y2

y4

1.4

y1

0.5

0.5

-0.5

-0.5

-1

y3

-1

0.5

0.5

-0.5

-0.5

-1

-1


1-4-1 ))cell

) (interger , character
0 0
matlab cell array 0

]a=[2 3 5
]b=[1 2;4 6
'd=['a' 'c
}c={a,b,d

]''b

''acb

][2x2 double

2
6

1
4

][1x3 double

= a
= b
= d
acb
= c

0
'e='ahmed
= e
ahmed
}f={a,b,d,e
''ahmed

''acb

][2x2 double

= f

][1x3 double


}c{1,1
}c{1,2
}c{1,3
= ans
5

2
6

1
4

= ans
= ans
acb


)g=c(1,2
= g
][2x2 double


}g1=c{1,2
= g1
2
6

1
4

1-4-2 )(structure array



0
circle radius center
line style color
;circle.raduis=5
;]circle.center=[2 3
;'circle.linestyle='-
;'circle.color='red
circle
5
][2 3
''-
''red

circle
size

= circle
raduis:
center:
linestyle:
color:

size(circle)
ans =
1

circle(2).raduis=2;
circle(2).center=[1 3];
circle(2).linestyle='*';
circle(2).color='green';
circle(2)
ans =

raduis:
center:
linestyle:
color:

2
[1 3]
'*'
'green'

size(circle)
ans =


circle(1).filled='yes'
circle =
1x2 struct array with fields:
raduis
center
linestyle
color
filled


x=circle(2).raduis
a=pi*x*x %area of the circle
x =

a =

12.5664

0
( help)
fieldnames(circle)
ans =
'raduis'
'center'
'linestyle'
'color'
'filled'
isfield(circle,'center')
ans =

1
class(circle)
ans =
struct
isstruct(circle)
ans =
1
rmfield(circle,'color')
ans =
1x2 struct array with fields:
raduis
center
linestyle
filled


Matlab
2-1
Matlab
) (help 0
Matlab
0 )user
(defined function
0
0
)(tool box
) (communication tool box
) (control tool box
0
0

(signal sources ) (white noise


0
) (
0
0

2-2 )(user defined function


matlab m-file
-1 work space
editor run
;]x=[0:0.01:5
;)y=2*sqrt(x
;)z=4*sin(3*x
;)'plot(x,y,x,z);xlabel('x');gtext('y');gtext('z

save as file
filename.m debug run
y, z
5
4
z

2
1
0
-1
-2
-3
5

4.5

3.5

2.5
x

1.5

0.5

-4

-2 ) ( 0
command window
)Function y = funname(x

y 0
x
) ( 0 x,y
0
%

)Function y = average (x
% Average mean of vector elements
%average(x), where x is a vector, is trhe mean of vector elements.
% non vector elements results in an error.
;)[m,n]=size(x
))if (~(m==1) | (n==1) | (m==1&n==1
)'error('input must be a vector
end
;)y= sum(x)/length (x
editor )(average.m

;z=1:99
)average (z
= ans
50

x z x
m ,n
-1
-2
-3
m ,n ) ( c
0

global r
0

2-3 )(polynomials
x^2+2*x+3

x^3+2*x^2+3*x+1

]p=[ 0 1 2 3
]g=[1 2 3 1
3

= p
= g

0
0

q=p+g
4

= q

conv
)c=conv(p,g
= c
3

11

13

10

decon

)[g1,s]=deconv(g,g

= g1

= s

polyval
x=3
)v=polyval(p,x
3
18

= x
= v

roots

)r1=roots(p
)r2=roots(g
+ 1.4142i
1.4142i

= r1
-1.0000
-1.0000
= r2
-0.7849
-0.7849
-0.4302

+ 1.3071i
1.3071i

poly

1.0000

)pr1=poly(r1
)gr2=poly(r2

3.0000

2.0000

3.0000

2.0000

= pr1
1.0000
= gr2
1.0000

polyder

)polyder(p
= ans
2

y x
polyfit

;x=0:1:10
;]y=[1.1 2.3 5.1 10.2 17.4 26.4 37.6 50.8 65.4 82.7 102
)f=polyfit(x,y,2

1.1252

0.0414

1.0035

= f

;x=0:1:10
)z=polyval(f,x

37.4994

26.4196

17.3467

10.2808

= z
Columns 1 through 9
1.1252
2.1701
5.2220
50.5863
65.6801
Columns 10 through 11
82.7810 101.8888

y z
x
y 0 )
( plot
)plot(x,y,'*',x,z

120

100

80

60

40

20

10

2-4
matlab ) (numerical analysis )
(numerical methods 0
0
Runge-Kutta methods with variable step size
solver solver ode23 ,ode45
ode23 Runge-Kutta
ode 45 Runge-Kutta 0

)[t ,y]=ode45('ydot' ,tspan ,y0

''ydot
) (m-file
tspan
][t0 ,te
y0
y t0 ) (initial condition

1
0.1 dy/dt +y =0
dy/dt =-10 y
editor

)function ydot=rccirc(t,y
ydot=-10 *y
rccirc.m


;)[t,y] = ode45('rccirc' ,[0,0.4] ,2
;)ya=2*exp(-10*t
)plot(t,y,'*',t,ya

2
1.8
1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
0.4

0.35

0.3

0.25

0.2

0.15

0.1

0.05

rccirc
t=0 :0.4 y=2
) (analytical solution )y=2*exp(-10*t
2 non linear pendulum model

+g/l sin(e)=0


0
x1=e , x2=
d x1/dt ==x2
))d x2/dt = =-(g/l)(sin(x1

editor
)function xdot = pendul(t,x
global g l
]))xdot = [x(2); -(g/l)*sin(x(1
pendul.m

x1(0)=0.5 , x1(0)= 0.8pi
global g l
;g=9.81; l=1
;)][ta,xa]=ode45('pendul', [0,5],[0.5,0
;)][tb,xb]=ode45('pendul', [0,5],[0.8*pi,0
;))plot(ta,xa(:,1),tb,xb(:,1
;)')xlabel('time(sec
;)')ylabel('angle(rad
;)'gtext('case1

;)'gtext('case2

2
2esac
1
1esac

0
(dar)elgna

1-

2-

5.4

5.3

2 5.2
(ces)emit

5.1

5.0

3-

case1 , case2
0
2-5



syms x y

e=(x-5)^2+(y-3)^2
= e
^2)^2+(y-3)(x-5


)collect(e
= ans
^2)x^2-10*x+25+(y-3

)collect(e,y
= ans
^2+9)y^2-6*y+(x-5

)2^(x+y


)expand((x+y)^2
= ans
x^2+2*x*y+y^2

) sin(x+y

))expand(sin(x+y
= ans
)*sin(y)+cos(x)*cos(y)sin(x


syms x y
e1=x^2+5
= e1
x^2+5
e2=y^3-2
= e2
y^3-2
s1=e1+e2
= s1
x^2+3+y^3
s2=e1*e2
= s2
)*(y^3-2)(x^2+5
)expand (s2
= ans
x^2*y^3-2*x^2+5*y^3-10
s3=e1/e2
= s3
)/(y^3-2)(x^2+5

e1=x^2+5
= e1
x^2+5
)e2=1/(x+6
= e2
)1/(x+6
)[num,den]=numden(e1+e2
= num
x^3+6*x^2+5*x+31
= den
x+6

poly2sym

)]e=poly2sym([2 6 4
= e
2*x^2+6*x+4


)sym2poly (e
= ans
4

2
syms x y
)subs(e,x,y
= ans
2*y^2+6*y+4

x ezplot

syms x
;e=x^2-6*x+7
)]ezplot(e,[-2,6

x 2 -6 x+7

02

51

01

2
x

1-

2-

solver
solver
'eq1='x+5=0
)solve(eq1
= eq1
x+5=0
= ans
-5


)'solve('y^2+3*y+2=0
= ans
-1
-2

x ,y x y
)'solve('x^2+9*y^4=0
= ans
3*i*y^2
-3*i*y^2

x ,y
;'eq4='6*x+2*y=14
;'eq5='3*x+7*y=31

)[x,y]=solve(eq4,eq5
= x
1
= y
4

syms x y b
'cir1='(x-3)^2+(y-5)^2=4
'cir2='(x-5)^2+(y-3)^2=b
)[x y]=solve(cir1,cir2
= cir1
^2=4)^2+(y-5)(x-3
= cir2
^2=b)^2+(y-3)(x-5
= x
)^(1/2)9/2-1/8*b+1/8*(-16+24*b-b^2
)^(1/2)9/2-1/8*b-1/8*(-16+24*b-b^2
= y
)^(1/2)7/2+1/8*b+1/8*(-16+24*b-b^2
)^(1/2)7/2+1/8*b-1/8*(-16+24*b-b^2


)diff(e,v,n

e
v

n

syms x
))diff(sin(x^2
= ans
*x)2*cos(x^2

)int (e,v,a,b
e v a b 0 a ,b

syms x
)int(2*x
= ans
x^2
)int(1/x
= ans
)log(x

)int(x^2,2,5
= ans
39

syms x y
)int(x*y^2,y,0,5
= ans
125/3*x
syms x a b
)int(x^2,a,b
= ans
1/3*b^3-1/3*a^3


)limit (e ,v ,a ,d
e v d )(right or left
taylor ) taylor(f ,n ,a )(n-1
f a
syms x
)f=exp(x
)taylor(f,3,2
= f
)exp(x
= ans
^2)*(x-2)+1/2*exp(2)*(x-2)+exp(2)exp(2

2-6 )(simulink
Matlab )
(
)0 (block diagram
- simulink
) 0 (simulink library browser ) (model
new )0 (file menu

0 )
(ramp, unit step, sine wave etc )
(integrator, derivative ..etc ) (gain

0 ) (scope ) (xy graph


0 ) (
0
0
) (simulation
0 )(help
0

You might also like